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


Python Board.movePiece方法代码示例

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


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

示例1: test_move_piece_white_knight

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import movePiece [as 别名]
 def test_move_piece_white_knight(self):
     from board import Board
     import constants
     b = Board()
    
     self.assertEqual(b._board[1][0], "n")
     b.movePiece(constants.WHITE_PLAYER, [1, 0, 2, 2])
     self.assertEqual(b._board[1][0], "")
     self.assertEqual(b._board[2][2], "n")
开发者ID:simfarm,项目名称:chess,代码行数:11,代码来源:test.py

示例2: test_move_piece_black_pawn

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import movePiece [as 别名]
 def test_move_piece_black_pawn(self):
     from board import Board
     import constants
     b = Board()
    
     self.assertEqual(b._board[4][6], "*p")
     b.movePiece(constants.WHITE_PLAYER, [4, 6, 4, 5]) 
     self.assertEqual(b._board[4][6], "")
     self.assertEqual(b._board[4][5], "*p")
开发者ID:simfarm,项目名称:chess,代码行数:11,代码来源:test.py

示例3: test_move_piece_queen_overtakes_queen

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import movePiece [as 别名]
 def test_move_piece_queen_overtakes_queen(self):
     from board import Board
     import constants
     b = Board()
     b._board      = ChessTest.board1
    
     self.assertEqual(b._board[3][3], "q")
     self.assertEqual(b._board[5][3], "*q")
     b.movePiece(constants.WHITE_PLAYER, [3, 3, 5, 3]) 
     self.assertEqual(b._board[3][3], "")
     self.assertEqual(b._board[5][3], "q")
开发者ID:simfarm,项目名称:chess,代码行数:13,代码来源:test.py

示例4: Game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import movePiece [as 别名]
class Game(object):
    def __init__(self, playerA = "Player A", playerB = "Player B"):
        self._board = Board()
        self.players = [Player(playerA, PieceColor.White), Player(playerB,
        PieceColor.Black)]
        self.playerIdTurn = PieceColor.White.value
        self.winner = ""
        self._gameOver = False
        self.totalMoves = 0

    @classmethod
    def resumeFromBoardState(cls, playerA = "Player A", playerB = "Player " \
            "B", boardState = None):
        game = cls(playerA, playerB)
        game._setBoardState(boardState)
        return game

    def _setBoardState(self, boardState):
        logger.debug('Setting board state')
        if not boardState:
            return
        if __debug__:
            pdb.set_trace()
        self._board.removeAllPiecesFromBoard()
        self._board.validateAndSetBoardState(boardState)
        # TODO: Validate and set the board state
        self.playerIdTurn = PieceColor.Black.value
        pass

    def play(self):

        while True:
            return

    def isGameOver(self):
        #Check if game is over. Return stalemate or checkmate
        return self._gameOver

    def _setGameOverWithWinner(self, winner):
        self._gameOver = True
        # Notify all observers here that the game is over

    """
    Make move first checks if it's the turn of the current player.
    Then it verifies whether the player is moving his/her own piece.

    At last it'll check if the opponent player can make any legal moves.
    If not, then the game will be declared over.
    """
    def makeMove(self, playerId, fromPosition, toPosition):
        if self.playerIdTurn != playerId:
            raise PlayerTurnError("It's not the turn of player %s"
                    %playerId)

        #TODO: Check if player is moving the correct piece color

        if self.isGameOver():
            return False

        player = self.players[playerId]
        playerColor = player.getPlayerPieceColor()
        validMove = self._board.isMovePossible(playerColor, fromPosition, toPosition)

        if not validMove:
            logger.debug('Cannot move the piece, Invalid move')
            # Construct json object and return information
            return

        # Simulate move and check if the King is checked
        self._board.movePiece(fromPosition, toPosition)

        #TODO: Check for checkmate, en passe, castling or check condition

        # Checks if the current move has caused the current player's king to be
        # checked. Reverts the move if this is the case. Also, returns false
        if self._board.isPlayerChecked(playerColor):
            self._board.undoLastMove()
            logger.debug('Cannot make this move. King is left in a check' \
                    'position')
            return False

        #Check if the opponent's King has been check mate
        opponentId = self.playerIdTurn ^ 1
        opponentPlayer = self.players[opponentId]
        opponentPlayerColor = opponentPlayer.getPlayerPieceColor()

        if self._board.isPlayerCheckMate(opponentPlayerColor):
            self._setGameOverWithWinner(player)

        if self._board.isPlayerChecked(opponentPlayerColor):
            #TODO: Return information in JSON that opponent is in check
            logger.debug('Opponent player has been checked')

        self.totalMoves += 1
        #self._board.movePiece(fromPosition, toPosition)
        self.playerIdTurn ^= 1

    def checkIfGameIsOver(self):
        for player in self.players:
            playerColor = player.getPlayerPieceColor()
