def conduct_move(existing_board, candidate_move, player_col): """ Uses the supplied move on the existing board and returns a new board if the move is valid. If the move causes current player to be checked, False is returned. Does NOT modify existing_board. Note: Does minimal verification of validity of move """ # Copy old 2D board array new_board = deepcopy(existing_board) start_coords = candidate_move.start_coords end_coords = candidate_move.end_coords piece = selected_piece(new_board, start_coords) if piece.col != player_col: return False if candidate_move.move_type == MoveType.NORMAL: move_history_element = _move_piece_inplace(new_board, candidate_move) elif candidate_move.move_type == MoveType.CASTLING: if candidate_move.end_coords[1] < King.KING_HOME_COL: # Move column a rook to other side of where King will move rook = new_board[start_coords[0]][0] new_board[start_coords[0]][2] = rook new_board[start_coords[0]][0] = BlankPiece() rook.has_never_moved = False move_history_element = _move_piece_inplace(new_board, candidate_move) else: # Move column h rook to other side of where King will move rook = new_board[start_coords[0]][7] new_board[start_coords[0]][5] = rook new_board[start_coords[0]][7] = BlankPiece() rook.has_never_moved = False move_history_element = _move_piece_inplace(new_board, candidate_move) elif candidate_move.move_type == MoveType.PROMOTION: # Advance pawn to final row move_history_element = _move_piece_inplace(new_board, candidate_move) # Then replace it with selected piece in-place new_board[end_coords[0]][ end_coords[1]] = candidate_move.promotion_piece elif candidate_move.move_type == MoveType.EN_PASSANT: # Move pawn to where you'd expect it move_history_element = _move_piece_inplace(new_board, candidate_move) # Remove the opponents' pawn via enpassant en_passant_pawn = new_board[end_coords[0] - piece.forward_dir][end_coords[1]] new_board[end_coords[0] - piece.forward_dir][end_coords[1]] = BlankPiece() move_history_element.capture_piece = en_passant_pawn return new_board, move_history_element
def create(): b = [[Rook("black"), Knight("black"), Bishop("black"), Queen("black"), King("black"), Bishop("black"), Knight("black"), Rook("black")], [Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black"), Pawn("black")]] for i in range(2, 6): b.append([BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece(), BlankPiece()]) b.append([Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white"), Pawn("white")]) b.append([Rook("white"), Knight("white"), Bishop("white"), Queen("white"), King("white"), Bishop("white"), Knight("white"), Rook("white")]) return b
def test_stalemate(): # Opposite col test omitted as player color doesn't effect this test. __ = BlankPiece() wk = King("white") r = Rook("black") q = Queen("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, r, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, bk], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, q, __, __, __], # 6 [wk, __, __, __, __, __, __, __] # 7 ] assert_false("White should NOT be a check state", is_being_checked(board, "white")) assert_true("White should be in stalemate", is_stalemate(board, "white", [])) assert_false("Black should NOT be in stalemate state", is_stalemate(board, "black", []))
def test_pawn_enpassant(): # Advance pawn by two spaces conducted_move_history = [] board = get_enpassant_board() board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [1, 5], [3, 5]), "black") conducted_move_history.append(move_history_element) # Assert white pawn can only do regular attack, one space advance AND enpassant en_passant_move = Move.en_passant([3, 6], [2, 5]) assert_contains( board[3][6].get_attack_set(board, [3, 6], conducted_move_history), [en_passant_move]) # Actually execute en passant move board, move_history_element = conduct_move(board, en_passant_move, "white") conducted_move_history.append(move_history_element) __ = BlankPiece() p2 = Pawn("black") wp = Pawn("white") # 0 1 2 3 4 5 6 7 expected_board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, p2], # 1 [__, __, __, __, __, wp, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_row_contain_same_type_elements(expected_board[1], board[1]) assert_row_contain_same_type_elements(expected_board[2], board[2]) assert_row_contain_same_type_elements(expected_board[3], board[3])
def test_white_pawn_promotion(): __ = BlankPiece() p1 = Pawn("white") p2 = Pawn("white") # Enemy rook rr = Rook("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, rr, __, __], # 0 [__, __, p1, __, __, p2, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] # Left-most pawn assert_length(board[1][2].get_attack_set(board, [1, 2], []), 0) assert_contains(board[1][2].get_move_set(board, [1, 2], []), [ Move.pawn_promotion([1, 2], [0, 2], Queen("white")), Move.pawn_promotion([1, 2], [0, 2], Bishop("white")), Move.pawn_promotion([1, 2], [0, 2], Knight("white")), Move.pawn_promotion([1, 2], [0, 2], Rook("white")) ]) assert_length(board[1][5].get_attack_set(board, [1, 5], []), 0) assert_length(board[1][5].get_move_set(board, [1, 5], []), 0)
def test_conducting_col_h_castle_move(): board = get_castling_board() col_h_castle = Move(MoveType.CASTLING, [7, 4], [7, 6]) board, move_history_element = conduct_move(board, col_h_castle, "white") _ = BlankPiece() expected_row = [Rook("white"), _, _, _, _, Rook("white"), King("white"), _] actual_row = board[7] assert_row_contain_same_type_elements(expected_row, actual_row)
def test_serialization(): __ = BlankPiece() wp = Pawn("white") bp = Pawn("black") br = Rook("black") bh = Knight("black") bb = Bishop("black") bq = Queen("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [br, bh, bb, bq, bk, __, __, __], # 0 [bp, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [wp, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] serializable_gameboard = convert_to_string_gameboard(board) expected_gameboard = [ ['br', 'bh', 'bb', 'bq', 'bk', '_', '_', '_'], ['bp', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'], ['wp', '_', '_', '_', '_', '_', '_', '_'], ['_', '_', '_', '_', '_', '_', '_', '_'] ] assert_length(serializable_gameboard, 8) assert_length(serializable_gameboard[3], 8) if expected_gameboard != serializable_gameboard: raise AssertionError("Expected gameboard" + str(expected_gameboard) + " but was " + str(serializable_gameboard)) new_board = convert_from_string_gameboard(expected_gameboard) for i in range(0, len(board)): for j in range(0, len(board[0])): expected_piece = board[i][j] actual_piece = new_board[i][j] if expected_piece.type != actual_piece.type: raise AssertionError("Expected piece type at " + str(i) + ", " + str(j) + " was " + expected_piece.type + ", but got " + actual_piece.type) if expected_piece.col != actual_piece.col: raise AssertionError("Expected piece col at " + str(i) + ", " + str( j) + " was " + expected_piece.col + " but got " + actual_piece.col)
def _move_piece_inplace(board, move): """ Moves the piece at move.start_coords to move.end_coords, and replaces the first pieces with a blank piece Board is modified in-place. """ start_coords = move.start_coords end_coords = move.end_coords piece = board[start_coords[0]][start_coords[1]] captured_piece = board[end_coords[0]][end_coords[1]] board[end_coords[0]][end_coords[1]] = piece piece.has_never_moved = False board[start_coords[0]][start_coords[1]] = BlankPiece() return MoveHistoryElement(move, piece, captured_piece)
def get_fifty_move_rule_board(): """ Helper function to run through the first 3 moves in a "four move rule" (use a smaller deque for sanity here -- 50 is a lot), """ # conducted_move_history = deque([], 4) __ = BlankPiece() wk = King("white") wp = Pawn("white") br = Rook("black") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, bk, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, br, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, wp, __, __, __, __, __, __], # 6 [__, __, __, __, wk, __, __, __] # 7 ] msg = "Should NOT be 50-move rule draw" board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [7, 4], [7, 1]), "white") conducted_move_history.append(move_history_element) assert_false(msg, is_fifty_move_rule_draw(conducted_move_history)) board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [7, 1], [7, 2]), "white") conducted_move_history.append(move_history_element) assert_false(msg, is_fifty_move_rule_draw(conducted_move_history)) board, move_history_element = conduct_move( board, Move(MoveType.NORMAL, [7, 2], [7, 3]), "white") conducted_move_history.append(move_history_element) assert_false(msg, is_fifty_move_rule_draw(conducted_move_history)) return board, conducted_move_history
def test_check(): # Opposite col test omitted as it requires an alternate implementation *and* player color doesn't effect this test. _ = BlankPiece() k = King("white") k.has_never_moved = False p = Pawn("black") p.has_never_moved = False # 0 1 2 3 4 5 6 7 board = [ [_, _, _, _, _, _, _, _], # 0 [_, _, _, _, _, _, _, _], # 1 [_, _, _, _, _, p, _, _], # 2 [_, _, _, _, k, _, _, _], # 3 [_, _, _, _, _, _, _, _], # 4 [_, _, _, _, _, _, _, _], # 5 [_, _, _, _, _, _, _, _], # 6 [_, _, _, _, _, _, _, _] # 7 ] assert_true("King should be checked", is_being_checked(board, "white")) # Enemy rook r = Rook("black") q = Queen("black") # 0 1 2 3 4 5 6 7 board = [ [_, _, _, _, _, _, _, _], # 0 [_, _, _, _, _, _, _, _], # 1 [_, _, _, _, _, _, _, _], # 2 [_, _, _, _, _, _, _, _], # 3 [_, _, _, _, _, _, _, _], # 4 [_, _, _, _, _, _, _, _], # 5 [_, _, _, _, _, _, r, _], # 6 [_, _, k, _, _, _, _, q] # 7 ] assert_false("Should be checkmate", can_player_leave_check_state(board, "white", []))
def get_castling_board(): # Home rank is just a constant in the castling implementation # so it's asserted unit testing only white piece's castling is sufficient __ = BlankPiece() p = Pawn("white") k = King("white") r1 = Rook("white") r2 = Rook("white") # 0 1 2 3 4 5 6 7 return [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, p, p, p, __, __], # 6 [r1, __, __, __, k, __, __, r2] # 7 ]
def get_enpassant_board(): __ = BlankPiece() p1 = Pawn("black") p2 = Pawn("black") wp = Pawn("white") wp.has_never_moved = False # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, p1, __, p2], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, wp, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_length(board[3][6].get_attack_set(board, [3, 6], []), 0) assert_contains(board[3][6].get_move_set(board, [3, 6], []), [Move(MoveType.NORMAL, [3, 6], [2, 6])]) return board
def test_white_pawn_movements(): __ = BlankPiece() p1 = Pawn("white") p2 = Pawn("white") p3 = Pawn("white") p3.has_never_moved = False # Enemy rook rr = Rook("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, rr, rr], # 3 [__, __, __, __, __, __, __, p3], # 4 [__, __, __, __, rr, __, __, __], # 5 [p1, __, __, __, __, p2, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] # Left-most pawn assert_length(board[6][0].get_attack_set(board, [6, 0], []), 0) assert_contains( board[6][0].get_move_set(board, [6, 0], []), create_list_of_moves(MoveType.NORMAL, [6, 0], [[5, 0], [4, 0]])) assert_contains(board[6][5].get_attack_set(board, [6, 5], []), create_list_of_moves(MoveType.NORMAL, [6, 5], [[5, 4]])) assert_contains( board[6][5].get_move_set(board, [6, 5], []), create_list_of_moves(MoveType.NORMAL, [6, 5], [[5, 5], [4, 5]])) assert_contains(board[4][7].get_attack_set(board, [4, 7], []), create_list_of_moves(MoveType.NORMAL, [4, 7], [[3, 6]])) assert_length(board[4][7].get_move_set(board, [4, 7], []), 0)
def convert_from_string_gameboard(serializable_gameboard): """ Get gameboard from the serializable entity. Note: This information is only useful for display purposes. (information such as whether castling is still a valid move does not exist here). :param game_board: list of lists containing "wp" or "bk" to represent white pawn or black knight. :return: list of list containing instances of the Piece class """ gameboard = [] for row in serializable_gameboard: gameboard_row = [] for piece in row: piece_col = "" if piece == "_": gameboard_row.append(BlankPiece()) else: if piece[0] == "w": piece_col = "white" elif piece[0] == "b": piece_col = "black" if piece[1] == "r": gameboard_row.append(Rook(piece_col)) elif piece[1] == "h": gameboard_row.append(Knight(piece_col)) elif piece[1] == "b": gameboard_row.append(Bishop(piece_col)) elif piece[1] == "q": gameboard_row.append(Queen(piece_col)) elif piece[1] == "k": gameboard_row.append(King(piece_col)) elif piece[1] == "p": gameboard_row.append(Pawn(piece_col)) gameboard.append(gameboard_row) return gameboard
def test_black_pawn_movements(): __ = BlankPiece() p1 = Pawn("black") p2 = Pawn("black") p3 = Pawn("black") # Enemy rook rr = Rook("white") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [p1, __, __, __, __, p2, __, __], # 1 [__, __, __, __, rr, __, __, __], # 2 [__, __, __, __, __, __, __, p3], # 3 [__, __, __, __, __, __, rr, rr], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] # Left-most pawn assert_length(board[1][0].get_attack_set(board, [1, 0], []), 0) assert_contains( board[1][0].get_move_set(board, [1, 0], []), create_list_of_moves(MoveType.NORMAL, [1, 0], [[2, 0], [3, 0]])) assert_contains(board[1][5].get_attack_set(board, [1, 5], []), create_list_of_moves(MoveType.NORMAL, [1, 5], [[2, 4]])) assert_contains( board[1][5].get_move_set(board, [1, 5], []), create_list_of_moves(MoveType.NORMAL, [1, 5], [[2, 5], [3, 5]])) assert_contains(board[3][7].get_attack_set(board, [3, 7], []), create_list_of_moves(MoveType.NORMAL, [3, 7], [[4, 6]])) assert_length(board[3][7].get_move_set(board, [3, 7], []), 0)
def test_sliding_pieces(player_col, opponent_col): _ = BlankPiece() r = Rook(player_col) b = Bishop(player_col) q = Queen(player_col) # Enemy rook e = Rook(opponent_col) # Friendly Pawn f = Pawn(player_col) f.has_never_moved = False # 0 1 2 3 4 5 6 7 board = [ [r, _, _, e, _, _, _, _], # 0 [_, _, _, _, _, _, r, _], # 1 [_, _, q, _, _, _, f, _], # 2 [_, _, _, _, _, _, _, _], # 3 [_, _, _, _, _, _, _, _], # 4 [_, _, _, b, _, _, _, _], # 5 [_, _, _, _, _, _, _, _], # 6 [_, _, e, _, _, _, _, f] # 7 ] # Top-left rook assert_length(board[0][0].get_move_set(board, [0, 0], []), 0) assert_contains( board[0][0].get_attack_set(board, [0, 0], []), create_list_of_moves( MoveType.NORMAL, [0, 0], [ # Down [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], # Right [0, 1], [0, 2], [0, 3] ])) # Second rook assert_length(board[1][6].get_move_set(board, [1, 6], []), 0) assert_contains( board[1][6].get_attack_set(board, [1, 6], []), create_list_of_moves( MoveType.NORMAL, [1, 6], [ # Up [0, 6], # Left [1, 5], [1, 4], [1, 3], [1, 2], [1, 1], [1, 0], # Right [1, 7] ])) # Bishop assert_length(board[5][3].get_move_set(board, [5, 3], []), 0) assert_contains( board[5][3].get_attack_set(board, [5, 3], []), create_list_of_moves( MoveType.NORMAL, [5, 3], [ # North-west [4, 2], [3, 1], [2, 0], # North-east [4, 4], [3, 5], # South-west [6, 2], [7, 1], # South-east [6, 4], [7, 5] ])) # Queen assert_length(board[2][2].get_move_set(board, [2, 2], []), 0) assert_contains( board[2][2].get_attack_set(board, [2, 2], []), create_list_of_moves( MoveType.NORMAL, [2, 2], [ # Down [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], # Up [1, 2], [0, 2], # Left [2, 0], [2, 1], # Right [2, 3], [2, 4], [2, 5], # North-west [1, 1], # North-east [1, 3], [0, 4], # South-west [3, 1], [4, 0], # South-east [3, 3], [4, 4], [5, 5], [6, 6] ]))
def test_teleporting_pieces(player_col, opponent_col): _ = BlankPiece() k = King(player_col) k.has_never_moved = False h = Knight(player_col) # Enemy rook e = Rook(opponent_col) # Friendly Pawn f = Pawn(player_col) f.has_never_moved = False # 0 1 2 3 4 5 6 7 board = [ [h, _, _, _, _, _, _, _], # 0 [_, _, _, _, _, _, _, _], # 1 [_, _, _, f, _, e, _, _], # 2 [_, _, _, _, _, _, _, _], # 3 [_, _, _, _, h, _, _, _], # 4 [_, _, _, _, _, _, _, _], # 5 [_, h, _, _, _, _, _, _], # 6 [_, _, _, _, _, _, _, k] # 7 ] # Top-left knight assert_length(board[0][0].get_move_set(board, [0, 0], []), 0) assert_contains( board[0][0].get_attack_set(board, [0, 0], []), create_list_of_moves(MoveType.NORMAL, [0, 0], [ [1, 2], [2, 1], ])) # Knight near bottom assert_length(board[6][1].get_move_set(board, [6, 1], []), 0) assert_contains( board[6][1].get_attack_set(board, [6, 1], []), create_list_of_moves(MoveType.NORMAL, [6, 1], [[4, 0], [4, 2], [5, 3], [7, 3]])) # Middle knight assert_length(board[4][4].get_move_set(board, [4, 4], []), 0) assert_contains( board[4][4].get_attack_set(board, [4, 4], []), create_list_of_moves( MoveType.NORMAL, [4, 4], [[2, 5], [6, 5], [6, 3], [3, 2], [5, 2], [3, 6], [5, 6]])) # Bottom-right king assert_length(board[7][7].get_move_set(board, [7, 7], []), 0) assert_contains( board[7][7].get_attack_set(board, [7, 7], []), create_list_of_moves( MoveType.NORMAL, [7, 7], [ # Up [6, 7], # Left [7, 6], # north-east [6, 6] ]))
def test_threefold_repetition(): move_list = [] # Not maintaining conducted_move_history for brevity conducted_move_history = [] __ = BlankPiece() wk = King("white") bk = King("black") # 0 1 2 3 4 5 6 7 board0 = [ [__, __, __, bk, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, wk, __, __, __] # 7 ] # 0 1 2 3 4 5 6 7 board1 = [ [__, __, __, __, bk, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, wk, __, __, __] # 7 ] board2 = [ [__, __, __, __, bk, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, wk, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] board3 = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, bk, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, wk, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] board4 = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, bk, __, __, __], # 1 [__, __, __, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, __, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, wk, __, __, __] # 7 ] msg = "Expected to not yet have 3 repetitions" # Initial state update_move_history(board0, move_list, "black", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) # Board state repeated once update_move_history(board1, move_list, "white", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) update_move_history(board2, move_list, "black", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) update_move_history(board3, move_list, "white", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) update_move_history(board4, move_list, "black", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) # Board state repeated twice update_move_history(board1, move_list, "white", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) update_move_history(board2, move_list, "black", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) update_move_history(board3, move_list, "white", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) update_move_history(board4, move_list, "black", conducted_move_history) assert_false(msg, is_threefold_repetition_stalemate(move_list)) # Board state repeated three times update_move_history(board1, move_list, "white", conducted_move_history) assert_true("Expected threefold repetition stalemate", is_threefold_repetition_stalemate(move_list))
def test_insufficient_material_states(): __ = BlankPiece() wk = King("white") bk = King("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, wk, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true("Just kings should impossible to checkmate", is_impossible_to_reach_checkmate(board)) bB = Bishop("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, wk, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, bB, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true("King vs King and bishop should be impossible to checkmate", is_impossible_to_reach_checkmate(board)) bH = Knight("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, __, __, __, __, wk, __], # 2 [__, __, __, __, __, __, __, __], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, bH, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true("King vs King and Knight should be impossible checkmate", is_impossible_to_reach_checkmate(board)) wb = Bishop("white") bb = Bishop("black") # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, __, __, bb], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_true( "King and Bishop vs King and Bishop should be impossible if they are on the same color square", is_impossible_to_reach_checkmate(board)) # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, __, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, bb], # 4 [__, __, __, __, __, __, __, __], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, __] # 7 ] assert_false( "King and Bishop vs King and Bishop should be POSSIBLE to reach checkmate if they're on different " "colours", is_impossible_to_reach_checkmate(board)) # Does not matter the amount of Bishops as long as they are on the same color square # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, wb, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, wb, __, bb], # 5 [__, __, __, __, __, __, __, __], # 6 [__, __, __, __, __, __, __, wb] # 7 ] assert_true( "King and Bishop(s) vs King and Bishop(s) should be impossible if they are on the same color square", is_impossible_to_reach_checkmate(board)) # 0 1 2 3 4 5 6 7 board = [ [__, __, __, __, __, __, __, __], # 0 [__, __, __, __, __, wb, __, __], # 1 [__, __, wk, __, __, __, __, __], # 2 [__, __, __, __, __, __, __, wb], # 3 [__, __, bk, __, __, __, __, __], # 4 [__, __, __, __, __, wb, __, bb], # 5 [__, __, __, __, __, __, __, wb], # 6 [__, __, __, __, __, __, __, wb] # 7 ] assert_false( "King and Bishop(s) vs King and Bishop(s) should be POSSIBLE as not all on same color square", is_impossible_to_reach_checkmate(board))