def test_pawn_can_defend(self): board = fen_to_board("7k/5p2/8/4B3/8/8/8/K7 w") print_board(board) assert is_in_check(board, BLACK) new_board = gen_successor(Board(board), sq_to_index("f7"), sq_to_index("f6")) print_board(new_board) assert not is_in_check(new_board, BLACK) # it follows that black is not in checkmate assert not is_in_checkmate(board, BLACK)
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_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_sq_to_index(): assert sq_to_index("a1") == 91 assert starter_board[sq_to_index("a1")] == "R" assert sq_to_index("a8") == 21 assert starter_board[sq_to_index("a8")] == "r" assert sq_to_index("h1") == 98 assert starter_board[sq_to_index("h1")] == "R" assert sq_to_index("h8") == 28 assert starter_board[sq_to_index("h8")] == "r" assert sq_to_index("c1") == 93 assert starter_board[sq_to_index("c1")] == "B"
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_promotions_pawn_backwards(self): board = load_board([[" ", "", "", "", " ", "k", "", "K"], ["p", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "], [" ", "", "", "", " ", " ", "", " "]]) assert get_promotions(board, sq_to_index("a7"), sq_to_index("a8")) == []
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_valid_sq(): idx = sq_to_index("a8") assert is_valid_square(idx) assert not is_valid_square(idx - 1) assert not is_valid_square(idx - 10) idx = sq_to_index("h8") assert is_valid_square(idx) assert not is_valid_square(idx + 1) assert not is_valid_square(idx + 2) idx = sq_to_index("h1") assert is_valid_square(idx) assert not is_valid_square(idx + 1) assert not is_valid_square(idx + 10)
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_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_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_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_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_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_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_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)
def test_piece_valid_squares(self): board = Board() assert [ sq for sq in get_piece_valid_squares(board, sq_to_index("e1")) ] == [] assert [ sq for sq in get_piece_valid_squares(board, sq_to_index("d1")) ] == [] assert [ sq for sq in get_piece_valid_squares(board, sq_to_index("c1")) ] == [] assert [ sq for sq in get_piece_valid_squares(board, sq_to_index("a1")) ] == [] assert [ sq for sq in get_piece_valid_squares(board, sq_to_index("a8")) ] == [] assert [ sq for sq in get_piece_valid_squares(board, sq_to_index("e8")) ] == [] assert sorted(get_piece_valid_squares(board, sq_to_index("e2"))) == \ sorted([sq_to_index(idx) for idx in ["e3", "e4"]])
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_king_valid_squares(self): board = Board() index = sq_to_index("e1") assert [sq for sq in get_king_valid_squares(board, index)] == [] assert [sq for sq in get_king_valid_squares(board, index)] == []
def test_pawn_valid_squares(self): board = Board() index = sq_to_index("e2") pawn_sq = sorted([sq_to_index(sq) for sq in ["e3", "e4"]]) assert sorted(get_pawn_valid_squares(board, index)) == pawn_sq
def test_is_legal_pawn_move(self): board = Board() assert is_legal_move(board, sq_to_index("e2"), sq_to_index("e3")) assert is_legal_move(board, sq_to_index("e2"), sq_to_index("e4")) assert not is_legal_move(board, sq_to_index("e2"), sq_to_index("e5")) assert not is_legal_move(board, sq_to_index("e2"), sq_to_index("f3")) assert is_legal_move(board, sq_to_index("e7"), sq_to_index("e6")) assert is_legal_move(board, sq_to_index("e7"), sq_to_index("e5")) assert not is_legal_move(board, sq_to_index("e7"), sq_to_index("e4"))
def test_knight_valid_squares(self): board = Board() index = sq_to_index("b1") knight_sq = sorted([sq_to_index(sq) for sq in ["a3", "c3"]]) assert sorted(get_knight_valid_squares(board, index)) == knight_sq
def test_rook_starter_board_valid_squares(self): board = Board() index = sq_to_index("a1") assert [sq for sq in get_rook_valid_squares(board, index)] == []