Esempio n. 1
0
 def single_source_shortest_path(self, player, avoid):
     """
         avoid ([nodes]): nodes to temporarily avoid, (y,x)
     """
     temp_edge_storage = []
     dbout("AVOID LIST:")
     dbout(avoid)
     temp_edge_storage = []
     paths = []
     for node in avoid:
         neighbors = list(nx.all_neighbors(self.G, node))
         for neighbor in neighbors:
             self.G.remove_edge(node, neighbor)
         temp_edge_storage.extend([(node, neighbor) for neighbor in neighbors])
     try:
         paths = nx.single_source_shortest_path(self.G, (player.y, player.x), cutoff=10)
         paths = list(paths.values())
         del paths[0]            
     except:
         dbout("EXCEPTION: UNABLE TO FIND SHORTEST PATH")
         for edge in temp_edge_storage:
             self.G.add_edge(edge[0], edge[1])
         raise Exception("Except")
     for edge in temp_edge_storage:
         self.G.add_edge(edge[0], edge[1])
     dbout("PATH FROM PLAYER TO TARGET GIVEN AVOID LIST:")
     dbout(paths)
     return paths
Esempio n. 2
0
 def path_to_enemy(self, gameboard, player, opponent, avoid):
     end_nodes = list(nx.all_neighbors(self.G, (opponent.y, opponent.x)))
     opp_front_node = None
     if opponent.direction == Direction.UP:
         opp_front_node = (opponent.y-1, opponent.x)
     if opponent.direction == Direction.DOWN:
         opp_front_node = (opponent.y+1, opponent.x)
     if opponent.direction == Direction.RIGHT:
         opp_front_node = (opponent.y, opponent.x+1)
     if opponent.direction == Direction.LEFT:
         opp_front_node = (opponent.y, opponent.x-1)
     if opp_front_node in end_nodes:
         end_nodes.remove(opp_front_node)
     dbout((player.y, player.x))
     for end_node in end_nodes:
         dbout(end_node)
     paths = [self.get_shortest_path(player, GameObject(end_node[1], end_node[0]), avoid) for end_node in end_nodes]
     if len(paths)==0:
         return []
     return paths[self.argmin([len(path) for path in paths])]