Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. import networkx as nx
  2.  
  3. class GameLogic(object):
  4.  
  5.     def __init__(self):
  6.         self.player_one = PlayerOne()
  7.         self.player_two = PlayerTwo()
  8.  
  9.     def find_path(graph, start_node, end_node, path=[]):
  10.         path = path + [start_node]
  11.         if start_node == end_node:
  12.             return path
  13.         if start_node not in graph:
  14.             return None
  15.         for node in graph[start_node]:
  16.             if node not in graph:
  17.                 new_path = find_path(graph, node, end_node, path)
  18.                 if new_path:
  19.                     return new_path
  20.         return None
  21.  
  22.     def find_all_paths(graph, start_node, end_node, path=[]):
  23.         path = path + [start_node]
  24.         if start_node == end_node:
  25.             return path
  26.         if start_node not in graph:
  27.             return []
  28.         paths = []
  29.         for node in graph[start_node]:
  30.             if node not in graph:
  31.                 new_paths = find_all_paths(graph, node, end_node, path)
  32.                 for new_path in new_paths:
  33.                     paths.append(new_path)
  34.         return paths
  35.  
  36.     def find_shortest_path(graph, start_node, end_node, path=[]):
  37.         path = path + [start_node]
  38.         if start_node == end_node:
  39.             return path
  40.         if start_node not in graph:
  41.             return None
  42.         shortest = None
  43.         for node in graph[start_node]:
  44.             if node not in graph:
  45.                 new_path = find_shortest_path(graph, node, end_node, path)
  46.                 if new_path:
  47.                     if not shortest or len(new_path) < len(shortest):
  48.                         shortest = new_path
  49.         return shortest
  50.    
  51.     # sets the node attribute conquered to all nodes in the graph and returns the new graph
  52.     def set_node_attribute_conquered(graph, node):
  53.         for node in graph:
  54.             nx.set_node_attributes(graph, 'conquered', 'false')
  55.         return graph
  56.  
  57.     def set_player_one(graph, node_nbr, player):
  58.         graph.node[node_nbr]['conquered'] = 'true'
  59.         graph.node[node_nbr]['player'] = self.player_one ## not sure if this works
  60.  
  61.     def set_player_two(graph, node_nbr, player):
  62.         graph.node[node_nbr]['conquered'] = 'true'
  63.         graph.node[node_nbr]['player'] = self.player_two
  64.  
  65.     def move_to_next_node(graph, curr_node, end_node):
  66.         list_of_path = find_shortest_path(graph, curr_node, end_node)
  67.         for path in list_of_path:
  68.             #TODO -- may need help on this one!
  69.             # graph.neighbors(curr_node) returns a list of neighbors, this could be useful
  70.  
  71.  
  72.  
  73. class PlayerOne():
  74.  
  75.     def __init__(self):
  76.         self.player_name = 'Hannah'
  77.         self.player_color = 'red'
  78.         self.player_troops = '0'
  79.  
  80. class PlayerTwo():
  81.  
  82.     def __init__(self):
  83.         self.player_name = 'Anna'
  84.         self.player_color = 'blue'
  85.         self.player_troops = '0'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement