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 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 print_move_actions(white, path): """Print move actions from a given path to standard output :param path: list of (x, y) :type path: list """ for i in range(len(path) - 1): n = 1 if (abs(path[i][1] - path[i + 1][1]) + abs(path[i][2] - path[i + 1][2])) > 1: n = int(white[0][1:]) - 1 print_move(n, path[i][1], path[i][2], path[i + 1][1], path[i + 1][2])
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 an action to stdout according to the format instructions.""" print_move(self.n, *self.a, *self.b, **kwargs)
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])