Esempio n. 1
0
 def test_all_moves(self):
     """Test of all_moves() method."""
     assert sorted(PieceMoves.all_moves(self.game)) == [
         Move(Position(0, 0), Position(1, 0)),
         Move(Position(0, 0), Position(2, 0)),
         Move(Position(0, 0), Position(3, 0)),
         Move(Position(0, 1), Position(0, 2)),
         Move(Position(0, 1), Position(0, 3)),
         *sorted(PieceMoves.king_moves(Position(4, 0), self.game)),
         Move(Position(4, 4), Position(3, 5)),
         Move(Position(4, 4), Position(4, 5)),
         Move(Position(7, 0), Position(5, 0)),
         Move(Position(7, 0), Position(6, 0)),
         Move(Position(7, 1), Position(7, 2)),
         Move(Position(7, 1), Position(7, 3)),
     ]
Esempio n. 2
0
    def test_pawn_moves(self):
        """Test of pawn_moves() method."""

        assert sorted(PieceMoves.pawn_moves(Position(4, 4), self.game)) == [
            Move(Position(4, 4), Position(3, 5)),
            Move(Position(4, 4), Position(4, 5)),
        ]
Esempio n. 3
0
    def is_mate(game: Game):
        """Check if <game.turn> side got mate.
        mate = check without possibility to defend own king
        """

        if GameLogic.is_check(game.board, game.turn):
            for move in PieceMoves.all_moves(game):
                if GameLogic.is_move_possible(game, move):
                    return False
            return True
        return False
Esempio n. 4
0
    def test_king_moves(self):
        """Test of king_moves() method."""

        assert sorted(PieceMoves.king_moves(Position(4, 0), self.game)) == [
            Move(Position(4, 0), Position(3, 0)),
            Move(Position(4, 0), Position(3, 1)),
            Move(Position(4, 0), Position(4, 1)),
            Move(Position(4, 0), Position(5, 0)),
            Move(Position(4, 0), Position(5, 1)),
            Move(Position(4, 0), Position(6, 0)),
        ]
Esempio n. 5
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. 6
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))]
Esempio n. 7
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))]
Esempio n. 8
0
    def test_is_piece_touched(self):
        """Test of is_piece_touched() method."""

        assert PieceMoves.is_piece_touched(Position(4, 4), self.game)
        assert PieceMoves.is_piece_touched(Position(3, 4), self.game)