Example #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)
Example #2
0
 def test_pawn_black_path_impeded(self):
     """Tests that no moves are returned if the pawn's path is impeded."""
     board, white, black = create_board_and_players()
     pawn_white = Pawn(white, [3, 6])  # d2
     board.add_to_board(pawn_white)
     pawn_black = Pawn(black, [3, 5])  # d3
     board.add_to_board(pawn_black)
     returned_moves = pawn_black.get_legal_moves(board, True)
     self.assertTrue(len(returned_moves) == 0)  
Example #3
0
    def test_pawn_black_simple(self):
        """Tests that a simple forward move is in the pawn's move list."""
        board, black = Board(), Player(Color.B)
        pawn_black = Pawn(black, [3, 4])  # d4
        pawn_black.first_move = False
        board.add_to_board(pawn_black)

        correct_move = [3, 5]  # d3

        returned_moves = pawn_black.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 1)
        self.assertTrue(correct_move in returned_moves)
Example #4
0
    def test_pawn_white_simple(self):
        """Tests that a simple forward move is in the pawn's move list."""
        board, white = Board(), Player(Color.W)
        pawn_white = Pawn(white, [3, 4])  # d4
        pawn_white.first_move = False
        board.add_to_board(pawn_white)

        correct_move = [3, 3]  # d5

        returned_moves = pawn_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 1)
        self.assertTrue(correct_move in returned_moves)
Example #5
0
    def test_pawn_black_first_move(self):
        """Tests that two moves are available to a new unimpeded pawn."""
        board, black = Board(), Player(Color.B)
        pawn_black = Pawn(black, [3, 1])  # d7
        board.add_to_board(pawn_black)

        correct_move1 = [3, 2]  # d6
        correct_move2 = [3, 3]  # d5

        returned_moves = pawn_black.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 2)
        self.assertTrue(correct_move1 in returned_moves)
        self.assertTrue(correct_move2 in returned_moves)
Example #6
0
    def test_pawn_white_first_move(self):
        """Tests that two moves are available to a new unimpeded pawn."""
        board, white = Board(), Player(Color.W)
        pawn_white = Pawn(white, [3, 6])  # d2
        board.add_to_board(pawn_white)

        correct_move1 = [3, 5]  # d3
        correct_move2 = [3, 4]  # d4

        returned_moves = pawn_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 2)
        self.assertTrue(correct_move1 in returned_moves)
        self.assertTrue(correct_move2 in returned_moves)
Example #7
0
    def test_pawn_black_promotion(self):
        """Tests that the four promotion moves are available to a promoting
         pawn."""
        board, black = Board(), Player(Color.B)
        pawn_black = Pawn(black, [3, 6])  # d2
        board.add_to_board(pawn_black)

        correct_move1 = [3, "Q"]  # d1
        correct_move2 = [3, "N"]  # d1
        correct_move3 = [3, "B"]  # d1
        correct_move4 = [3, "R"]  # d1

        returned_moves = pawn_black.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 4)
        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)
Example #8
0
    def test_pawn_white_promotion(self):
        """Tests that the four promotion moves are available to a promoting
         pawn."""
        board, white = Board(), Player(Color.W)
        pawn_white = Pawn(white, [3, 1])  # d7
        board.add_to_board(pawn_white)

        correct_move1 = [3, "Q"]  # d8
        correct_move2 = [3, "N"]  # d8
        correct_move3 = [3, "B"]  # d8
        correct_move4 = [3, "R"]  # d8

        returned_moves = pawn_white.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 4)
        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)
Example #9
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)
Example #10
0
    def test_pawn_black_en_passant(self):
        """Tests that en passant opportunities are returned correctly."""
        board, white, black = create_board_and_players()
        pawn_black = Pawn(black, [3, 4])  # d4
        pawn_black.first_move = False
        board.add_to_board(pawn_black)
        pawn_white = Pawn(white, [2, 6])  # c2
        board.add_to_board(pawn_white)

        # simulate a move to c4, creating en passant opportunity.
        board.make_move(pawn_white, [2, 4])

        correct_move1 = [3, 5]  # d3
        correct_move2 = [2, 5]  # c3, en passant capture

        returned_moves = pawn_black.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 2)

        self.assertTrue(correct_move1 in returned_moves)
        self.assertTrue(correct_move2 in returned_moves)
Example #11
0
    def test_pawn_black_capture(self):
        """Tests that pawn capture moves are returned correctly."""
        board, white, black = create_board_and_players()
        pawn_black = Pawn(black, [3, 4])  # d4
        pawn_black.first_move = False
        board.add_to_board(pawn_black)
        pawn_white1 = Pawn(white, [2, 5])  # c3
        board.add_to_board(pawn_white1)
        pawn_white2 = Pawn(white, [4, 5])  # e3
        board.add_to_board(pawn_white2)

        correct_move1 = [3, 5]  # d3
        correct_move2 = [2, 5]  # c3
        correct_move3 = [4, 5]  # e3

        returned_moves = pawn_black.get_legal_moves(board, True)
        self.assertTrue(len(returned_moves) == 3)

        self.assertTrue(correct_move1 in returned_moves)
        self.assertTrue(correct_move2 in returned_moves)
        self.assertTrue(correct_move3 in returned_moves)
Example #12
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)