-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex35c.py
More file actions
244 lines (182 loc) · 6.15 KB
/
Copy pathex35c.py
File metadata and controls
244 lines (182 loc) · 6.15 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
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
# Classes file
from random import choice
def text_list(listtext, sep1=", ", sep2=", and "):
n = len(listtext)
if n > 1:
return sep1.join(listtext[:-1]) + sep2 + listtext[-1]
elif n == 1:
return listtext[0]
else:
return "nothing!"
class Dungeon(object):
def __init__(self, x, y):
self.rooms = []
self.cur_x = 0
self.cur_y = 0
for i in range(0, x):
self.rooms.append([])
for j in range(0, y):
self.rooms[i].append(None)
def add_room(self, x, y, room):
self.rooms[x][y] = room
def north(self, args=[]):
self.go(['north'])
def south(self, args=[]):
self.go(['south'])
def east(self, args=[]):
self.go(['east'])
def west(self, args=[]):
self.go(['west'])
def go(self, args):
direction = args[0]
x = self.cur_x
y = self.cur_y
if direction == "north":
if y == len(self.rooms[x]) - 1:
print "You can't go that way."
else:
y += 1
elif direction == "south":
if y == 0:
print "You can't go that way"
else:
y -= 1
elif direction == "east":
if x == len(self.rooms) - 1:
print "You can't go that way."
else:
x += 1
elif direction == "west":
if x == 0:
print "You can't go that way"
else:
x -= 1
else:
print "I don't know what that means."
if self.rooms[x][y]:
self.cur_x = x
self.cur_y = y
self.room().enter()
else:
print "You can't go that way."
return self.room()
def room(self):
return self.rooms[self.cur_x][self.cur_y]
class Room(object):
def __init__(self, name="Kitchen"):
self.name = name
self.__inventory = {}
def enter(self, args=[]):
print "You are in the %s" % self.name
def look(self, args=[]):
self.enter()
if self.__inventory:
print "You see %s lying on the floor" \
% text_list(self.__inventory.values())
else:
return
def fight(self, args=[]):
print "You take a swing but there is nothing to fight"
def add_item(self, item_name, item_description):
self.__inventory[item_name] = item_description
def take_item(self, item_name):
if item_name in self.__inventory:
item_description = self.__inventory[item_name]
del self.__inventory[item_name]
return item_description
return None
class MonsterRoom(Room):
def __init__(self, monster, name="Monster Room"):
self.monster = monster
super(MonsterRoom, self).__init__(name)
def enter(self, args=[]):
super(MonsterRoom, self).enter()
if self.monster:
print "There is a %s here!" % self.monster
def fight(self, args=[]):
if not self.monster:
print "There is nothing to fight here."
return
if choice([True, False]):
print "You win!"
self.monster = None
else:
print "You lose!"
print "That was a ridiculously short game."
user_command = raw_input("Play again? ").lower()
if user_command == "yes":
start()
else:
exit(0)
class Character(object):
def __init__(self, c_name):
self.name = c_name
self.__inventory = \
{"hat": "a knit hat",
"mittens": "a pair of mittens", "key": "a brass key"}
self.__dungeon = None
def inventory(self, args=[]):
print "You have %s" % text_list(self.__inventory.values())
def enter_dungeon(self, dungeon):
print "Welcome %s! You find yourself standing in a strange " \
"%s." % (self.name, dungeon.room().name)
self.__dungeon = dungeon
def drop(self, args=[]):
item_name = args[0]
if item_name in self.__inventory:
item_description = self.__inventory[item_name]
print "You have dropped the %s." % item_name
self.__dungeon.room().add_item(item_name, item_description)
del self.__inventory[item_name]
else:
print "You don't have that."
def get(self, args=[]):
item_name = args[0]
item_description = self.__dungeon.room().take_item(item_name)
if item_description:
print "You picked up %s." % item_description
self.__inventory[item_name] = item_description
else:
print "I don't see %s." % item_name
class Adventurer(Character):
def __init__(self, a_name):
if not a_name:
print "No name huh? I guess you will be " \
"called Emanon"
a_name = "Emanon"
super(Adventurer, self).__init__(a_name)
class Monster(Character):
pass
my_adventurer = None
my_dungeon = None
# This is where things start happening.
def start():
global my_adventurer
global my_dungeon
my_adventurer = Adventurer(raw_input("What is your name, Adventurer? "))
dark_room = Room("Dark Room")
dark_room.add_item("lamp", "a brass lamp")
my_dungeon = Dungeon(3, 3)
my_dungeon.add_room(0, 0, Room())
my_dungeon.add_room(0, 1, MonsterRoom("Grue", "Dining Room"))
my_dungeon.add_room(0, 2, dark_room)
my_dungeon.add_room(1, 2, Room("Bedroom"))
my_dungeon.add_room(2, 2, Room("Closet"))
my_dungeon.add_room(2, 1, Room("Secret Passage"))
my_dungeon.add_room(2, 0, Room("Hidden Room"))
my_adventurer.enter_dungeon(my_dungeon)
start()
while True:
print "Your coordinates are %s, %s" % (my_dungeon.cur_x, my_dungeon.cur_y)
args = raw_input("> ").lower().split(' ')
method = args.pop(0)
has_error = True
for obj in (my_adventurer, my_dungeon.room(), my_dungeon):
try:
getattr(obj, method)(args)
has_error = False
break
except AttributeError:
pass
if has_error:
print "Not sure what you mean"