def test_ordering_heuristic_in_gen_all_moves(self): """Similar to test case above, but makes sure that sorting by key works as expected.""" board = load_board([ [" ", "", "", "k", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", "K", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], ["R", "", "", " ", "", "", "", ""] ]) all_moves = gen_all_moves(board, WHITE) def _score_move(move): return score_move(board, move) all_moves_sorted = sorted(all_moves, key=_score_move, reverse=True) win_move_index = None idle_move_index = None for i, move in enumerate(all_moves_sorted): if move.piece == ROOK and index_to_sq(move.src) == "a1" and index_to_sq(move.dest) == "a8": win_move_index = i elif move.piece == ROOK and index_to_sq(move.src) == "a1" and index_to_sq(move.dest) == "a7": idle_move_index = i assert win_move_index < idle_move_index
def test_promotions_pawn_white_move(self): board = load_board([[" ", "", "", "", " ", "k", "", "K"], ["P", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "]]) assert sorted(get_promotions(board, sq_to_index("a7"), sq_to_index("a8"))) == \ sorted(["Q", "R", "B", "N"])
def test_promotions_pawn_black_move(self): board = load_board([[" ", "", "", "", " ", "k", "", "K"], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], ["p", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "]]) assert sorted(get_promotions(board, sq_to_index("a2"), sq_to_index("a1"))) == \ sorted(["q", "r", "b", "n"])
def test_pawn_give_check(self): board = load_board([[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', 'k', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', 'P', ' ', ' '], [' ', ' ', ' ', ' ', 'K', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']]) assert is_in_check(board, BLACK) assert not is_in_check(board, WHITE)
def test_promotions_pawn_forward_two_spaces(self): board = load_board([[" ", "", "", "", " ", "k", "", "K"], [" ", "", "", "", " ", " ", "", " "], ["P", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "]]) assert get_promotions(board, sq_to_index("a6"), sq_to_index("a8")) == []
def test_promotions_pawn_backwards(self): board = load_board([[" ", "", "", "", " ", "k", "", "K"], ["p", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "]]) assert get_promotions(board, sq_to_index("a7"), sq_to_index("a8")) == []
def test_is_in_checkmate_unprotected_attacker(self): board = load_board([ ["k", "", "", "", "", "", "", ""], ["", "Q", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "K", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) assert not is_in_checkmate(board, BLACK)
def test_promotions_not_pawn(self): board = load_board([[" ", "", "", "", "", "k", "", "K"], ["R", "", "", "", "", " ", "", " "], [" ", "", "", "", "", " ", "", " "], [" ", "", "", "", "", " ", "", " "], [" ", "", "", "", "", " ", "", " "], [" ", "", "", "", "", " ", "", " "], [" ", "", "", "", "", " ", "", " "], [" ", "", "", "", "", " ", "", " "]]) board = Board() assert get_promotions(board, sq_to_index("a7"), sq_to_index("a8")) == []
def test_is_in_check_basic_bishop(self): board = load_board([ ["B", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "k", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "K", ""], ["", "", "", "", "", "", "", ""], ]) assert is_in_check(board, BLACK) assert not is_in_check(board, WHITE)
def test_promotions_blocked(self): board = load_board([[" ", " ", "", "", " ", " ", "", " "], [" ", " ", "", "", " ", " ", "", " "], [" ", " ", "", "", " ", " ", "", " "], [" ", " ", "", "", " ", " ", "", " "], [" ", " ", "", "", " ", " ", "", " "], ["k", " ", "", "", " ", " ", "", " "], ["p", " ", "", "", " ", " ", "", " "], ["K", " ", "", "", " ", " ", "", " "]]) assert get_promotions(board, sq_to_index("a2"), sq_to_index("a1")) == [] assert get_promotions(board, sq_to_index("a2"), sq_to_index("b1")) == []
def test_is_in_stalemate_with_rook(self): board = load_board([ ["k", "", "", "", "", "", "", ""], ["", "R", "", "", "", "", "", ""], ["", "", "K", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) assert is_in_stalemate(board, BLACK) assert not is_in_checkmate(board, BLACK)
def test_is_in_checkmate_simple_rook_check(self): board = load_board([ ["", "R", "", "", "k", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "K", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) assert not is_in_checkmate(board, BLACK) assert not is_in_checkmate(board, WHITE)
def test_load_board(): sample_board = [ ["r", "n", "b", "q", "k", "b", "n", "r"], ["p", "p", "p", "p", "p", "p", "p", "p"], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["P", "P", "P", "P", "P", "P", "P", "P"], ["R", "N", "B", "Q", "K", "B", "N", "R"], ] # comparison by value assert dump_board(load_board(sample_board)) == dump_board(starter_board)
def test_rook_blocked_own_piece_valid_squares(self): board = load_board([ ["", "R", "", "Q", "", "", "", ""], ["", "N", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) rook_squares = sorted([sq_to_index(sq) for sq in ["a8", "c8"]]) assert sorted(get_rook_valid_squares( board, sq_to_index("b8"))) == rook_squares
def test_simple_board_no_mate_in_1(self): board = load_board([ ["", "", "", "k", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "K", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "R", "", "", "", "", ""], ]) mate_result = find_mate_in_n(board, WHITE, 1) assert mate_result[0] == 0 assert len(mate_result[1]) == 1
def test_simple_forced_rook_mate_in_2(self): board = load_board([ ["", "k", "", "", "", "", "", ""], ["", "", "", "", "Q", "", "", ""], ["", "", "K", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) mate_result = find_mate_in_n(board, WHITE, 2) assert mate_result[0] == CHECKMATE
def test_rook_valid_squares_capture_blocks_own_piece(self): board = load_board([ ["R", "", "", "", "", "k", "", "K"], ["n", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) rook_squares = sorted( [sq_to_index(sq) for sq in ["a7", "b8", "c8", "d8", "e8", "f8"]]) assert sorted(get_rook_valid_squares( board, sq_to_index("a8"))) == rook_squares
def test_rook_blocked_capture_valid_squares(self): board = load_board([ ["n", "R", "", "q", "", "", "", ""], ["", "r", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) rook_squares = sorted( [sq_to_index(sq) for sq in ["a8", "b7", "c8", "d8"]]) assert sorted(get_rook_valid_squares( board, sq_to_index("b8"))) == rook_squares
def test_ordering_heuristic_simple(self): board = load_board([ [" ", "", "", "k", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", "K", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], [" ", "", "", " ", "", "", "", ""], ["R", "", "", " ", "", "", "", ""], ]) m1 = Move(board[sq_to_index("a1")], sq_to_index("a1"), sq_to_index("a8")) winning_move_score = score_move(board, m1) m2 = Move(board[sq_to_index("a1")], sq_to_index("a1"), sq_to_index("a7")) idle_move_score = score_move(board, m2) assert winning_move_score > idle_move_score
def test_not_lose_immediately(self): """In any position, the opponent should choose the move which does not lose immediately.""" # black to move. choose the option where black is not mated board = load_board([ [" ", " ", "", " ", "k", "", "", ""], [" ", "Q", "", " ", " ", "", "", ""], [" ", " ", "", "K", " ", "", "", ""], [" ", " ", "", " ", " ", "", "", ""], [" ", " ", "", " ", " ", "", "", ""], [" ", " ", "", " ", " ", "", "", ""], [" ", " ", "", " ", " ", "", "", ""], [" ", " ", "", " ", " ", "", "", ""] ]) _, move_list = dls_minimax(board, 2, MIN) assert len(move_list) == 2 assert move_list[0].piece == "k" assert move_list[0].src == sq_to_index("e8") assert move_list[0].dest == sq_to_index("f8")
def test_mate_in_1_p1(self): board = load_board([ [""] * 8, [""] * 8, ["", "", "", "B", "", "r", "p", ""], ["", "", "P", "", "", "k", "b", ""], [""] * 7 + ["p"], ["", "p", "", "K", "", "", "", ""], ["", "P", "", "", "", "", "B", ""], [""] * 8, ]) result, mating_moves = find_mate_in_n(board, WHITE, 1) assert result == CHECKMATE write_mate_result(board, mating_moves, sys.stdout) assert len(mating_moves) == 1 assert mating_moves[0].piece == BISHOP assert mating_moves[0].src == sq_to_index("g2") assert mating_moves[0].dest == sq_to_index("h3")
def test_rook_empty_board_valid_squares(self): board = load_board([ ["", "", "", "", "", "", "", ""], ["", "R", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ]) rook_squares = sorted([ sq_to_index(sq) for sq in [ "a7", "b1", "b2", "b3", "b4", "b5", "b6", "b8", "c7", "d7", "e7", "f7", "g7", "h7" ] ]) assert sorted(get_rook_valid_squares( board, sq_to_index("b7"))) == rook_squares
def test_simple_rook_mate_in_1(self): board = load_board([ ["", "", "", "k", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "K", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["R", "", "", "", "", "", "", ""], ]) mate_result = find_mate_in_n(board, WHITE, 1) assert mate_result[0] == CHECKMATE assert len(mate_result[1]) == 1 winning_move = mate_result[1][0] assert winning_move.piece == "R" assert index_to_sq(winning_move.src) == "a1" assert index_to_sq(winning_move.dest) == "a8"
def test_not_in_mate_1(self): board = load_board([[' ', ' ', ' ', ' ', 'r', 'b', 'Q', 'k'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'p'], ['p', ' ', ' ', 'p', ' ', 'P', ' ', ' '], [' ', ' ', ' ', ' ', ' ', 'R', ' ', ' '], [' ', ' ', ' ', ' ', 'q', ' ', ' ', 'P'], [' ', 'P', ' ', ' ', 'P', ' ', ' ', ' '], ['P', 'B', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', 'K', ' ']]) b2 = gen_successor(Board(board), sq_to_index("h8"), sq_to_index("g8")) for row in dump_board(b2): print(row) assert not is_in_check(b2, BLACK) assert not is_in_checkmate(board, BLACK) assert not is_in_checkmate(board, WHITE) assert not is_in_stalemate(board, BLACK) assert not is_in_stalemate(board, WHITE) assert not _has_no_legal_moves(board, BLACK)