def test_white_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))) assert g.ask_for(KSMove(QA.ASK_ANY)) == KSAnswer(MA.HAS_ANY)
def _generate_posible_pawn_captures(self): possibilities = list() for square in list(self._board.pieces(chess.PAWN, self._board.turn)): for attacked in list(self._players_board.attacks(square)): if self._players_board.piece_at(attacked) is None: if chess.square_rank(attacked) in (0, 7): # If capture is promotion for pawn. possibilities.extend([ KSMove( QA.COMMON, chess.Move(square, attacked, promotion=chess.QUEEN)), KSMove( QA.COMMON, chess.Move(square, attacked, promotion=chess.BISHOP)), KSMove( QA.COMMON, chess.Move(square, attacked, promotion=chess.KNIGHT)), KSMove( QA.COMMON, chess.Move(square, attacked, promotion=chess.ROOK)) ]) else: # If capture is not promotion for pawn possibilities.append( KSMove(QA.COMMON, chess.Move(square, attacked))) return possibilities
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
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)
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]
def test_check_rank(): 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))) assert g.ask_for(KSMove(QA.COMMON, chess.Move( chess.D1, chess.D3))) == KSAnswer(MA.REGULAR_MOVE, special_announcement=SCA.CHECK_RANK)
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)
def test_black_from_check_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))) g.ask_for(KSMove(QA.ASK_ANY)) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G6, chess.F7))) assert g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E8, chess.E7))) == KSAnswer(MA.ILLEGAL_MOVE)
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 ]))
def test_illegal_to_castling_through_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.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))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F2, chess.F4))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.B8, chess.A6))) assert g.ask_for(KSMove(QA.COMMON, chess.Move(chess.E1, chess.G1))) == KSAnswer(MA.ILLEGAL_MOVE)
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
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)
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)
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
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
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)
def test_200_reversible_moves(): g = BerkeleyGame() for _ in range(499): g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G1, chess.F3))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G8, chess.F6))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F3, chess.G1))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F6, chess.G8))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G1, chess.F3))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.G8, chess.F6))) g.ask_for(KSMove(QA.COMMON, chess.Move(chess.F3, chess.G1))) assert g.ask_for(KSMove(QA.COMMON, chess.Move( chess.F6, chess.G8))) == KSAnswer( MA.REGULAR_MOVE, special_announcement=SCA.DRAW_TOOMANYREVERSIBLEMOVES)
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)
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)
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
def test_black_illegal_after_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))) g.ask_for(KSMove(QA.ASK_ANY)) # Legal in chess, but illegal after ask 'for any' assert g.ask_for(KSMove(QA.COMMON, chess.Move( chess.D8, chess.D7))) == KSAnswer(MA.IMPOSSIBLE_TO_ASK)
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)
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)
def test_black_legal_after_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))) g.ask_for(KSMove(QA.ASK_ANY)) # Capture by pawn after ANY assert g.ask_for(KSMove(QA.COMMON, chess.Move( chess.D5, chess.E4))) == KSAnswer(MA.CAPTURE_DONE, capture_at_square=chess.E4)
def _generate_possible_to_ask_list(self): # Very slow. :( if self._game_over: self._possible_to_ask = list() return # Make the board that current player sees self._prepare_players_board() # Now players_board is equal to board that current player sees possibilities = list() # First collect all possible moves keeping in mind castling rules. # Castling rules are kept as it is generated from referee's board, # which contain info about previous moves. possibilities.extend([ KSMove(QA.COMMON, chess_move) for chess_move in self._players_board.legal_moves ]) if self._any_rule: # Always possible to ask ANY? possibilities.append(KSMove(QA.ASK_ANY)) # Second add possible pawn captures possibilities.extend(self._generate_posible_pawn_captures()) # And remove finally — remove duplicates self._possible_to_ask = list(set(possibilities))
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
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)
def test_move_ne_nonmove(): assert KSMove(QA.ASK_ANY) != 'A nonmove.'
def test_move_sorting_last(): g = BerkeleyGame() assert sorted(g.possible_to_ask)[-1] == KSMove( QA.COMMON, chess.Move(chess.H2, chess.H4))
def test_move_sorting_first(): g = BerkeleyGame() assert sorted(g.possible_to_ask)[0] == KSMove(QA.ASK_ANY)