Skip to content

Commit 1a0c7df

Browse files
authored
Create Maze Project (graphs)
1 parent df439d3 commit 1a0c7df

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Maze Project (graphs)

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#Vertex file
2+
3+
class Vertex:
4+
def __init__(self, value):
5+
self.value = value
6+
self.edges = {}
7+
8+
def add_edge(self, adjacent_value, weight = 0):
9+
self.edges[adjacent_value] = weight
10+
11+
def get_edges(self):
12+
return self.edges.keys()
13+
14+
15+
#Graph file
16+
17+
from vertex import Vertex
18+
19+
class Graph:
20+
def __init__(self):
21+
self.graph_dict = {}
22+
23+
def add_vertex(self, node):
24+
self.graph_dict[node.value] = node
25+
26+
def add_edge(self, from_node, to_node, weight = 0):
27+
self.graph_dict[from_node.value].add_edge(to_node.value, weight)
28+
self.graph_dict[to_node.value].add_edge(from_node.value, weight)
29+
30+
def explore(self):
31+
print("Exploring the graph....\n")
32+
#FILL IN EXPLORE METHOD BELOW
33+
current_room = 'entrance'
34+
path_total = 0
35+
print("\nStarting off at the {0}\n".format(current_room))
36+
while current_room != 'treasure room':
37+
node = self.graph_dict[current_room]
38+
for connected_room,weight in node.edges.items():
39+
key = connected_room[:1]
40+
print("enter {0} for {1}: {2} cost".format(key,connected_room,weight))
41+
valid_choices = [room[:1] for room in node.edges.keys()]
42+
print("\nYou have accumulated: {0} cost".format(path_total))
43+
choice = input("\nWhich room do you move to?")
44+
if choice !(in) valid_choices:
45+
print("please select from these letters: {0}".format(valid_choices))
46+
else:
47+
for room in node.edges.keys():
48+
if room.startswith(choice):
49+
current_room = room
50+
path_total+=node.edges.[room]
51+
print("\n*** You have chosen: {0} ***\n".format(current_room))
52+
print("Made it to the treasure room with {0} cost".format(path_total))
53+
54+
def print_map(self):
55+
print("\nMAZE LAYOUT\n")
56+
for node_key in self.graph_dict:
57+
print("{0} connected to...".format(node_key))
58+
node = self.graph_dict[node_key]
59+
for adjacent_node, weight in node.edges.items():
60+
print("=> {0}: cost is {1}".format(adjacent_node, weight))
61+
print("")
62+
print("")
63+
64+
def build_graph():
65+
graph = Graph()
66+
67+
# MAKE ROOMS INTO VERTICES BELOW...
68+
entrance = Vertex("entrance")
69+
ante_chamber = Vertex("ante-chamber")
70+
kings_room = Vertex("king's room")
71+
grand_gallery = Vertex("grand gallery")
72+
treasure_room = Vertex("treasure room")
73+
# ADD ROOMS TO GRAPH BELOW...
74+
graph.add_vertex(entrance)
75+
graph.add_vertex(ante_chamber)
76+
graph.add_vertex(kings_room)
77+
graph.add_vertex(grand_gallery)
78+
graph.add_vertex(treasure_room)
79+
# ADD EDGES BETWEEN ROOMS BELOW...
80+
graph.add_edge(entrance,ante_chamber,7)
81+
graph.add_edge(entrance,kings_room,3)
82+
graph.add_edge(kings_room,ante_chamber,1)
83+
graph.add_edge(grand_gallery,ante_chamber,2)
84+
graph.add_edge(grand_gallery,kings_room,2)
85+
graph.add_edge(treasure_room,ante_chamber,6)
86+
graph.add_edge(treasure_room,grand_gallery,4)
87+
# DON'T CHANGE THIS CODE
88+
graph.print_map()
89+
return graph
90+
91+
92+
#Script file
93+
94+
# import classes
95+
from graph import Graph, build_graph
96+
from vertex import Vertex
97+
excavation_site = build_graph()
98+
excavation_site.explore()

0 commit comments

Comments
 (0)