-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
72 lines (66 loc) · 2.57 KB
/
graph.py
File metadata and controls
72 lines (66 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from vertex import Vertex
class Graph:
def __init__(self):
self.graph_dict = {}
def add_vertex(self, node):
self.graph_dict[node.value] = node
def add_edge(self, from_node, to_node, weight = 0):
self.graph_dict[from_node.value].add_edge(to_node.value, weight)
self.graph_dict[to_node.value].add_edge(from_node.value, weight)
def explore(self):
print("Exploring the graph....\n")
#EXPLORE METHOD BELOW
current_room = 'entrance'
path_total = 0
print("\nStarting off at the {0}\n".format(current_room))
while (current_room != 'treasure room'):
node = self.graph_dict[current_room]
for connected_room, weight in node.edges.items():
key = connected_room[0]
print("Enter {0} for {1}: {2} cost".format(key, connected_room, weight))
valid_choices = [room [0] for room in node.edges.keys()]
print("\nYou have accumalted: {0} cost".format(path_total))
choice = input("\n Which room do you move to? ")
if choice not in valid_choices:
print("Please select from these letters: {0} ")
else:
for room in node.edges.keys():
if choice == room[0]:
current_room = room
path_total += node.edges[room]
print("\nYou have chosen: {0} ".format(current_room))
print("Made it to the treasure room with {0} cost ".format(path_total))
def print_map(self):
print("\nMAZE LAYOUT\n")
for node_key in self.graph_dict:
print("{0} connected to...".format(node_key))
node = self.graph_dict[node_key]
for adjacent_node, weight in node.edges.items():
print("=> {0}: cost is {1}".format(adjacent_node, weight))
print("")
print("")
def build_graph():
graph = Graph()
# MAKING ROOMS INTO VERTICES BELOW...
entrance = Vertex('entrance')
ante_chamber = Vertex('ante-chamber')
kings_room = Vertex('kings room')
grand_gallery = Vertex('grand gallery')
treasure_room = Vertex('treasure room')
# ADDING ROOMS TO GRAPH BELOW...
graph.add_vertex(entrance)
graph.add_vertex(ante_chamber)
graph.add_vertex(kings_room)
graph.add_vertex(grand_gallery)
graph.add_vertex(treasure_room)
# ADDING EDGES BETWEEN ROOMS BELOW...
graph.add_edge(entrance, ante_chamber, 7)
graph.add_edge(entrance, kings_room, 3)
graph.add_edge(ante_chamber, kings_room, 1)
graph.add_edge(ante_chamber, grand_gallery, 2)
graph.add_edge(kings_room, grand_gallery, 2)
graph.add_edge(treasure_room, grand_gallery, 4)
graph.add_edge(treasure_room, ante_chamber, 6)
# DON'T CHANGE THIS CODE!!!
graph.print_map()
return graph