Пример #1
0
    def test_pawn_black_capture_promo(self):
        """Tests that a pawn that is about to promote can do so via capture."""
        board, white, black = create_board_and_players()
        pawn_black = Pawn(black, [3, 6])  # d2
        board.add_to_board(pawn_black)
        rook_white1 = Rook(white, [2, 7])  # c1
        board.add_to_board(rook_white1)
        rook_white2 = Rook(white, [4, 7])  # e1
        board.add_to_board(rook_white2)
        king_white = King(white, [3, 7])  # d1
        board.add_to_board(king_white)

        correct_move1 = [2, "Q"]  # c8
        correct_move2 = [2, "N"]  # c8
        correct_move3 = [2, "B"]  # c8
        correct_move4 = [2, "R"]  # c8
        correct_move5 = [4, "Q"]  # e8
        correct_move6 = [4, "N"]  # e8
        correct_move7 = [4, "B"]  # e8
        correct_move8 = [4, "R"]  # e8

        returned_moves = pawn_black.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 8)
        self.assertTrue(correct_move1 in returned_moves)
        self.assertTrue(correct_move2 in returned_moves)
        self.assertTrue(correct_move3 in returned_moves)
        self.assertTrue(correct_move4 in returned_moves)
        self.assertTrue(correct_move5 in returned_moves)
        self.assertTrue(correct_move6 in returned_moves)
        self.assertTrue(correct_move7 in returned_moves)
        self.assertTrue(correct_move8 in returned_moves)
Пример #2
0
 def test_Rook(self):
     self.assertEqual(str(Rook(1, 1, WHITE, None)), 'Ra1')
     self.assertEqual(str(Rook(1, 1, BLACK, None)), 'ra1')
     cases = [
         ('Re4', [
             'e1', 'e2', 'e3', 'e5', 'e6', 'e7', 'e8', 'a4', 'b4', 'c4',
             'd4', 'f4', 'g4', 'h4'
         ]),
         ('Re4,Bc4,Ke1,pe6,bf4', ['e3', 'e2', 'e5', 'e6', 'd4', 'f4']),
     ]
     self.check_cases(WHITE, ROOK, cases)
     self.check_cases_visible(WHITE, ROOK, cases)
Пример #3
0
    def test_path_impeded(self):
        """Verifies that a rook's returned moves stops when encountering
        another of the rook's owner's pieces."""
        board, white = Board(), Player(Color.W)
        rook_white = Rook(white, [3, 4])  # d4
        board.add_to_board(rook_white)

        pawn_white = Pawn(white, [2, 4])  # c4
        board.add_to_board(pawn_white)

        incorrect_move1 = [2, 4]  # c4

        returned_moves = rook_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 11)
        self.assertTrue(incorrect_move1 not in returned_moves)
Пример #4
0
    def test_capture(self):
        """Verifies that a rook's returned move list correctly includes
        a capture opportunity."""
        board, white, black = create_board_and_players()
        rook_white = Rook(white, [3, 4])  # d4
        board.add_to_board(rook_white)

        pawn_black = Pawn(black, [2, 4])  # c4
        board.add_to_board(pawn_black)

        correct_move1 = [2, 4]  # c4

        returned_moves = rook_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 12)
        self.assertTrue(correct_move1 in returned_moves)
Пример #5
0
    def test_get_all_legal_moves_check(self):
        """Assures that get_all_legal_moves won't return a move that puts
        the owner in check."""
        board, white, black = create_board_and_players()
        pawn_pos_white = [4, 6]  # e2
        pawn_white = Pawn(white, pawn_pos_white)
        board.add_to_board(pawn_white)

        king_pos_white = [5, 6]
        king_white = King(white, king_pos_white)
        board.add_to_board(king_white)

        found_a_move = False
        for move in board.get_all_legal_moves(white):
            if move[0] == pawn_white:
                found_a_move = True
        self.assertTrue(found_a_move)

        rook_pos_black = [1, 6]
        rook_black = Rook(black, rook_pos_black)
        board.add_to_board(rook_black)

        found_a_move = False
        for move in board.get_all_legal_moves(white):
            if move[0] == pawn_white:
                found_a_move = True
        self.assertFalse(found_a_move)
