Beispiel #1
0
def test_12_possibilities_with_pawn_capture_and_promotion():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.A7, chess.Piece(chess.PAWN, chess.WHITE))
    g._board.set_piece_at(chess.H1, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.KING, chess.WHITE))
    g._generate_possible_to_ask_list()
    assert len(g.possible_to_ask) == 12
Beispiel #2
0
def test_impossible_to_promotion_without_piece_spesification():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.A7, chess.Piece(chess.PAWN, chess.WHITE))
    g._board.set_piece_at(chess.H1, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.KING, chess.WHITE))
    g._generate_possible_to_ask_list()
    assert g.ask_for(KSMove(QA.COMMON, chess.Move(
        chess.A7, chess.A8))) == KSAnswer(MA.IMPOSSIBLE_TO_ASK)
Beispiel #3
0
def test_must_use_pawns_after_positive_ask_any():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E7, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D1, chess.H5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D7, chess.D5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.H5, chess.G6)))
    g.ask_for(KSMove(QA.ASK_ANY))
    assert g.must_use_pawns == True
Beispiel #4
0
def test_possible_capture_with_promotion():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.A7, chess.Piece(chess.PAWN, chess.WHITE))
    g._board.set_piece_at(chess.H1, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.KING, chess.WHITE))
    g._generate_possible_to_ask_list()
    assert KSMove(QA.COMMON,
                  chess.Move(chess.A7, chess.B8,
                             promotion=chess.BISHOP)) in g.possible_to_ask
Beispiel #5
0
def test_black_any_true():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E7, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D1, chess.H5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D7, chess.D5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.H5, chess.G6)))
    assert g.ask_for(KSMove(QA.ASK_ANY)) == KSAnswer(MA.HAS_ANY)
Beispiel #6
0
def test_stalemate():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.C2, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.G1, chess.Piece(chess.ROOK, chess.WHITE))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.WHITE))
    g._generate_possible_to_ask_list()
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.A1, chess.A7))) == KSAnswer(
                                MA.REGULAR_MOVE,
                                special_announcement=SCA.DRAW_STALEMATE)
Beispiel #7
0
def test_white_wins():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.C2, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.G1, chess.Piece(chess.ROOK, chess.WHITE))
    g._board.set_piece_at(chess.B7, chess.Piece(chess.QUEEN, chess.WHITE))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.WHITE))
    g._generate_possible_to_ask_list()
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.A1, chess.A8))) == KSAnswer(
                                MA.REGULAR_MOVE,
                                special_announcement=SCA.CHECKMATE_WHITE_WINS)
Beispiel #8
0
def test_promotion_check_long():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.A7, chess.Piece(chess.PAWN, chess.WHITE))
    g._board.set_piece_at(chess.H1, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.KING, chess.WHITE))
    g._generate_possible_to_ask_list()
    assert g.ask_for(
        KSMove(QA.COMMON,
               chess.Move(chess.A7, chess.A8,
                          promotion=chess.QUEEN))) == KSAnswer(
                              MA.REGULAR_MOVE,
                              special_announcement=SCA.CHECK_LONG_DIAGONAL)
Beispiel #9
0
def test_check_short_diagonal_upper_left_quadrant():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.B8, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.B7, chess.Piece(chess.QUEEN, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.G1, chess.Piece(chess.QUEEN, chess.BLACK))
    g._board.turn = chess.BLACK
    g._generate_possible_to_ask_list()
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.G1, chess.A7))) == KSAnswer(
                                MA.REGULAR_MOVE,
                                special_announcement=SCA.CHECK_SHORT_DIAGONAL)
Beispiel #10
0
def test_castling():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E7, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F1, chess.C4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F8, chess.C5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G1, chess.H3)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G8, chess.H6)))
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.E1,
                                       chess.G1))) == KSAnswer(MA.REGULAR_MOVE)
Beispiel #11
0
def test_promotion_capture_draw():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.B8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.B2, chess.Piece(chess.PAWN, chess.BLACK))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.QUEEN, chess.WHITE))
    g._board.turn = chess.BLACK
    g._generate_possible_to_ask_list()
    assert g.ask_for(
        KSMove(QA.COMMON,
               chess.Move(chess.B2, chess.A1,
                          promotion=chess.BISHOP))) == KSAnswer(
                              MA.CAPTURE_DONE,
                              capture_at_square=chess.A1,
                              special_announcement=SCA.DRAW_INSUFFICIENT)
Beispiel #12
0
def test_check_knight():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.B2, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.D1, chess.Piece(chess.QUEEN, chess.BLACK))
    g._generate_possible_to_ask_list()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.B2, chess.A3)))
    g._board.set_piece_at(chess.C3, chess.Piece(chess.KNIGHT, chess.BLACK))
    g._generate_possible_to_ask_list()
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.C3, chess.B5))) == KSAnswer(
                                MA.REGULAR_MOVE,
                                special_announcement=SCA.CHECK_KNIGHT)
Beispiel #13
0
def test_white_capture_en_passant():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G8, chess.H6)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E4, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F7, chess.F5)))
    assert g.ask_for(KSMove(QA.COMMON, chess.Move(
        chess.E5, chess.F6))) == KSAnswer(MA.CAPTURE_DONE,
                                          capture_at_square=chess.F5)
Beispiel #14
0
def test_impossible_ask_nonpawnmoves_after_askany():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D7, chess.D5)))
    g.ask_for(KSMove(QA.ASK_ANY))
    assert (KSMove(QA.COMMON, chess.Move(chess.B1, chess.C3))
            in g.possible_to_ask) is False
