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 needs work - 'quit' will exit below loop ### And not exit game #End game if king is in check-mate if board_analyzer.isCheckMate(self._board, self._otherPlayer) == True: #Game end conditions print "Congradulations! Player", self._currentPlayer, "has won!" choice = None while choice != 'quit': choice = raw_input("Enter 'quit' to exit. ") #Warn if king is in check if board_analyzer.isCheck(self._board, self._otherPlayer) == True: print "Player", self._otherPlayer, "is in check!" #Switches players if self._currentPlayer == constants.WHITE_PLAYER: self._currentPlayer = constants.BLACK_PLAYER else: self._currentPlayer = constants.WHITE_PLAYER
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 needs work - 'quit' will exit below loop ### And not exit game # End game if king is in check-mate if board_analyzer.isCheckMate(self._board, self._otherPlayer) == True: # Game end conditions print "Congradulations! Player", self._currentPlayer, "has won!" choice = None while choice != "quit": choice = raw_input("Enter 'quit' to exit. ") # Warn if king is in check if board_analyzer.isCheck(self._board, self._otherPlayer) == True: print "Player", self._otherPlayer, "is in check!" # Switches players if self._currentPlayer == constants.WHITE_PLAYER: self._currentPlayer = constants.BLACK_PLAYER else: self._currentPlayer = constants.WHITE_PLAYER
def isLegalMove(self, currentPlayer, move): """ Determines if a move is legal or not. A move is not legal if any of the following is true: a) piece is not actually moved (e.g. 'a5a5') b) move refers to empty space c) the game piece is not owned by the current player d) a game piece owned by the same player is at the destination e) the move is not legal for the game piece f) not moving into check @precondition: Method presumes that move is well-formed. (e.g. row and column values are correct) @param move: Four letter combination representing move. (e.g. "b3c4") @return: True if move is legal, False otherwise. """ #a)Tests for whether a piece is not actually moved (e.g. 'a5a5') if move[0:2] == move[2:4]: print "Doesn't work because no piece moved." return False else: validity = True #b)Tests if there is a piece in the targeted space if self._board[move[0]][move[1]] == constants.EMPTY_SYMBOL: print "No piece there." return False else: validity = True #c)Tests if the game piece is not owned by the current player if currentPlayer != self.pieceOwner([move[0], move[1]]): print "Piece not owned by player" return False else: validity = True #d)Tests if game piece owned by the current player occupies end destination if currentPlayer == self.pieceOwner([move[2], move[3]]): print "Endpoint space has piece currently owned by player" return False else: validity = True #e)Tests whether the move is legal for a specific piece if constants.ROOK_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForRook(move) == True: validity = True else: return False if constants.KNIGHT_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForKnight(move) == True: validity = True else: return False if constants.BISHOP_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForBishop(move) == True: validity = True else: return False if constants.QUEEN_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForQueen(move) == True: validity = True else: return False if constants.KING_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForKing(move) == True: validity = True else: return False if constants.PAWN_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForPawn(move) == True: validity = True else: return False #f)Tests whether current player's move will move current player in check self._testBoard = self._board testPiece = self._board[move[0]][move[1]] self._testBoard[move[2]][move[3]] = testPiece if board_analyzer.isCheck(self._testBoard, currentPlayer) == True: print "Cannot move into check." return False else: validity = True return validity
def isLegalMove(self, currentPlayer, move): """ Determines if a move is legal or not. A move is not legal if any of the following is true: a) piece is not actually moved (e.g. 'a5a5') b) move refers to empty space c) the game piece is not owned by the current player d) a game piece owned by the same player is at the destination e) the move is not legal for the game piece f) not moving into check @precondition: Method presumes that move is well-formed. (e.g. row and column values are correct). @param currentPlayer: "1" for white, "2" for black. @param move: Four character combination representing move (e.g. [1, 2, 1, 4]). @return: True if move is legal, False otherwise. """ #Tests for whether a piece is not actually moved if move[0:2] == move[2:4]: return False else: validity = True #Tests if there is a piece in the targeted space if self._board[move[0]][move[1]] == constants.EMPTY_SYMBOL: return False else: validity = True #Tests if the game piece is not owned by the current player if currentPlayer != self.pieceOwner([move[0], move[1]]): return False else: validity = True #Tests if game piece owned by the current player occupies end destination if currentPlayer == self.pieceOwner([move[2], move[3]]): return False else: validity = True #Tests whether the move is legal for a specific piece if constants.PAWN_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForPawn(move, currentPlayer) == True: validity = True else: return False if constants.ROOK_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForRook(move) == True: validity = True else: return False if constants.KNIGHT_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForKnight(move) == True: validity = True else: return False if constants.BISHOP_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForBishop(move) == True: validity = True else: return False if constants.QUEEN_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForQueen(move) == True: validity = True else: return False if constants.KING_SYMBOL in self._board[move[0]][move[1]]: if self._isLegalMoveForKing(move) == True: validity = True else: return False #Tests whether current player's move will move current player in check if board_analyzer.isCheck(self._board, currentPlayer, move) == True: return False else: validity = True return validity