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


Python Move.kingsideCastle方法代码示例

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


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

示例1: getPossibleMoves

# 需要导入模块: from Move import Move [as 别名]
# 或者: from Move.Move import kingsideCastle [as 别名]
    def getPossibleMoves(self):
        currentPos = self.position
        movements = [C(0, 1), C(0, -1), C(1, 0), C(-1, 0), C(1, 1), C(1, -1), C(-1, 1), C(-1, -1)]
        for movement in movements:
            newPos = currentPos + movement
            if self.board.isValidPos(newPos):
                pieceAtNewPos = self.board.pieceAtPosition(newPos)
                if self.board.pieceAtPosition(newPos) is None:
                    yield Move(self, newPos)
                elif pieceAtNewPos.side != self.side:
                    yield Move(self, newPos, pieceToCapture=pieceAtNewPos)

        # Castling
        if self.movesMade == 0:
            inCheck = False
            kingsideCastleBlocked = False
            queensideCastleBlocked = False
            kingsideCastleCheck = False
            queensideCastleCheck = False
            kingsideRookMoved = True
            queensideRookMoved = True

            kingsideCastlePositions = [self.position + C(1, 0), self.position + C(2, 0)]
            for pos in kingsideCastlePositions:
                if self.board.pieceAtPosition(pos):
                    kingsideCastleBlocked = True

            queensideCastlePositions = [self.position - C(1, 0), self.position - C(2, 0), self.position - C(3, 0)]
            for pos in queensideCastlePositions:
                if self.board.pieceAtPosition(pos):
                    queensideCastleBlocked = True

            if kingsideCastleBlocked and queensideCastleBlocked:
                return

            otherSideMoves = self.board.getAllMovesUnfiltered(not self.side, includeKing=False)
            for move in otherSideMoves:
                if move.newPos == self.position:
                    inCheck = True
                    break
                if move.newPos == self.position + C(1, 0) or move.newPos == self.position + C(2, 0):
                    kingsideCastleCheck = True
                if move.newPos == self.position - C(1, 0) or move.newPos == self.position - C(2, 0):
                    queensideCastleCheck = True

            kingsideRookPos = self.position + C(3, 0)
            kingsideRook = self.board.pieceAtPosition(kingsideRookPos) if self.board.isValidPos(kingsideRookPos) else None
            if kingsideRook and kingsideRook.stringRep == 'R' and kingsideRook.movesMade == 0:
                kingsideRookMoved = False

            queensideRookPos = self.position - C(4, 0)
            queensideRook = self.board.pieceAtPosition(queensideRookPos) if self.board.isValidPos(queensideRookPos) else None
            if queensideRook and queensideRook.stringRep == 'R' and queensideRook.movesMade == 0:
                queensideRookMoved = False

            if not inCheck:
                if not kingsideCastleBlocked and not kingsideCastleCheck and not kingsideRookMoved:
                    move = Move(self, self.position + C(2, 0))
                    rookMove = Move(self.position, self.position + C(1, 0))
                    move.specialMovePiece = self.board.pieceAtPosition(kingsideRookPos)
                    move.kingsideCastle = True
                    move.rookMove = rookMove
                    yield move
                if not queensideCastleBlocked and not queensideCastleCheck and not queensideRookMoved:
                    move = Move(self, self.position - C(2, 0))
                    rookMove = Move(self.position, self.position - C(1, 0))
                    move.specialMovePiece = self.board.pieceAtPosition(queensideRookPos)
                    move.queensideCastle = True
                    move.rookMove = rookMove
                    yield move
开发者ID:Daniel-NJ,项目名称:command-line-chess,代码行数:72,代码来源:King.py


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