Пример #6
0
    def test_simple(self):
        """Verifies that a rook's returned move list has the correct
        number of moves and that the boundaries of it's movelist is correct."""
        board, white = Board(), Player(Color.W)
        rook_white = Rook(white, [3, 4])  # d4
        board.add_to_board(rook_white)

        correct_move1 = [0, 4]  # a4
        correct_move2 = [7, 4]  # h4
        correct_move3 = [3, 0]  # d8
        correct_move4 = [3, 7]  # d1

        returned_moves = rook_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 14)
        self.assertTrue(correct_move1 in returned_moves)
        self.assertTrue(correct_move2 in returned_moves)
        self.assertTrue(correct_move3 in returned_moves)
        self.assertTrue(correct_move4 in returned_moves)
Пример #7
0
    def test_castling_to_check(self):
        """Ensures that castling is not returned in the King's moved
        list if the King would be castling into check."""
        board, white, black = Board(), Player(Color.W), Player(Color.B)
        king_white = King(white, [4, 7])  # e1
        board.add_to_board(king_white)

        rook_white = Rook(white, [7, 7])  # h1
        board.add_to_board(rook_white)

        castle_move = [6, 7]  # g1

        returned_moves = king_white.get_legal_moves(board, True)
        self.assertTrue(castle_move in returned_moves)

        rook_black = Rook(black, [6, 0])  # g7
        board.add_to_board(rook_black)
        returned_moves = king_white.get_legal_moves(board, True)
        self.assertFalse(castle_move in returned_moves)
Пример #8
0
    def test_get_all_legal_moves_simple(self):
        """Simply verify that getting the legal moves of two pieces
        sum to the returned value of get_all_legal_moves."""
        board, white = Board(), Player(Color.W)
        pos_white = [4, 6]  # e2
        pawn_white = Pawn(white, pos_white)
        board.add_to_board(pawn_white)

        pos_white2 = [3, 3]  # d5
        rook_white = Rook(white, pos_white2)
        board.add_to_board(rook_white)

        combined_positions = []
        for move in pawn_white.get_legal_moves(board, True):
            combined_positions += [pawn_white, move],
        for move in rook_white.get_legal_moves(board, True):
            combined_positions += [rook_white, move],

        self.assertEqual(board.get_all_legal_moves(white), combined_positions)
Пример #9
0
    def test_is_attacked(self):
        """Assure that all positions attacked by an enemy piece return True."""
        board, white, black = create_board_and_players()
        pos_white = [4, 6]  # e2
        rook_white = Rook(white, pos_white)

        board.add_to_board(rook_white)
        self.assertTrue(board.is_attacked([3, 6], black))
        self.assertTrue(board.is_attacked([4, 7], black))
        self.assertFalse(board.is_attacked([5, 5], black))
        self.assertFalse(board.is_attacked([4, 7], white))
Пример #10
0
    def test_make_move_simple(self):
        """Makes a simple move, verifies values are appropriately updated."""
        board, white = Board(), Player(Color.W)
        rook_white = Rook(white, [3, 2])  # d6
        board.add_to_board(rook_white)

        board.make_move(rook_white, [5, 2])  # f6
        # ensure make_move updated piece position
        self.assertTrue(rook_white.position == [5, 2])
        # ensure make_move updated board position
        self.assertTrue(board.get_piece_at_position([5, 2]) is rook_white)
        self.assertTrue(board.get_piece_at_position([3, 2]) is None)
Пример #11
0
    def test_get_all_legal_moves_cm(self):
        """Assures that get_all_legal_moves returns no moves if the player
        is in checkmate."""
        board, white, black = create_board_and_players()
        pawn_pos_white = [4, 6]  # e2
        pawn_white = Pawn(white, pawn_pos_white)
        board.add_to_board(pawn_white)

        king_pos_white = [7, 6]
        king_white = King(white, king_pos_white)
        board.add_to_board(king_white)

        rook_pos_black1 = [7, 0]
        rook_black1 = Rook(black, rook_pos_black1)
        board.add_to_board(rook_black1)

        rook_pos_black2 = [6, 0]
        rook_black2 = Rook(black, rook_pos_black2)
        board.add_to_board(rook_black2)

        self.assertTrue(board.get_all_legal_moves(white) == [])
