Esempio n. 1
0
    def make_move(move: Move, game: Game) -> Game:
        """Make move.

        Attention: no checking of check after move. Technically move can be not valid!!!
        """

        # Copy game (pass by value)
        game = copy.deepcopy(game)
        # Retrieve piece at start position
        piece = game.board.get_piece(move.start)
        # Get possible moves
        possible_moves = PieceMoves.moves(piece.type, move.start, game)
        # Check if move satisfies
        if move in possible_moves:
            # Check if castling occurs
            if move in PieceMoves.castling_moves(move.start, game):
                game.board.set_piece(
                    Position(int((move.finish.x + move.start.x) / 2),
                             move.start.y),
                    Piece(PieceType.ROOK, game.turn),
                )
                # Short castling
                if move.finish.x - move.start.x > 0:
                    game.board.remove_piece(Position(7, move.start.y))
                # Long castling
                else:
                    game.board.remove_piece(Position(0, move.start.y))
            # Check if en passant occurs
            if move in PieceMoves.en_passant_moves(move.start, game):
                game.board.remove_piece(Position(move.finish.x, move.start.y))
            # Update board
            game.board.set_piece(move.finish, piece)
            game.board.remove_piece(move.start)
            # Update history
            game.history_moves.append(move)
        return Game(game.board, Colour.change_colour(game.turn),
                    game.history_moves)
Esempio n. 2
0
    def test_castling_moves(self):
        """Test of castling_moves() method."""

        assert sorted(PieceMoves.castling_moves(Position(
            4, 0), self.game)) == [Move(Position(4, 0), Position(6, 0))]