Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 国际象棋编程使和使移动_Python_Chess - Fatal编程技术网

Python 国际象棋编程使和使移动

Python 国际象棋编程使和使移动,python,chess,Python,Chess,嗨!我正在制作一个国际象棋引擎,我对make/unmake方法有一些问题 我有一个持有棋子类型(典当、皇后等)和位置的棋子类,还有一个持有目标方块、移动棋子和捕获棋子的移动类 问题是,当我调用makeMove方法时,它会将工件的位置更改为工件对象内部的目标正方形。但是现在,我不能用Move对象调用unmakeMove,因为现在我没有关于Move来自何处的信息,因为我刚刚更改了工件的位置。你将如何解决这个问题 多谢各位 class Board: # Previous methods omitted

嗨!我正在制作一个国际象棋引擎,我对make/unmake方法有一些问题

我有一个持有棋子类型(典当、皇后等)和位置的棋子类,还有一个持有目标方块、移动棋子和捕获棋子的移动类

问题是,当我调用makeMove方法时,它会将工件的位置更改为工件对象内部的目标正方形。但是现在,我不能用Move对象调用unmakeMove,因为现在我没有关于Move来自何处的信息,因为我刚刚更改了工件的位置。你将如何解决这个问题

多谢各位

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 !?

根据我的评论和Fred Larson发布的内容,以下是您可能想要做的一个示例实现:

class Engine(object):
    def __init__(self):
        self.board = Board()
        self.move_list = []

    def make_move(self, from, to):
        #board.makeMove returns a "memento object".
        self.move_list.append(board.makeMove(from, to))
        ...

    def undo_move(self):
        board.undo(self.move_list.pop())
    ...
    ...
假设有这样一个结构的移动对象:

class Move(object):
    def __init__(self, from_coords, to_coords, capture=None):
        self.from = from_coords
        self.to = to_coords
        self.capture = capture
您的
Board
对象将实现以下方法:

class Board(object):
    ...
    def make_move(self, from_coords, to_coords):
        #move logic here
        return Move(from_coords, to_coords, capturedPiece)

    def undo_move(self, move_object):
        self.make_move(move_object.to_coords, move_object.from_coords)
        self.uncapture(move_object.capture, move_object.to_coords)
显然,上面的代码只是概念性的。实际实现将取决于其余代码的结构


注意:我为
Move
对象使用了一个类,因为属性访问是显式的,并且容易理解。实际上,像这样基本的对象可能只是一个元组形式的
(from,to,capturedpice)

您应该保留一个移动列表。那样的话,你就可以把最后一步从堆栈中弹出,做相反的动作,你就是黄金。你可能需要一个
reverseMove
方法和一个
uncapturePiece
方法。我以前不知道这一点。如果你保留了所有移动的列表,你也可以从列表中删除最后一个移动,然后将移动应用到起始位置的板上。这可以避免需要任何单独的撤消逻辑。