本文整理汇总了Python中Board.movePiece方法的典型用法代码示例。如果您正苦于以下问题:Python Board.movePiece方法的具体用法?Python Board.movePiece怎么用?Python Board.movePiece使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Board
的用法示例。
在下文中一共展示了Board.movePiece方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BoardTest
# 需要导入模块: import Board [as 别名]
# 或者: from Board import movePiece [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()
#.........这里部分代码省略.........
示例2: getAgentForMove
# 需要导入模块: import Board [as 别名]
# 或者: from Board import movePiece [as 别名]
while True:
numMoves += 1
agt = getAgentForMove(numMoves, whiteAgent, blackAgent)
moveTuple = agt.getNextMove(board)
if moveTuple == checkmate:
printWinMessage(numMoves)
if getColorForMove(numMoves) == white: # the loser
blackWins += 1
else:
whiteWins += 1
break
elif moveTuple == stalemate or checkForStalemate():
printStalemateMessage(numMoves)
stalemates += 1
break
board.movePiece(moveTuple[0], moveTuple[1])
print "Game statistics:"
print "Games: %d" % simulationNum
print "White wins: %d" % whiteWins
print "Black wins: %d" % blackWins
print "Stalemates: %d" % stalemates
else: # use graphics
root = Tk()
boardGraphics = BoardGraphics(root)
board = Board()
boardGraphics.drawBoard(board.getGrid())
root.after(1000, getAndDrawNextMove)
root.mainloop()
示例3: __init__
# 需要导入模块: import Board [as 别名]
# 或者: from Board import movePiece [as 别名]
#.........这里部分代码省略.........
else:
tmp = tmp + "X|"
else:
tmp = tmp + " |"
tmp += "\n"
print (str(tmp))
# returns if the game is over
def isOver(self):
return self.game_over
# checks to see if the game has ended and sets game_over to true
def checkWin(self):
if self.board.countPlayerPieces ==0 or len(self.getMoves("Player")) == 0:
self.game_over = True
return "AI"
elif self.board.countAiPieces ==0 or len(self.getMoves("AI")) == 0:
self.game_over = True
return "Player"
"""
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: