Skip to content

Commit a6db875

Browse files
author
Darrell Little
committed
Fix 'go' bug with nested directions dictionary
1 parent ddfced7 commit a6db875

File tree

1 file changed

+103
-76
lines changed

1 file changed

+103
-76
lines changed

Zombie-House/main.py

Lines changed: 103 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def showInstructions():
1616
Protect yourself against the Poisons with a potion
1717
1818
Commands:
19-
go [direction] Options -> north, south, east or west
20-
get [item] Options -> key, sword or potion
19+
go [direction] (Options -> north, south, east or west)
20+
get [item] (Options -> key, sword or potion)
21+
exit (Exit the game)
2122
2223
''')
2324

@@ -43,63 +44,81 @@ def showStatus():
4344
# Rooms can contain a monster or poison
4445
rooms = {
4546

46-
'Hall' : {
47+
'Hall' : {
48+
'directions' : {
4749
'south' : 'Kitchen',
4850
'east' : 'Dining Room',
4951
'north' : 'Atrium',
50-
'west' : 'Library',
51-
'item' : 'key'
52-
},
52+
'west' : 'Library'
53+
},
54+
'item' : 'key'
55+
},
5356

5457
'Kitchen' : {
58+
'directions' : {
5559
'north' : 'Hall',
56-
'east' : 'Pantry',
57-
'monster' : 'Zombie',
58-
'item' : 'flower'
59-
},
60+
'east' : 'Pantry'
61+
},
62+
'monster' : 'Zombie',
63+
'item' : 'flower'
64+
},
6065

6166
'Dining Room' : {
67+
'directions' : {
6268
'west' : 'Hall',
6369
'south' : 'Pantry',
64-
'north' : 'Bedroom',
65-
'item' : 'sword'
70+
'north' : 'Bedroom'
71+
},
72+
'item' : 'sword'
6673
},
6774

6875
'Atrium' : {
76+
'directions' : {
6977
'south' : 'Hall',
7078
'west' : 'Parlor',
7179
'east' : 'Bedroom',
7280
'north' : 'Garden'
81+
}
7382
},
7483

7584
'Library' : {
85+
'directions' : {
7686
'east' : 'Hall',
7787
'south' : 'Study',
78-
'north' : 'Parlor',
79-
'item' : 'potion'
88+
'north' : 'Parlor'
89+
},
90+
'item' : 'potion'
8091
},
8192

8293
'Study' : {
83-
'north' : 'Library',
84-
'poison' : 'hemlock'
94+
'directions' : {
95+
'north' : 'Library'
96+
},
97+
'poison' : 'hemlock'
8598
},
8699

87100
'Parlor' : {
101+
'directions' : {
88102
'south' : 'Library',
89-
'east' : 'Atrium',
90-
'monster' : 'Zombie'
103+
'east' : 'Atrium'
104+
},
105+
'monster' : 'Zombie'
91106
},
92107

93108
'Pantry' : {
109+
'directions' : {
94110
'west' : 'Kitchen',
95-
'north' : 'Dining Room',
96-
'monster' : 'Zombie'
111+
'north' : 'Dining Room'
112+
},
113+
'monster' : 'Zombie'
97114
},
98115

99116
'Bedroom' : {
117+
'directions' : {
100118
'south' : 'Dining Room',
101-
'west' : 'Atrium',
102-
'poison' : 'hemlock'
119+
'west' : 'Atrium'
120+
},
121+
'poison' : 'hemlock'
103122
},
104123

105124
'Garden' : {
@@ -115,61 +134,69 @@ def showStatus():
115134

116135
# loop forever
117136
while True:
118-
119-
showStatus()
120-
121-
# get the player's next 'move'
122-
# .split() breaks it up into an list array
123-
# eg typing 'go east' would give the list:
124-
# ['go','east']
125-
move = ''
126-
while move == '':
127-
move = input('>')
137+
try:
138+
139+
showStatus()
140+
141+
# get the player's next 'move'
142+
# .split() breaks it up into an list array
143+
# eg typing 'go east' would give the list:
144+
# ['go','east']
145+
move = ''
146+
while move == '':
147+
move = input('>')
128148

129-
move = move.lower().split()
130-
131-
# if they type 'go' first
132-
if move[0] == 'go':
133-
# check that they are allowed wherever they want to go
134-
if move[1] in rooms[currentRoom]:
135-
# set the current room to the new room
136-
currentRoom = rooms[currentRoom][move[1]]
137-
# there is no door (link) to the new room
138-
else:
139-
print('You can\'t go that way!')
140-
141-
# if they type 'get' first
142-
if move[0] == 'get' :
143-
# if the room contains an item, and the item is the one they want to get
144-
if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
145-
# add the item to their inventory
146-
inventory += [move[1]]
147-
# display a helpful message
148-
print(move[1] + ' picked up!')
149-
# delete the item from the room
150-
del rooms[currentRoom]['item']
151-
# otherwise, if the item isn't there to get
152-
else:
153-
# tell player they can't get it
154-
print('Can\'t get ' + move[1] + '!')
149+
move = move.lower().split()
150+
151+
# if they type 'go' first
152+
if move[0] == 'go':
153+
# check that they are allowed wherever they want to go
154+
if move[1] in rooms[currentRoom]['directions']:
155+
# set the current room to the new room
156+
currentRoom = rooms[currentRoom]['directions'][move[1]]
157+
# there is no door (link) to the new room
158+
else:
159+
print('You can\'t go that way!')
160+
161+
# if they type 'get' first
162+
if move[0] == 'get' :
163+
# if the room contains an item, and the item is the one they want to get
164+
if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
165+
# add the item to their inventory
166+
inventory += [move[1]]
167+
# display a helpful message
168+
print(move[1] + ' picked up!')
169+
# delete the item from the room
170+
del rooms[currentRoom]['item']
171+
# otherwise, if the item isn't there to get
172+
else:
173+
# tell player they can't get it
174+
print('Can\'t get ' + move[1] + '!')
175+
176+
# Allow command to exit the game
177+
if move[0] == 'exit':
178+
break
155179

156-
# Player loses game if they enter room with Zombie without a Sword
157-
if 'monster' in rooms[currentRoom] and 'Zombie' in rooms[currentRoom]['monster'] and 'sword' in inventory:
158-
print('You have defended yourself from the Zombie attack! ... Continue On')
159-
elif 'monster' in rooms[currentRoom] and 'Zombie' in rooms[currentRoom]['monster']:
160-
print('A Zombie has eaten your brainz. You need to defend yourself ... GAME OVER!!!')
161-
break
180+
# Player loses game if they enter room with Zombie without a Sword
181+
if 'monster' in rooms[currentRoom] and 'Zombie' in rooms[currentRoom]['monster'] and 'sword' in inventory:
182+
print('You have defended yourself from the Zombie attack! ... Continue On')
183+
elif 'monster' in rooms[currentRoom] and 'Zombie' in rooms[currentRoom]['monster']:
184+
print('A Zombie has eaten your brainz. You need to defend yourself ... GAME OVER!!!')
185+
break
162186

163-
# Player loses game if they enter room with poison without a potion
164-
if 'poison' in rooms[currentRoom] and 'hemlock' in rooms[currentRoom]['poison'] and 'potion' in inventory:
165-
print('The potion has protected you from the Poison! ... Continue On')
166-
elif 'poison' in rooms[currentRoom] and 'hemlock' in rooms[currentRoom]['poison']:
167-
print('The Poison Hemlock has killed you. A potion will protect you ... GAME OVER!!!')
168-
break
187+
# Player loses game if they enter room with poison without a potion
188+
if 'poison' in rooms[currentRoom] and 'hemlock' in rooms[currentRoom]['poison'] and 'potion' in inventory:
189+
print('The potion has protected you from the Poison! ... Continue On')
190+
elif 'poison' in rooms[currentRoom] and 'hemlock' in rooms[currentRoom]['poison']:
191+
print('The Poison Hemlock has killed you. A potion will protect you ... GAME OVER!!!')
192+
break
169193

170-
# Player wins game if they get to the Garden with the key and magic potion
171-
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
172-
print('You have escaped the house with the Key and Magic Potion ... YOU WIN!!!')
173-
break
174-
elif currentRoom == 'Garden':
175-
print('To escape you need the key and potion ... Keep Looking')
194+
# Player wins game if they get to the Garden with the key and magic potion
195+
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
196+
print('You have escaped the house with the Key and Magic Potion ... YOU WIN!!!')
197+
break
198+
elif currentRoom == 'Garden':
199+
print('To escape you need the key and potion ... Keep Looking')
200+
201+
except KeyboardInterrupt:
202+
break

0 commit comments

Comments
 (0)