#.........这里部分代码省略.........
开发者ID:sayedatifali,项目名称:django-chess,代码行数:103,代码来源:game.py

示例5: Game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import movePiece [as 别名]
class Game(object):
    def __init__(self):
        """
        Initializes new game.
        """
        #Create new game board
        self._board = Board() 

        #Record whose turn it is
        self._currentPlayer = constants.WHITE_PLAYER 

        #Print welcome text
        os.system('clear')
        welcome = "Welcome to Solidarity Bros. Chess!\n" \
          "by Chris Wang, Dmitriy Chukhin, and Jim Ladd\n" \
          "\n" \
          "To move a piece, give the starting and ending coordinates.\n" \
          "To move the white knight for example, type 'b1c3'.\n" \
          "To quit, type 'quit'.\n\n" \
          "Enjoy!\n\n"
 
        print welcome

    def play(self):
        """
        Main game loop.
        """
        #Print initial board
        self._board.printBoard()

        while True:
            self._nextTurn()

    def _nextTurn(self):
        """
        Contains logic for executing a player's turn.
        Exits when game is finished.
        """
        #Get next move from player
        move = self._getPlayersNextMove()
        move = self._board._moveConverter(move)
        while self._board.isLegalMove(self._currentPlayer, move) != True:
            move = self._getPlayersNextMove()
            move = self._board._moveConverter(move)

        #Executes move
        self._board.movePiece(self._currentPlayer, move)

        #Prints board
        os.system('clear')
        self._board.printBoard()

        #Creates variable other_player
        if self._currentPlayer == constants.WHITE_PLAYER:
            self._otherPlayer = constants.BLACK_PLAYER
        else:
            self._otherPlayer = constants.WHITE_PLAYER
        
        #End game conditions
        if board_analyzer.isCheckMate(self._board, self._otherPlayer) == True:
            print "Player",self._currentPlayer,"has won the game!"
            choice = None
            while choice != 'quit':
                choice = raw_input("Type 'quit' to exit. ")
            else:
                sys.exit(0)
                                      
        #Switches players
        if self._currentPlayer == constants.WHITE_PLAYER:
            self._currentPlayer = constants.BLACK_PLAYER
        else: 
            self._currentPlayer = constants.WHITE_PLAYER
            
    def _getPlayersNextMove(self):
        """
        Retrieves player's next move. (e.g. "b1d4").
        Ensures that move is well-formed.

        @return: Player's next move (e.g. "b1d4")
        """
        while True:
            playerColor = ""
            if self._currentPlayer == constants.WHITE_PLAYER:
                playerColor = "White"
            else:
                playerColor = "Black"
            prompt = "%s> " % playerColor
            response = raw_input(prompt)

            #Check response length
            if len(response) != 4:
                self._printHelp()
                continue

            #Does user want to quit?
            if response.lower() == 'quit':
                sys.exit(0)

            #Check row/column values 
            validColumns = "abcdefgh"
#.........这里部分代码省略.........
开发者ID:simfarm,项目名称:chess,代码行数:103,代码来源:game.py


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