Пример #12
0
    def test_castling_normal(self):
        """Ensures that castling is properly returned in the King's moved
        list."""
        board, white = Board(), Player(Color.W)
        king_white = King(white, [4, 7])  # e1
        board.add_to_board(king_white)

        rook_white = Rook(white, [7, 7])  # h1
        board.add_to_board(rook_white)

        castle_move = [6, 7]  # g1

        returned_moves = king_white.get_legal_moves(board, True)
        self.assertTrue(castle_move in returned_moves)
Пример #13
0
    def test_castling_already_moved(self):
        """Ensures that castling is not returned in the King's moved
        list if the King has already moved."""
        board, white = Board(), Player(Color.W)
        king_white = King(white, [4, 7])  # e1
        king_white.first_move = False
        board.add_to_board(king_white)

        rook_white = Rook(white, [7, 7])  # h1
        board.add_to_board(rook_white)

        castle_move = [6, 7]  # g1

        returned_moves = king_white.get_legal_moves(board, True)
        self.assertFalse(castle_move in returned_moves)
Пример #14
0
    def test_make_move_castle_kingside(self):
        """Ensures kingside castling moving works correctly."""
        board, white = Board(), Player(Color.W)
        king_pos_white = [4, 7]  # e1
        rook_pos_white = [7, 7]  # h1
        king_white = King(white, king_pos_white)
        rook_white = Rook(white, rook_pos_white)
        board.add_to_board(king_white)
        board.add_to_board(rook_white)

        board.make_move(king_white, [6, 7])  # castle to g1
        self.assertTrue(board.get_piece_at_position([6, 7]) is king_white)
        self.assertTrue(board.get_piece_at_position([5, 7]) is rook_white)
        self.assertTrue(board.get_piece_at_position([4, 7]) is None)
        self.assertTrue(board.get_piece_at_position([7, 7]) is None)
Пример #15
0
    def test_determine_check(self):
        """Creates two check situations and verifies that is_in_check returns
        True in both situations."""
        # Check 1:
        board, white, black = create_board_and_players()

        pos_rook_black = [4, 0]  # e8
        rook_black = Rook(black, pos_rook_black)

        pos_king_white = [4, 7]  # e1
        king_white = King(white, pos_king_white)

        board.add_to_board(rook_black)
        board.add_to_board(king_white)

        self.assertTrue(board.is_in_check(white))

        # Add a separation pawn, should no longer be in check

        pos_pawn_black = [4, 3]  # e5
        pawn_black = Pawn(black, pos_pawn_black)
        board.add_to_board(pawn_black)
        self.assertFalse(board.is_in_check(white))

        # Check 2:
        board, white, black = create_board_and_players()

        pos_bishop_black = [1, 0]  # b8
        bishop_black = Bishop(black, pos_bishop_black)

        pos_king_white = [5, 4]  # f4
        king_white = King(white, pos_king_white)

        board.add_to_board(bishop_black)
        board.add_to_board(king_white)

        self.assertTrue(board.is_in_check(white))

        # Add a separation pawn, should no longer be in check

        pos_pawn_black = [3, 2]  # d6
        pawn_black = Pawn(black, pos_pawn_black)
        board.add_to_board(pawn_black)
        self.assertFalse(board.is_in_check(white))
Пример #16
0
    def test_filter_checks(self):
        """Assure that all moves that lead to check for the player are
        appropriately filtered out of the move list."""
        board, white, black = create_board_and_players()
        pawn_pos_white = [5, 3]  # f5
        pawn_white = Pawn(white, pawn_pos_white)
        pawn_white.first_move = False
        board.add_to_board(pawn_white)

        king_pos_white = [7, 3]  # h5
        king_white = King(white, king_pos_white)
        board.add_to_board(king_white)

        pawn_moves = pawn_white.get_legal_moves(board, False)
        filtered_pawn_moves = pawn_white.filter_checks(pawn_moves, board)
        self.assertEqual(pawn_moves, filtered_pawn_moves)

        rook_pos_black = [1, 3]  # a1
        rook_black = Rook(black, rook_pos_black)
        board.add_to_board(rook_black)

        pawn_moves = pawn_white.get_legal_moves(board, False)
        filtered_pawn_moves = pawn_white.filter_checks(pawn_moves, board)
        self.assertEqual(len(filtered_pawn_moves), 0)