Ejemplo n.º 1
0
 def test_King(self):
     self.assertEqual(str(King(1, 1, WHITE, None)), 'Ka1')
     self.assertEqual(str(King(1, 1, BLACK, None)), 'ka1')
     cases = [
         ('Ke4', ['d5', 'e5', 'f5', 'f4', 'f3', 'e3', 'd3', 'd4']),
         ('Ke4,Pd3,Rg2,nd5,re6', ['d5', 'e5', 'f5', 'f4', 'f3', 'e3',
                                  'd4']),
         ('Ke1,Ra1,Rh1', ['c1', 'd1', 'd2', 'e2', 'f2', 'f1', 'g1']),
         ('Ke1,Ra1,Rh1,nb1', ['d1', 'd2', 'e2', 'f2', 'f1', 'g1']),
     ]
     self.check_cases(WHITE, KING, cases)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    def test_path_impeded(self):
        """Verifies that a bishop's returned moves stops when encountering
        another of the bishop's owner's pieces."""
        board, white = Board(), Player(Color.W)
        king_white = King(white, [3, 4])  # d4
        board.add_to_board(king_white)

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

        incorrect_move1 = [2, 4]  # c4

        returned_moves = king_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 7)
        self.assertFalse(incorrect_move1 in returned_moves)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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))
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
    def test_simple(self):
        """Verifies that a king's returned movelist has the correct
        number of moves and that the boundaries of it's movelist is correct."""
        board, white = Board(), Player(Color.W)
        king_white = King(white, [3, 4])  # d4
        board.add_to_board(king_white)

        correct_move_list = [[3, 5]]  # d3
        correct_move_list += [2, 5],  # c3
        correct_move_list += [4, 5],  # e3
        correct_move_list += [4, 4],  # e4
        correct_move_list += [2, 4],  # c4
        correct_move_list += [4, 3],  # e5
        correct_move_list += [3, 3],  # d5
        correct_move_list += [2, 3],  # c5

        returned_moves = king_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 8)
        for move in correct_move_list:
            self.assertTrue(move in returned_moves)
Ejemplo n.º 10
0
    def test_capture(self):
        """Verifies that a king's returned movelist correctly includes
        a capture opportunity."""
        board, white, black = create_board_and_players()
        king_white = King(white, [3, 4])  # d4
        board.add_to_board(king_white)

        pawn_black = Pawn(black, [2, 3])  # c5
        board.add_to_board(pawn_black)

        correct_move_list = [[3, 5]]  # d3
        correct_move_list += [2, 5],  # c3
        correct_move_list += [4, 5],  # e3
        correct_move_list += [4, 4],  # e4
        correct_move_list += [2, 4],  # c4
        correct_move_list += [4, 3],  # e5
        correct_move_list += [3, 3],  # d5
        correct_move_list += [2, 3],  # c5

        returned_moves = king_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 8)
        for move in correct_move_list:
            self.assertTrue(move in returned_moves)
Ejemplo n.º 11
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)
Ejemplo n.º 12
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) == [])
Ejemplo n.º 13
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)