Beispiel #15
0
def test_check_double_check_2():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.C2, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.E2, chess.Piece(chess.QUEEN, chess.BLACK))
    g._board.set_piece_at(chess.D2, chess.Piece(chess.KNIGHT, chess.BLACK))
    g._generate_possible_to_ask_list()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.C2, chess.B2)))
    res = g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D2, chess.C4)))
    assert res.check_2 in [SCA.CHECK_RANK, SCA.CHECK_KNIGHT]
Beispiel #16
0
def test_mate_is_game_over_2():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.C2, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.G1, chess.Piece(chess.ROOK, chess.WHITE))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.WHITE))
    g._generate_possible_to_ask_list()
    # Stalemate
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.A1, chess.A7)))
    assert g.is_game_over() == True
Beispiel #17
0
def test_draw_insufficient():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.A8, chess.Piece(chess.QUEEN, chess.WHITE))
    g._board.set_piece_at(chess.F7, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.G4, chess.Piece(chess.BISHOP, chess.WHITE))
    g._board.set_piece_at(chess.D4, chess.Piece(chess.KING, chess.BLACK))
    g._generate_possible_to_ask_list()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.A8, chess.D5)))
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.D4, chess.D5))) == KSAnswer(
                                MA.CAPTURE_DONE,
                                capture_at_square=chess.D5,
                                special_announcement=SCA.DRAW_INSUFFICIENT)
Beispiel #18
0
def test_check_double():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.C2, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.E2, chess.Piece(chess.QUEEN, chess.BLACK))
    g._board.set_piece_at(chess.D2, chess.Piece(chess.KNIGHT, chess.BLACK))
    g._generate_possible_to_ask_list()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.C2, chess.B2)))
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.D2, chess.C4))) == KSAnswer(
                                MA.REGULAR_MOVE,
                                special_announcement=(SCA.CHECK_DOUBLE, [
                                    SCA.CHECK_RANK, SCA.CHECK_KNIGHT
                                ]))
Beispiel #19
0
def test_no_legal_moves_after_gameover():
    g = BerkeleyGame()
    g._board.clear()
    g._board.set_piece_at(chess.C2, chess.Piece(chess.KING, chess.BLACK))
    g._board.set_piece_at(chess.H8, chess.Piece(chess.KING, chess.WHITE))
    g._board.set_piece_at(chess.G1, chess.Piece(chess.ROOK, chess.BLACK))
    g._board.set_piece_at(chess.B7, chess.Piece(chess.QUEEN, chess.BLACK))
    g._board.set_piece_at(chess.A1, chess.Piece(chess.ROOK, chess.BLACK))
    g._board.turn = chess.BLACK
    g._generate_possible_to_ask_list()
    # Checkmate
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.A1, chess.A8)))
    assert len(g.possible_to_ask) == 0
Beispiel #20
0
def test_white_capture_and_check():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E7, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D1, chess.H5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D7, chess.D5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.H5, chess.G6)))
    g.ask_for(KSMove(QA.ASK_ANY))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D5, chess.E4)))
    g.ask_for(KSMove(QA.ASK_ANY))
    assert g.ask_for(KSMove(QA.COMMON,
                            chess.Move(chess.G6, chess.F7))) == KSAnswer(
                                MA.CAPTURE_DONE,
                                capture_at_square=chess.F7,
                                special_announcement=SCA.CHECK_SHORT_DIAGONAL)
Beispiel #21
0
def test_if_berkeley_wo_any():
    g = BerkeleyGame(any_rule=False)
    assert g.ask_for(KSMove(QA.ASK_ANY)) == KSAnswer(MA.IMPOSSIBLE_TO_ASK)
Beispiel #22
0
def test_ask_for_bad_type():
    g = BerkeleyGame()
    with pytest.raises(TypeError):
        g.ask_for('Not a KSMove.')
Beispiel #23
0
def test_white_starts():
    g = BerkeleyGame()
    assert g.turn == chess.WHITE
Beispiel #24
0
def test_move_sorting_first():
    g = BerkeleyGame()
    assert sorted(g.possible_to_ask)[0] == KSMove(QA.ASK_ANY)
Beispiel #25
0
def test_white_any_false():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E7, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D1, chess.H5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D7, chess.D5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.H5, chess.G6)))
    g.ask_for(KSMove(QA.ASK_ANY))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D5, chess.E4)))
    assert g.ask_for(KSMove(QA.ASK_ANY)) == KSAnswer(MA.NO_ANY)
Beispiel #26
0
def test_possible_to_ask(what, result):
    g = BerkeleyGame()
    assert g.is_possible_to_ask(what) == result
Beispiel #27
0
def test_may_not_use_pawns_at_initial_state():
    g = BerkeleyGame()
    assert g.must_use_pawns == False
Beispiel #28
0
def test_initial_game_is_not_over():
    g = BerkeleyGame()
    assert g.game_over == False
Beispiel #29
0
def test_move_sorting_last():
    g = BerkeleyGame()
    assert sorted(g.possible_to_ask)[-1] == KSMove(
        QA.COMMON, chess.Move(chess.H2, chess.H4))
Beispiel #30
0
def test_impossible_ask_same_move_twice():
    g = BerkeleyGame()
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E2, chess.E4)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E7, chess.E5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D1, chess.H5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D7, chess.D5)))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.H5, chess.G6)))
    g.ask_for(KSMove(QA.ASK_ANY))
    g.ask_for(KSMove(QA.COMMON, chess.Move(chess.D5, chess.E4)))
    illegal_move = KSMove(QA.COMMON, chess.Move(chess.G6, chess.G8))
    g.ask_for(illegal_move)
    g.ask_for(KSMove(QA.ASK_ANY))
    assert g.ask_for(illegal_move) not in g.possible_to_ask