def main(): with open(sys.argv[1]) as file: data = json.load(file) # TODO: find and print winning action sequence start = time.time() board = data print_board(game.get_grid_format(board), "Start") solution = ai.search(data) for action, stack_from, stack_to in solution: if action == "move": board = game.move(stack_from, stack_to, board) if DEBUG: print_board(game.get_grid_format(board)) else: print_move(stack_to[N_TOKENS], stack_from[X_POS], stack_from[Y_POS], stack_to[X_POS], stack_to[Y_POS]) if action == "boom": board = game.boom(stack_from, board) if DEBUG: print_board(game.get_grid_format(board)) else: print_boom(stack_from[X_POS], stack_from[Y_POS]) print("# Length of solution ", len(solution)) print("# Execution time: ", time.time() - start)
def boomAll(gs): """ Prints "boom" for every white token """ if len(gs["white"]) > 0: token = gs["white"][0] print_boom(token[1], token[2]) newGs = boom(gs, token[1], token[2], groupBlacks(gs))[0] boomAll(newGs)
def print_history_behaviors(history_behaviors_list): for behavior in history_behaviors_list: if behavior: if behavior[0] == "boom": print_boom(behavior[1][0], behavior[1][1]) else: print_move(behavior[3], behavior[1][0], behavior[1][1], behavior[2][0], behavior[2][1])
def print_action(action): """ print an action Args: action: action tuple """ if action[0] == 0: print_boom(action[1], action[2]) else: print_move(action[0], action[1], action[2], action[3], action[4])
def expendibots(data): # Use A* algorithm to find optimal path # Have it return a path (list of actions either Move or Boom) # Print the actions path = a_star_search(data) for action in path: if action is not None: if action.name == "Move": util.print_move(action.n, action.x_a, action.y_a, action.x_b, action.y_b) else: util.print_boom(action.x, action.y)
def findPath(node, visited): """ This function prints all the moves made to go from the initial gamestate to the final one Arguments: node {node} -- the final node (containing the final gamestate) visited {list(node)} -- list of the nodes visited during the search Returns: list(node) -- list of the nodes from the initial one to the final one """ if node[1] == 0: return [] else: result = findPath(list(filter(lambda n: n[0][0] == node[3], visited))[0], visited) + [node[0]] if node[0][1][0] == "move": print_move(node[0][1][1:]) elif node[0][1][0] == "boom": print_boom(node[0][1][1], node[0][1][2]) return result
def print(self, **kwargs): print_boom(*self.square, **kwargs)
def run_case(data): """ Run simulation :param data: JSON input :type data: Dictionary """ emptys = generate_all_empty_squares(data) whites = [tuple(white) for white in data["white"]] blacks = [tuple(black) for black in data["black"]] layout = {"emptys": emptys, "whites": whites, "blacks": blacks} def get_exploded_dict(layout): def get_exploded_tokens(coordinate, exploded_tokens): _3x3_surrounding_white_tokens = get_3x3_surrounding_tokens( layout["whites"], find_3x3_surrounding_squares(coordinate)) _3x3_surrounding_black_tokens = get_3x3_surrounding_tokens( layout["blacks"], find_3x3_surrounding_squares(coordinate)) for token in _3x3_surrounding_black_tokens: if token not in exploded_tokens['blacks']: exploded_tokens['blacks'].append(token) coordinate = tuple(token[1:]) get_exploded_tokens(coordinate, exploded_tokens) for token in _3x3_surrounding_white_tokens: if token not in exploded_tokens['whites']: exploded_tokens['whites'].append(token) coordinate = tuple(token[1:]) get_exploded_tokens(coordinate, exploded_tokens) exploded_dict = {} non_black_squares = sorted(layout["emptys"] + layout["whites"]) for non_black_square in non_black_squares: exploded_tokens = {"blacks": [], "whites": []} get_exploded_tokens(non_black_square[1:], exploded_tokens) exploded_dict[non_black_square] = exploded_tokens return exploded_dict def pick_up(white, layout): # A white token is picked up if white not in layout["emptys"]: layout["emptys"].append(white) if white in layout["whites"]: layout["whites"].remove(white) return layout def place(empty, layout): # A white token is placed if empty in layout["emptys"]: layout["emptys"].remove(empty) layout["whites"].append(empty) return layout # Recursively finding the destinations whites will move to def find_destinations(exploded_blacks, exploded_whites, destinations, n, destinations_list, layout): if n >= 1: # Determine which white to move white = layout["whites"][0] # Pick up a token and Reset layout layout = pick_up(white, layout) # print(n, white, layout["whites"]) # Obtain exploded dictionary exploded_dict = get_exploded_dict(layout) non_blacks = sorted(layout["emptys"] + layout["whites"]) for empty in non_blacks: exploded_blacks_tmp = exploded_blacks.copy() exploded_whites_tmp = exploded_whites.copy() # Obtain list of exploded tokens token_dict = exploded_dict[empty] for black in token_dict['blacks']: if black not in exploded_blacks: exploded_blacks_tmp.append(black) for exploded_white in token_dict['whites']: if exploded_white not in exploded_whites: exploded_whites_tmp.append(exploded_white) # Place the token layout = place(empty, layout) # Recursively adding exploded blacks if len( find_destinations(exploded_blacks_tmp, exploded_whites_tmp, destinations + [empty], n - 1 - len(exploded_whites_tmp), destinations_list, layout)) == len(blacks): destinations_list.append(destinations + [empty]) # Pick up placed token layout = pick_up(empty, layout) # Place the token back layout = place(white, layout) return exploded_blacks else: return exploded_blacks destinations = [] destinations_list = [] layout_copy = copy.deepcopy(layout) find_destinations([], [], destinations, len(whites), destinations_list, layout_copy) destinations = destinations_list[0] # print("destinations", destinations) # Level 1-3 for i in range(len(destinations)): white = layout["whites"][i] start = tuple(white) end = destinations[i] whites_adjacency_list = generate_adjacency_list( white, layout, find_adjacent_squares) # Check if end is accessible if end in dfs(whites_adjacency_list, start): shortest_path = bfs_shortest_path(blacks, whites_adjacency_list, start, end) print_move_actions(white, shortest_path) print_boom(end[1], end[2]) return ###**************************************************************************************### ### level 4 ### ###**************************************************************************************### # Determine if the whites are trapped reachable = {} for i in range(len(destinations)): for white in whites: start = tuple(white) end = destinations[i] whites_adjacency_list = generate_adjacency_list( white, layout, find_adjacent_squares) # Check if end is accessible if end in dfs(whites_adjacency_list, start): if white not in reachable.keys(): reachable[white] = [end] else: reachable[white].append(end) else: reachable[white] = [] reachable_copy = reachable.copy() for i in range(len(destinations)): for k, v in reachable_copy.items(): if destinations[i] in v: reachable_copy.pop(k) break # Level 1-3 if reachable_copy == {}: for i in range(len(destinations)): white = layout["whites"][i] start = tuple(white) end = destinations[i] whites_adjacency_list = generate_adjacency_list( white, layout, find_adjacent_squares) # Check if end is accessible if end in dfs(whites_adjacency_list, start): shortest_path = bfs_shortest_path(blacks, whites_adjacency_list, start, end) print_move_actions(white, shortest_path) print_boom(end[1], end[2]) return # Level 4 # Stacking trapped_whites = whites.copy() trapped_whites_copy = whites.copy() layout_copy = layout.copy() for i in range(len(trapped_whites)): for j in range(i + 1, len(trapped_whites)): start = trapped_whites[i] end = trapped_whites[j] whites_adjacency_list = generate_adjacency_list( start, layout_copy, find_adjacent_squares) if end in dfs(whites_adjacency_list, start): shortest_path = bfs_shortest_path(blacks, whites_adjacency_list, start, end) print_move_actions(start, shortest_path) # Stack moved = (str("W" + str( int(trapped_whites[j][0][1:]) + int(trapped_whites[i][0][1:]))), ) + tuple( trapped_whites[j][1:]) layout.remove(trapped_whites[i]) layout.append( ("E", trapped_whites[i][1], trapped_whites[i][2])) layout.remove(trapped_whites[j]) layout.append(moved) # trapped_whites[j] = moved trapped_whites_copy[j] = moved trapped_whites_copy.remove(trapped_whites[i]) break trapped_whites = trapped_whites_copy.copy() trapped_whites.sort(key=lambda x: int(x[0][1:]), reverse=True) for i in range(len(destinations)): white = trapped_whites[i] start = tuple(white) end = destinations[i] whites_adjacency_list = generate_adjacency_list( white, layout, find_adjacent_squares) # break # Check if end is accessible if end in dfs(whites_adjacency_list, start): shortest_path = bfs_shortest_path(blacks, whites_adjacency_list, start, end) print_move_actions(white, shortest_path) print_boom(end[1], end[2]) # De-stack moved = ("W1", ) + tuple(trapped_whites[i][1:]) layout.append(moved) trapped_whites.append(moved) else: trapped_whites.append(white)
def main(): with open(sys.argv[1]) as file: data = json.load(file) board_dict = {} whites = data['white'] # array of arrays number_of_whites = 0 for piece in whites: number_of_whites += piece[0] print("number of whites: " + str(number_of_whites)) blacks = data['black'] # array of arrays place_pieces(board_dict, whites, blacks) killZone = set() find_killZones(board_dict, whites, blacks, killZone) place_pieces(board_dict, whites, blacks) print_board(board_dict) print(killZone) # find a combination of white positions within the killzone that when they explode they kill every black piece # results gives us the positions for white pieces possible_white_positions = list(combinations(killZone, number_of_whites)) for tupl in possible_white_positions: for piece in blacks: if ((piece[1], piece[2]) in tupl): possible_white_positions.remove(tupl) break combinations_success = set( ) # combinations of postiton that kill all blacks for tupl in possible_white_positions: if (kills_all_blacks(tupl, blacks)): combinations_success.add(tupl) print("combinations that killed all the black pieces:") print(combinations_success) print(combinations_success.issuperset({((1, 1), (1, 1), (1, 1))})) for e in combinations_success: for n in range(number_of_whites): board_dict[(e[n][0], e[n][1])] = "!" print_board(board_dict) # Finally, move whites to the optimum positions in the least amount of moves #1st find combination that is closest #2nd move peices there comb_dsitances = set() for ntuple in combinations_success: comb_dsitances.add(d_comb_whites(ntuple, whites, number_of_whites)) print(comb_dsitances) print(min(comb_dsitances)) mintuple = tuple() for ntuple in combinations_success: if (d_comb_whites(ntuple, whites, number_of_whites) == (min(comb_dsitances))): mintuple = ntuple print(mintuple) #which picece is closest to which point? #goal_dict = closest(mintuple, whites, number_of_whites) goal_dict = closest(mintuple, whites) #last step is to actually move the pices to their optimum position and BOOM! endconfig = list() for piece in whites: spos = (piece[1], piece[2]) endpos = goal_dict[(piece[1], piece[2])] # print("start position: {}, goal position: {}".format((piece[1],piece[2]),goal_dict[(piece[1],piece[2])])) # print(goal_dict[(piece[1],piece[2])][0]) while (not (spos == endpos)): if (spos[0] > endpos[0] and (not (board_dict.get({(spos[0] - 1, spos[1])})[0] == 'b'))): print_move(1, spos[0], spos[1], spos[0] - 1, spos[1]) spos = (spos[0] - 1, spos[1]) if (spos[0] < endpos[0]): print_move(1, spos[0], spos[1], spos[0] + 1, spos[1]) spos = (spos[0] + 1, spos[1]) if (spos[1] > endpos[1]): print_move(1, spos[0], spos[1], spos[0], spos[1] - 1) spos = (spos[0], spos[1] - 1) if (spos[1] < endpos[1]): print_move(1, spos[0], spos[1], spos[0], spos[1] + 1) spos = (spos[0], spos[1] + 1) endconfig.append(spos) for spos in endconfig: print_boom(spos[0], spos[1])