-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
364 lines (299 loc) · 11.9 KB
/
game.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import os
import time
from map import rooms
from player import *
from items import *
from gameparser import *
def list_of_items(items):
"""This function takes a list of items (see items.py for the definition) and
returns a comma-separated list of item names (as a string). For example:
>>> list_of_items([item_pen, item_handbook])
'a pen, a student handbook'
>>> list_of_items([item_id])
'id card'
>>> list_of_items([])
''
>>> list_of_items([item_money, item_handbook, item_laptop])
'money, a student handbook, laptop'
"""
list = ""
for item in items:
if (list == ""):
list = list + str(item["name"])
else:
list = list + ", " + str(item["name"])
return list
def print_room_items(room):
"""This function takes a room as an input and nicely displays a list of items
found in this room (followed by a blank line). If there are no items in
the room, nothing is printed. See map.py for the definition of a room, and
items.py for the definition of an item. This function uses list_of_items()
to produce a comma-separated list of item names. For example:
>>> print_room_items(rooms["Reception"])
There is a pack of biscuits, a student handbook here.
<BLANKLINE>
>>> print_room_items(rooms["Office"])
There is a pen here.
<BLANKLINE>
>>> print_room_items(rooms["Admins"])
(no output)
Note: <BLANKLINE> here means that doctest should expect a blank line.
"""
item_list = list_of_items(room["items"])
if (item_list != ""):
print("There is " + item_list + " here.")
print()
def print_inventory_items(items):
"""This function takes a list of inventory items and displays it nicely, in a
manner similar to print_room_items(). The only difference is in formatting:
print "You have ..." instead of "There is ... here.". For example:
>>> print_inventory_items(inventory)
You have id card, laptop, money.
<BLANKLINE>
"""
item_list = list_of_items(inventory)
if (item_list != ""):
print("You have " + item_list + ".")
print()
def print_room(room):
"""This function takes a room as an input and nicely displays its name
and description. The room argument is a dictionary with entries "name",
"description" etc. (see map.py for the definition). The name of the room
is printed in all capitals and framed by blank lines. Then follows the
description of the room and a blank line again. If there are any items
in the room, the list of items is printed next followed by a blank line
(use print_room_items() for this). For example:
>>> print_room(rooms["Office"])
<BLANKLINE>
THE GENERAL OFFICE
<BLANKLINE>
You are standing next to the cashier's till at
30-36 Newport Road. The cashier looks at you with hope
in their eyes. If you go west you can return to the
Queen's Buildings.
<BLANKLINE>
There is a pen here.
<BLANKLINE>
>>> print_room(rooms["Reception"])
<BLANKLINE>
RECEPTION
<BLANKLINE>
You are in a maze of twisty little passages, all alike.
Next to you is the School of Computer Science and
Informatics reception. The receptionist, Matt Strangis,
seems to be playing an old school text-based adventure
game on his computer. There are corridors leading to the
south and east. The exit is to the west.
<BLANKLINE>
There is a pack of biscuits, a student handbook here.
<BLANKLINE>
>>> print_room(rooms["Admins"])
<BLANKLINE>
MJ AND SIMON'S ROOM
<BLANKLINE>
You are leaning agains the door of the systems managers'
room. Inside you notice Matt "MJ" John and Simon Jones. They
ignore you. To the north is the reception.
<BLANKLINE>
Note: <BLANKLINE> here means that doctest should expect a blank line.
"""
# Display room name
print()
print(room["name"].upper())
print()
# Display room description
print(room["description"])
print()
if (room["items"] != []):
print_room_items(room)
def exit_leads_to(exits, direction):
"""This function takes a dictionary of exits and a direction (a particular
exit taken from this dictionary). It returns the name of the room into which
this exit leads. For example:
>>> exit_leads_to(rooms["Reception"]["exits"], "south")
"MJ and Simon's room"
>>> exit_leads_to(rooms["Reception"]["exits"], "east")
"your personal tutor's office"
>>> exit_leads_to(rooms["Tutor"]["exits"], "west")
'Reception'
"""
return rooms[exits[direction]]["name"]
def print_exit(direction, leads_to):
"""This function prints a line of a menu of exits. It takes a direction (the
name of an exit) and the name of the room into which it leads (leads_to),
and should print a menu line in the following format:
GO <EXIT NAME UPPERCASE> to <where it leads>.
For example:
>>> print_exit("east", "you personal tutor's office")
GO EAST to you personal tutor's office.
>>> print_exit("south", "MJ and Simon's room")
GO SOUTH to MJ and Simon's room.
"""
print("GO " + direction.upper() + " to " + leads_to + ".")
def print_menu(exits, room_items, inv_items):
"""This function displays the menu of available actions to the player. The
argument exits is a dictionary of exits as exemplified in map.py. The
arguments room_items and inv_items are the items lying around in the room
and carried by the player respectively. The menu should, for each exit,
call the function print_exit() to print the information about each exit in
the appropriate format. The room into which an exit leads is obtained
using the function exit_leads_to(). Then, it should print a list of commands
related to items: for each item in the room print
"TAKE <ITEM ID> to take <item name>."
and for each item in the inventory print
"DROP <ITEM ID> to drop <item name>."
For example, the menu of actions available at the Reception may look like this:
You can:
GO EAST to your personal tutor's office.
GO WEST to the parking lot.
GO SOUTH to MJ and Simon's room.
TAKE BISCUITS to take a pack of biscuits.
TAKE HANDBOOK to take a student handbook.
DROP ID to drop your id card.
DROP LAPTOP to drop your laptop.
DROP MONEY to drop your money.
What do you want to do?
"""
print("You can:")
# Iterate over available exits
for direction in exits:
# Print the exit name and where it leads to
print_exit(direction, exit_leads_to(exits, direction))
for item in room_items:
# Print the items in the room
print("TAKE " + item["id"].upper() + " to take " + item["name"])
for item in inv_items:
print("DROP " + item["id"].upper() + " to drop " + item["name"])
print("What do you want to do?")
def is_valid_exit(exits, chosen_exit):
"""This function checks, given a dictionary "exits" (see map.py) and
a players's choice "chosen_exit" whether the player has chosen a valid exit.
It returns True if the exit is valid, and False otherwise. Assume that
the name of the exit has been normalised by the function normalise_input().
For example:
>>> is_valid_exit(rooms["Reception"]["exits"], "south")
True
>>> is_valid_exit(rooms["Reception"]["exits"], "up")
False
>>> is_valid_exit(rooms["Parking"]["exits"], "west")
False
>>> is_valid_exit(rooms["Parking"]["exits"], "east")
True
"""
return chosen_exit in exits
def execute_go(direction):
"""This function, given the direction (e.g. "south") updates the current room
to reflect the movement of the player if the direction is a valid exit
(and prints the name of the room into which the player is
moving). Otherwise, it prints "You cannot go there."
"""
global current_room
if direction in current_room["exits"]:
current_room = move(current_room["exits"], direction)
print(current_room["name"].upper())
else:
print("You cannot go there.")
def execute_take(item_id):
"""This function takes an item_id as an argument and moves this item from the
list of items in the current room to the player's inventory. However, if
there is no such item in the room, this function prints
"You cannot take that."
"""
took_item = False
for item in current_room["items"]:
if (item["id"] == item_id):
inventory.append(item)
current_room["items"].remove(item)
print ("Took " + item_id + ".")
took_item = True
break
if took_item == False:
print ("You cannot take that.")
def execute_drop(item_id):
"""This function takes an item_id as an argument and moves this item from the
player's inventory to list of items in the current room. However, if there is
no such item in the inventory, this function prints "You cannot drop that."
"""
has_item = False
for item in inventory:
if item["id"] == item_id:
has_item = True
current_room["items"].append(item)
inventory.remove(item)
print ("Dropped " + item_id + ".")
break
if has_item == False:
print ("You cannot drop that.")
def execute_command(command):
"""This function takes a command (a list of words as returned by
normalise_input) and, depending on the type of action (the first word of
the command: "go", "take", or "drop"), executes either execute_go,
execute_take, or execute_drop, supplying the second word as the argument.
"""
if 0 == len(command):
return
if command[0] == "go":
if len(command) > 1:
execute_go(command[1])
else:
print("Go where?")
elif command[0] == "take":
if len(command) > 1:
execute_take(command[1])
else:
print("Take what?")
elif command[0] == "drop":
if len(command) > 1:
execute_drop(command[1])
else:
print("Drop what?")
else:
print("This makes no sense.")
def menu(exits, room_items, inv_items):
"""This function, given a dictionary of possible exits from a room, and a list
of items found in the room and carried by the player, prints the menu of
actions using print_menu() function. It then prompts the player to type an
action. The players's input is normalised using the normalise_input()
function before being returned.
"""
# Display menu
print_menu(exits, room_items, inv_items)
#print (current_room)
# Read player's input
user_input = input("> ")
# Normalise the input
normalised_user_input = normalise_input(user_input)
return normalised_user_input
def move(exits, direction):
"""This function returns the room into which the player will move if, from a
dictionary "exits" of avaiable exits, they choose to move towards the exit
with the name given by "direction". For example:
>>> move(rooms["Reception"]["exits"], "south") == rooms["Admins"]
True
>>> move(rooms["Reception"]["exits"], "east") == rooms["Tutor"]
True
>>> move(rooms["Reception"]["exits"], "west") == rooms["Office"]
False
"""
# Next room to go to
return rooms[exits[direction]]
# This is the entry point of our program
def main():
print("""'python' is not recognized as an internal or external command,
operable program or batch file.""")
time.sleep(4)
print("lol jk")
time.sleep(1)
clear = lambda: os.system('cls')
clear()
# Main game loop
while True:
# Display game status (room description, inventory etc.)
print_room(current_room)
print_inventory_items(inventory)
# Show the menu with possible actions and ask the player
command = menu(current_room["exits"], current_room["items"], inventory)
# Execute the player's command
execute_command(command)
if __name__ == "__main__":
main()