你好!我正在制作一个国际象棋引擎,但 make/unmake 方法存在一些问题。
我有一个 Piece 类来保存棋子的类型(棋子、皇后等)和位置,还有一个 Move 类来保存目标方格、移动的棋子和捕获的棋子。
问题是,当我调用 makeMove 方法时,它会将片段的位置更改为 Piece 对象内的目标正方形。但是现在,我不能用 Move 对象调用 unmakeMove,因为现在我没有关于移动来自哪里的信息,因为我刚刚改变了棋子的位置。你将如何解决这个问题?
非常感谢!
class Board:
# Previous methods omitted.
def makeMove(self, move, player):
""" Makes a move on the board and changes Piece object. Returns None. """
self.moved_piece = move.getPiece()
self.captured_piece = move.getCapturedPiece(self)
if self.captured_piece: # Remove captured piece from player's piece dict.
player.removePiece(self.captured_piece)
self.setPiece(move.getTargetSquare(), self.moved_piece) # Set moved piece on target square.
self.setPiece(self.moved_piece.getPosition(), EMPTY) # Make the origin square empty.
self.moved_piece.changePosition(move.getTargetSquare()) # Change piece object's position.
def unmakeMove(self, move, player):
""" Unmakes a move. Returns None. """
self.moved_piece = move.getPiece()
self.captured_piece = move.getCapturedPiece(self)
self.setPiece(self.moved_piece.getPosition(), captured_piece) # Set captured piece or empty square to target square.
# Set piece to original square. HOW !?