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_en_passant_moves(self):
        """Test of en_passant_moves() method."""

        assert sorted(PieceMoves.en_passant_moves(Position(
            4, 4), self.game)) == [Move(Position(4, 4), Position(3, 5))]