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


Python History.undoLastMove方法代码示例

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


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

示例1: Board

# 需要导入模块: from history import History [as 别名]
# 或者: from history.History import undoLastMove [as 别名]

#.........这里部分代码省略.........
        # Check each opponent piece if it is attacking the King
        for row in xrange(self.rows):
            for col in xrange(self.cols):
                square = self._squares[row][col]
                piece = square.piece
                if piece and piece.getPieceColor() != playerColor:
                    movePossible = self.isMovePossible(piece.getPieceColor(),
                            square.position, kingPosition)
                    if movePossible:
                        return True

        return False

    """
    Try to move all pieces of player to all locations and check if there
    exists a valid legal move. If there is none, then the player has been
    check mated
    """
    def isPlayerCheckMate(self, playerColor):
        kingPiece, kingPosition = self._getKingOfPlayer(playerColor)
        pieces = self._getAllPiecesOfColor(playerColor)
        logger.debug(playerColor)
        logger.debug((kingPiece, kingPosition, pieces))

        if __debug__:
            pdb.set_trace()

        for piece in pieces:
            fromPosition = piece.getPiecePosition()
            for row in xrange(self.rows):
                for col in xrange(self.cols):
                    toPosition = (row, col)
                    if row == 1 and col == 4 and __debug__:
                        pdb.set_trace()
                    movePossible = self.isMovePossible(playerColor, fromPosition,
                            toPosition)
                    if movePossible:
                        logger.debug("%s to %s" %(fromPosition ,toPosition))
                        self.movePiece(fromPosition, toPosition)
                        if not self.isPlayerChecked(playerColor):
                            self.undoLastMove()
                            return False
                        self.undoLastMove()

        logger.debug("Player with color %s has been check mated! Game over buddy!" %playerColor)

        return True

    def movePiece(self, fromPosition, toPosition):
        if __debug__:
            pdb.set_trace()
        fromSquare = self._getSquareAtPosition(fromPosition)
        toSquare = self._getSquareAtPosition(toPosition)
        fromPiece = fromSquare.piece
        toPiece =  toSquare.piece
        fromSquare.removePiece()
        toSquare.setPiece(fromPiece)
        fromSquare.setPieceSquare()
        #fromPiece.setSquare(toSquare)

        #TODO: Add undo logic to store moved and killed piece locations
        self._history.makeMove(fromPosition, toPosition, toPiece)

    """
    Restores board state before the last move, if possible
    """
    def undoLastMove(self):
        if __debug__:
            pdb.set_trace()
        if self._history.canUndoLastMove():
            fromPosition, toPosition, killedPiece = self._history.undoLastMove()
            piece = self._getPieceAtPosition(toPosition)
            fromSquare = self._getSquareAtPosition(fromPosition)
            toSquare = self._getSquareAtPosition(toPosition)
            fromSquare.setPiece(piece)
            toSquare.setPiece(killedPiece)
            fromSquare.setPieceSquare()
            toSquare.setPieceSquare()

            return True

        return False

    def _getSquareAtPosition(self, position):
        row = position[0]
        col = position[1]
        return self._squares[row][col]

    def _getPieceAtPosition(self, position):
        square = self._getSquareAtPosition(position)
        return square.getPiece()

    @property
    def getCurrentBoardState(self):
        return self._squares

    def printCurrentBoardState(self):
        for row in xrange(self.rows):
            colInfo = ["".join(self._squares[row][col].getPieceInfoAtSquare()) for col in xrange(self.cols)]
            logger.debug(colInfo)
开发者ID:sayedatifali,项目名称:django-chess,代码行数:104,代码来源:board.py


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