Skip to content

Commit a6d7504

Browse files
committed
Sending game in py file
1 parent c52ce77 commit a6d7504

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed

your-code/sample-code.py

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
2+
3+
door_d = {
4+
"name": "door d",
5+
"type": "door",
6+
}
7+
couch = {
8+
"name": "couch",
9+
"type": "furniture",
10+
}
11+
12+
door_a = {
13+
"name": "door a",
14+
"type": "door",
15+
}
16+
17+
key_a = {
18+
"name": "key for door a",
19+
"type": "key",
20+
"target": door_a,
21+
}
22+
23+
piano = {
24+
"name": "piano",
25+
"type": "furniture",
26+
}
27+
28+
game_room = {
29+
"name": "game room",
30+
"type": "room",
31+
}
32+
33+
outside = {
34+
"name": "outside"
35+
}
36+
37+
#DEFINE BEDROOM1
38+
39+
bedroom_1 = {
40+
"name": "bedroom 1",
41+
"type": "room",
42+
}
43+
44+
door_b = {
45+
"name": "door b",
46+
"type": "door",
47+
}
48+
door_c = {
49+
"name": "door c",
50+
"type": "door",
51+
}
52+
53+
queen_bed = {
54+
"name": "queen bed",
55+
"type": "furniture",
56+
}
57+
key_b={
58+
"name": "key for door b",
59+
"type": "key",
60+
"target": door_b
61+
}
62+
63+
64+
65+
#DEFINE BEDROOM2
66+
67+
key_c = {
68+
"name": "key for door c",
69+
"type": "key",
70+
"target": door_c,
71+
}
72+
key_d = {
73+
"name": "key for door d",
74+
"type": "key",
75+
"target": door_d
76+
}
77+
double_bed = {
78+
"name": "double bed",
79+
"type": "furniture"
80+
}
81+
dresser = {
82+
"name": "dresser",
83+
"type": "furniture"
84+
}
85+
bedroom_2 = {
86+
"name": "bedroom 2",
87+
"type": "room",
88+
}
89+
90+
91+
92+
# Define LIVINGROOM
93+
94+
95+
dining_table = {
96+
"name": "dining table",
97+
"type": "furniture",
98+
}
99+
100+
101+
102+
key_d = {
103+
"name": "key for door d",
104+
"type": "key",
105+
"target": door_d
106+
}
107+
108+
109+
living_room = {
110+
"name": "living room",
111+
"type": "room",
112+
}
113+
114+
outside = {
115+
"name": "outside"
116+
}
117+
118+
all_rooms = [game_room, bedroom_1, bedroom_2, living_room, outside]
119+
120+
all_doors = [door_a, door_b, door_c, door_c]
121+
122+
# Define which items/rooms are related
123+
124+
object_relations = {
125+
"game room": [couch, piano, door_a],
126+
"piano": [key_a],
127+
"outside": [door_a],
128+
"door a": [game_room, bedroom_1],
129+
"queen bed":[key_b],
130+
"door b":[bedroom_1, bedroom_2],
131+
"door c":[bedroom_1, living_room],
132+
"double bed":[key_c],
133+
"dresser": [key_d],
134+
"dinning table":[living_room],
135+
"door d":[living_room, outside],
136+
"bedroom 1":[queen_bed,door_a,door_b,door_c],
137+
"bedroom 2":[double_bed,door_c,door_d,dresser,door_b],
138+
"living room":[dining_table, door_c, door_d]
139+
140+
}
141+
142+
143+
144+
145+
# define game state. Do not directly change this dict.
146+
# Instead, when a new game starts, make a copy of this
147+
# dict and use the copy to store gameplay state. This
148+
# way you can replay the game multiple times.
149+
150+
INIT_GAME_STATE = {
151+
"current_room": game_room,
152+
"keys_collected": [],
153+
"target_room": outside
154+
}
155+
156+
def linebreak():
157+
"""
158+
Print a line break
159+
"""
160+
print("\n\n")
161+
162+
def start_game():
163+
"""
164+
Start the game
165+
"""
166+
print(""" ************Welcome to Hogwards Escape Room*************
167+
168+
Welcome to our new Hogwarts Escape Room! Before we begin our journey, we would like to say a few words...
169+
170+
Choose wisely your paths and remember your spells...
171+
""")
172+
play_room(game_state["current_room"])
173+
174+
def play_room(room):
175+
"""
176+
Play a room. First check if the room being played is the target room.
177+
If it is, the game will end with success. Otherwise, let player either
178+
explore (list all items in this room) or examine an item found here.
179+
"""
180+
game_state["current_room"] = room
181+
if(game_state["current_room"] == game_state["target_room"]):
182+
print("Congrats! You escaped the room!")
183+
else:
184+
# if room["name"] == game_state["current_room"]:
185+
# print("\n You are still in: " + room["name"])
186+
# else:
187+
print("\n Let's proceed! You are now in: " + room["name"])
188+
#intended_action = input("What would you like to do? Type 'explore' or 'examine'?").strip()
189+
choice = input("""Choose one of these options:
190+
191+
A: Explore what's in the room
192+
B: Examine what you want to
193+
194+
195+
Please enter your choice: """)
196+
if choice.upper() == "A":
197+
explore_room(room)
198+
play_room(room)
199+
elif choice.upper() == "B":
200+
examine_item(input(" \n What would you like to examine? \n").strip())
201+
else:
202+
print("\n That is a muggle error! You must only type either A or B.")
203+
play_room(room)
204+
linebreak()
205+
206+
def explore_room(room):
207+
"""
208+
Explore a room. List all items belonging to this room.
209+
"""
210+
items = [i["name"] for i in object_relations[room["name"]]]
211+
print("\n You explore the room. This is " + room["name"] + ". You find these objects: \n- " + "\n- ".join(items))
212+
213+
def get_next_room_of_door(door, current_room):
214+
"""
215+
From object_relations, find the two rooms connected to the given door.
216+
Return the room that is not the current_room.
217+
"""
218+
connected_rooms = object_relations[door["name"]]
219+
for room in connected_rooms:
220+
if(not current_room == room):
221+
return room
222+
223+
def examine_item(item_name):
224+
"""
225+
Examine an item which can be a door or furniture.
226+
First make sure the intended item belongs to the current room.
227+
Then check if the item is a door. Tell player if key hasn't been
228+
collected yet. Otherwise ask player if they want to go to the next
229+
room. If the item is not a door, then check if it contains keys.
230+
Collect the key if found and update the game state. At the end,
231+
play either the current or the next room depending on the game state
232+
to keep playing.
233+
"""
234+
current_room = game_state["current_room"]
235+
next_room = ""
236+
output = None
237+
238+
for item in object_relations[current_room["name"]]:
239+
if(item["name"] == item_name):
240+
output = "\n You examine " + item_name + ". "
241+
if(item["type"] == "door"):
242+
have_key = False
243+
for key in game_state["keys_collected"]:
244+
if(key["target"] == item):
245+
have_key = True
246+
if(have_key):
247+
output += "You unlock it with a key you have."
248+
next_room = get_next_room_of_door(item, current_room)
249+
else:
250+
output += "It is locked but you don't have the key."
251+
else:
252+
if(item["name"] in object_relations and len(object_relations[item["name"]])>0):
253+
item_found = object_relations[item["name"]].pop()
254+
game_state["keys_collected"].append(item_found)
255+
output += "You find " + item_found["name"] + "."
256+
else:
257+
output += "There isn't anything interesting about it."
258+
259+
260+
print(output)
261+
see_again= input(("""Do you wish to examine more things?
262+
263+
Y: To examine more things
264+
N: Stop examine
265+
266+
267+
Please enter your choice: """))
268+
if see_again.upper() == 'Y':
269+
examine_item(input(" \n What would you like to examine? \n").strip())
270+
elif see_again.upper() == 'N':
271+
continue
272+
else:
273+
see_again
274+
break
275+
276+
if(output is None):
277+
print("You must have eaten some Bertie Botts Beansb and are seeing things twisted. That item isn't in the current room.")
278+
279+
if(next_room and input("Do you wish to go to another Hogwards room? Write 'yes' to enter or write anything else to go to main menu.").strip().lower() == 'yes'):
280+
play_room(next_room)
281+
else:
282+
play_room(current_room)
283+
284+
285+
game_state = INIT_GAME_STATE.copy()
286+
287+
start_game()

0 commit comments

Comments
 (0)