def get_all_black_pawn_attacks(current_board): all_attacks = [] for i in range(len(current_board)): for j in range(len(current_board[i])): piece_type = current_board[i][j].split("_")[1] current_possition = [i, j] if piece_type == "bp": piece_attacks = move_check.get_current_piece_attacks( current_possition, current_board) for pos in piece_attacks: all_attacks.append(pos) # To purge this list of duplicates! res = [] for i in all_attacks: if i not in res: res.append(i) return res
def opponent_has_no_legal_moves(turn, current_board): all_white_coords = get_all_white_coords(current_board) all_black_coords = get_all_black_coords(current_board) # all_white_moves = get_all_white_moves(current_board) # all_black_moves = get_all_black_moves(current_board) # all_black_attacks = get_all_black_attacks(current_board) # all_white_attacks = get_all_white_attacks(current_board) # white_king_coord = get_white_king_coord(current_board) # black_king_coord = get_black_king_coord(current_board) black_king_attacks = get_black_king_attacks(current_board) white_king_attacks = get_white_king_attacks(current_board) # all_black_pawn_attacks = get_all_black_pawn_attacks(current_board) # all_white_pawn_attacks = get_all_white_pawn_attacks(current_board) # all_white_pieces_attacked_by_black_pawns = [pos for pos in all_white_coords if pos in all_black_pawn_attacks] # all_black_pieces_attacked_by_white_pawns = [pos for pos in all_black_coords if pos in all_white_pawn_attacks] # black_king_moves = black_king_attacks # for pos in black_king_attacks: # if pos in white_king_attacks: # black_king_moves.pop(pos) # white_king_moves = white_king_attacks # for pos in white_king_attacks: # if pos in black_king_attacks: # white_king_moves.pop(pos) if turn == "white": check = 0 # Test all available black moves. If any move is legal, the function returns false. for pos in all_black_coords: this_current_piece = move_check.find_current_piece( pos, current_board) piece_moves = move_check.get_current_piece_moves( pos, current_board) if this_current_piece != "bp": for attack in piece_moves: new_board = current_board # bug happens here attacked_piece = find_current_piece(attack, current_board) new_board = test_move(pos, attack, new_board) new_all_black_attacks = get_all_black_attacks(new_board) new_all_white_attacks = get_all_white_attacks(new_board) new_white_king_coord = get_white_king_coord(new_board) new_black_king_coord = get_black_king_coord(new_board) new_board = rev_test_move(pos, attack, new_board, attacked_piece) if (new_black_king_coord not in new_all_white_attacks): check = 1 elif this_current_piece == "bp": pawn_attacks = move_check.get_current_piece_attacks( pos, current_board) valid_attacks = [ pos for pos in all_white_coords if pos in pawn_attacks ] for attack in valid_attacks: new_board = current_board attacked_piece = find_current_piece(attack, current_board) new_board = test_move(pos, attack, new_board) new_all_black_attacks = get_all_black_attacks(new_board) new_all_white_attacks = get_all_white_attacks(new_board) new_white_king_coord = get_white_king_coord(new_board) new_black_king_coord = get_black_king_coord(new_board) new_board = rev_test_move(pos, attack, new_board, attacked_piece) if (new_black_king_coord not in new_all_white_attacks): check = 1 print(check) if check == 1: return False elif check == 0: return True if turn == "black": check = 0 # Test all available white moves. If any move is legal, the function returns false. for pos in all_white_coords: this_current_piece = move_check.find_current_piece( pos, current_board) piece_moves = move_check.get_current_piece_moves( pos, current_board) if this_current_piece != "wp": for attack in piece_moves: new_board = current_board attacked_piece = find_current_piece(attack, current_board) new_board = test_move(pos, attack, new_board) new_all_black_attacks = get_all_black_attacks(new_board) new_all_white_attacks = get_all_white_attacks(new_board) new_white_king_coord = get_white_king_coord(new_board) new_black_king_coord = get_black_king_coord(new_board) new_board = rev_test_move(pos, attack, new_board, attacked_piece) if (new_white_king_coord not in new_all_black_attacks): check = 1 elif this_current_piece == "wp": pawn_attacks = move_check.get_current_piece_attacks( pos, current_board) valid_attacks = [ pos for pos in all_black_coords if pos in pawn_attacks ] for attack in valid_attacks: new_board = current_board attacked_piece = find_current_piece(attack, current_board) new_board = test_move(pos, attack, new_board) new_all_black_attacks = get_all_black_attacks(new_board) new_all_white_attacks = get_all_white_attacks(new_board) new_white_king_coord = get_white_king_coord(new_board) new_black_king_coord = get_black_king_coord(new_board) new_board = rev_test_move(pos, attack, new_board, attacked_piece) if (new_white_king_coord not in new_all_black_attacks): check = 1 print(check) if check == 1: return False elif check == 0: return True