-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze Project (graphs)
More file actions
98 lines (82 loc) · 2.98 KB
/
Maze Project (graphs)
File metadata and controls
98 lines (82 loc) · 2.98 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Vertex file
class Vertex:
def __init__(self, value):
self.value = value
self.edges = {}
def add_edge(self, adjacent_value, weight = 0):
self.edges[adjacent_value] = weight
def get_edges(self):
return self.edges.keys()
#Graph file
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")
#FILL IN 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[:1]
print("enter {0} for {1}: {2} cost".format(key,connected_room,weight))
valid_choices = [room[:1] for room in node.edges.keys()]
print("\nYou have accumulated: {0} cost".format(path_total))
choice = input("\nWhich room do you move to?")
if choice !(in) valid_choices:
print("please select from these letters: {0}".format(valid_choices))
else:
for room in node.edges.keys():
if room.startswith(choice):
current_room = room
path_total+=node.edges.[room]
print("\n*** You have chosen: {0} ***\n".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()
# MAKE ROOMS INTO VERTICES BELOW...
entrance = Vertex("entrance")
ante_chamber = Vertex("ante-chamber")
kings_room = Vertex("king's room")
grand_gallery = Vertex("grand gallery")
treasure_room = Vertex("treasure room")
# ADD 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)
# ADD EDGES BETWEEN ROOMS BELOW...
graph.add_edge(entrance,ante_chamber,7)
graph.add_edge(entrance,kings_room,3)
graph.add_edge(kings_room,ante_chamber,1)
graph.add_edge(grand_gallery,ante_chamber,2)
graph.add_edge(grand_gallery,kings_room,2)
graph.add_edge(treasure_room,ante_chamber,6)
graph.add_edge(treasure_room,grand_gallery,4)
# DON'T CHANGE THIS CODE
graph.print_map()
return graph
#Script file
# import classes
from graph import Graph, build_graph
from vertex import Vertex
excavation_site = build_graph()
excavation_site.explore()