当前位置: 首页>>代码示例>>Python>>正文


Python Board.removePiece方法代码示例

本文整理汇总了Python中Board.removePiece方法的典型用法代码示例。如果您正苦于以下问题:Python Board.removePiece方法的具体用法?Python Board.removePiece怎么用?Python Board.removePiece使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Board的用法示例。


在下文中一共展示了Board.removePiece方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BoardTest

# 需要导入模块: import Board [as 别名]
# 或者: from Board import removePiece [as 别名]
class BoardTest(unittest.TestCase):
	def setUp(self):
		self.board = Board(8)
		self.board2 = Board(3)
		self.board3 = Board(4)
		
	def testBoardSize(self):
		self.assertEqual(8,self.board.getSize())
		self.assertEqual(3,self.board2.getSize())
		self.assertEqual(4,self.board3.getSize())
		
	def test_Remove_From_Board(self):
		self.board.removePiece(2,1)
		self.assertEqual(0,self.board.countAiPieces())
		self.board.removePiece(6,6)
		self.assertEqual(0,self.board.countPlayerPieces())
		
	def test_Add_To_Board(self):
		# board 1
		self.board.addPiece("AI",0,1,1)
		self.board.addPiece("Player",0,6,6)
		self.assertEqual(1,self.board.countPlayerPieces())
		self.assertEqual(1,self.board.countAiPieces())
		self.assertTrue(self.board.pieceAt(1, 1))
		self.assertTrue(self.board.pieceAt(6, 6))
		
		# board 2		
		self.board2.addPiece("test",0,1,1)
		self.board2.addPiece("AI",3,1,2)
		self.assertEqual(0,self.board2.countPlayerPieces())
		self.assertEqual(0,self.board2.countAiPieces())
		self.assertFalse(self.board2.pieceAt(1, 1))
		self.assertFalse(self.board2.pieceAt(1, 2))
		
		self.board2.addPiece("AI",1,2,1)
		self.assertEqual(0,self.board2.countPlayerPieces())
		self.assertEqual(1,self.board2.countAiPieces())
		self.assertTrue(self.board2.pieceAt(2, 1))
		
		# board 3
		self.board3.addPiece("AI",0,1,1)
		self.assertEqual(0,self.board3.countPlayerPieces())
		self.assertEqual(1,self.board3.countAiPieces())
		self.assertTrue(self.board3.pieceAt(1, 1))
	
	def test_Get_Piece_Info(self):
		# board 1
		self.piece = self.board.getPieceAt(6, 6)
		self.assertEqual("Player", self.piece.getOwner())
		self.assertEqual(1, self.piece.getType())
		
		# board 2
		self.piece = self.board2.getPieceAt(2, 1)
		self.assertEqual("AI", self.piece.getOwner())
		self.assertEqual(1, self.piece.getType())
		
		self.piece = self.board2.getPieceAt(1, 2)
		self.assertIsNone(self.piece)
		
		self.piece = self.board2.getPieceAt(1, 1)
		self.assertIsNone(self.piece)

		# board 3
		self.piece = self.board3.getPieceAt(1, 1)
		self.assertEqual("AI", self.piece.getOwner())
		self.assertEqual(0, self.piece.getType())
		
	def test_Move_Piece(self):
		self.assertTrue(self.board.pieceAt(1, 1))
		self.board.movePiece(1, 1, 2, 1)
		self.assertFalse(self.board.pieceAt(1, 1))
		self.assertTrue(self.board.pieceAt(2, 1))

		self.assertTrue(self.board2.pieceAt(2, 1))
		self.board2.movePiece(2, 1, 2, 2)
		self.assertFalse(self.board2.pieceAt(2, 1))
		self.assertTrue(self.board2.pieceAt(2, 2))

		self.assertTrue(self.board3.pieceAt(1, 1))
		self.board3.movePiece(1, 1, 2, 1)
		self.assertFalse(self.board3.pieceAt(1, 1))
		self.assertTrue(self.board3.pieceAt(2, 1))
	
	def test_Update_Piece(self):
		self.board.updatePieceType(1,6,6)
		self.piece = self.board.getPieceAt(6, 6)
		self.assertEqual("Player", self.piece.getOwner())
		self.assertEqual(1, self.piece.getType())
		
	def tearDown(self):
		self.board = None
		self.board2 = None
		self.board3 = None
	
	def main(self):
		self.setUp()
		self.testBoardSize()
		self.test_Add_To_Board()
		self.test_Update_Piece()
		self.test_Get_Piece_Info()
#.........这里部分代码省略.........
开发者ID:astrok100,项目名称:Checkers-Minimax-PyQt,代码行数:103,代码来源:testing.py

示例2: __init__

# 需要导入模块: import Board [as 别名]
# 或者: from Board import removePiece [as 别名]

#.........这里部分代码省略.........
    
    """ 
        validates if piece at X,Y can move to x1 , y1
        checks if x1 and y1 is inside the board and x1 and y1 is a empty space
        returns true or false
    """
    def validMove(self,x1,y1):
        if x1 < self.board.getSize() and x1>= 0 and y1 >= 0 and y1 < self.board.getSize():
            if not self.board.pieceAt(x1, y1):
                return True
        else:
            return False

    # moves a piece players piece from X Y to x1 y1
    def movePiece(self,player,x,y,x1,y1):
        
        moved = False
        #gets an array of movable positions x y given by user
        moves = self.getMoves(player)
        # move is the string of x y, x1 y1 for comparing to the array of movable positions
        move = str(x)+","+str(y)+","+str(x1)+","+str(y1)
        if not self.isOver() :
            if self.forceJump(player):
                #Second jump
                if self.jumpAgain[0]:
                    # if jumpagain is true get the jumpable moves for jumpAgain[1], jumpAgain[2]
                    moves = self.canJump(self.jumpAgain[1], self.jumpAgain[2])
                    
                    #compares moves to move if equal move the piece and remove the piece being jumped 
                    for i in moves:
                        if i == move:
                            self.board.movePiece(x, y, x1, y1)
                            # checks if a jump is true then remove the piece being jumped        
                            removePieceX = (x + (x1))/2
                            removePieceY = (y + (y1))/2
                            self.board.removePiece(removePieceX, removePieceY)
                            
                            #checks if it is king
                            if self.isKing(x1,y1):
                                self.board.updatePieceType(1,x1,y1)
                            
                            #checks if it can jump again sets jumpAgain to true if true else set to False
                            if self.canJump(x1, y1):
                                moved = False
                                self.jumpAgain=(True,x1,y1)
                                break
                            else:  
                                self.jumpAgain=(False,None,None)
                                moved = True
                                break    
                
                else:
                    # This is the first jump
                    for i in moves:
                        if i == move :
                            self.board.movePiece(x, y, x1, y1)
                            # checks if a jump is true then remove the piece being jumped        
                            removePieceX = (x + (x1))/2
                            removePieceY = (y + (y1))/2
                            self.board.removePiece(removePieceX, removePieceY)
                            #after jumping check if it can be Kinged and can it jump again 
                            if self.isKing(x1,y1):
                                self.board.updatePieceType(1,x1,y1)
                            
                            if self.canJump(x1, y1):
                                moved = False
开发者ID:astrok100,项目名称:Checkers-Minimax-PyQt,代码行数:70,代码来源:Checkers.py


注:本文中的Board.removePiece方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。