-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwizard.py
More file actions
executable file
·311 lines (279 loc) · 8.96 KB
/
wizard.py
File metadata and controls
executable file
·311 lines (279 loc) · 8.96 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
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
#!/usr/bin/env python3
from configparser import ConfigParser
import sys
import os.path
##### HOMEWORK
# reorganize your code.
# apply literate programming (comments)
# test out completion algorithm in >>> (IDE)
def main():
set_readline()
wiz_help()
wiz = load('wiz.save')
try:
while True:
task = input("> ")
task = task.strip().lower()
print(request(wiz, task))
save(wiz)
except (KeyboardInterrupt, EOFError):
bye()
def request(wiz, task):
if task in ["quit", "q", "Q"]:
bye()
elif task in wiz.locations:
return wiz.travel(task)
elif task == "brew":
return wiz.brew()
elif task == "forage":
return wiz.forage()
elif task == "gift":
return wiz.gift()
elif task == "gold":
return wiz.purse()
elif task == "health":
return wiz.health()
elif task in ["help", "h", "?"]:
return wiz_help()
elif task in ["location", "where"]:
return wiz.where()
elif task == "purse":
return wiz.purse()
elif task == "relax":
return wiz.relax()
elif task == "sell":
return wiz.sell()
elif task in ["skill level", "skill", "skills"]:
return wiz.health
elif task == "shop":
return wiz.shop()
elif task == "study":
return wiz.study()
elif task == "work":
return wiz.work()
else:
print(f'''I dunno "{task}".''')
class Wizard:
def __init__(self, location="forest", skill=0, gold=0, books=1, stress=0,
ego=0, potions=0, mushrooms=0, work=0):
self.books = int(books)
self.ego = int(ego)
self.gold = int(gold)
self.location = location
self.mushrooms = int(mushrooms)
self.skill = int(skill)
self.stress = int(stress)
self.potions = int(potions)
self.locations = {
"forest" : "You travel to the forest where " \
"you can forage for mushrooms and relax.",
"tower" : "You travel to your tower where there is peace and quiet.",
"village" : "You travel to your village where " \
"you can work, sell goods and shop.",
"black rock city" : "You travel to the playa )'(.",
}
def travel(self, location):
if location not in self.locations:
msg = f'''{location} is not a place in this land.'''
elif location == self.location:
msg = f'''You are already in the {location}.'''
else:
self.location = location
msg = f'''{self.locations[location]}'''
return msg
def study(self):
if self.location == "tower":
if self.skill < self.books:
self.skill += 1
self.stress += 1
self.books -= 1
msg = f'''Your skill level is now {self.skill}.'''
else:
msg = f'''You have no more new books to read.'''
else:
msg = f'''You cannot study in the {self.location}.'''
return msg
def brew(self):
if self.location == "tower":
if self.mushrooms == 0:
msg = f'''You can't brew potions without mushrooms.'''
else:
self.mushrooms -= 1
self.potions += 1
self.stress -= 1
msg = f'''You now have {self.potions} potions.'''
else:
msg = f'''You cannot brew potions in the {self.location}.'''
return msg
def forage(self):
if self.location == "forest":
self.mushrooms += 1
msg = f'''You now have {self.mushrooms} mushrooms.'''
else:
msg = f'''There are no mushrooms in the {self.location}.'''
return msg
def gift(self):
if self.location == "black rock city":
if self.potions > 0:
self.potions -= 1
if self.stress > 0:
self.stress -= 1
msg = f'''You now have {self.potions} potions and you have lowered your stress level.'''
else:
msg = f'''You now have {self.potions} potions.'''
else:
msg = f'''You have no potions to gift.'''
else:
msg = f'''You cannot give gifts in the {self.location}.'''
return msg
def where(self):
if self.location is None:
msg = f'''You are nowhere. Where would you like to go?'''
else:
msg = f'''You are at the {self.location}.'''
return msg
def purse(self):
msg = f''' You have {self.books} books\n'''
msg += f''' You have {self.gold} gold\n'''
msg += f''' You have {self.mushrooms} mushrooms\n'''
msg += f''' You have {self.potions} potions'''
return msg
def relax(self):
if self.location == "forest":
if self.stress > 0:
self.stress -= 1
msg = f'''You now have {self.stress} stress level.'''
else:
msg = f'''You can't relax in {self.location}.'''
return msg
def sell(self):
if self.location == "village":
if self.potions > 0:
self.gold += 1
msg = f'''You now have {self.gold} gold.'''
else:
msg = f'''You have no brewed potions to sell.'''
else:
msg = f'''You can't sell goods in the {self.location}.'''
return msg
def shop(self):
if self.location == "village":
if self.gold == 0:
msg = f'''You have {self.gold} gold. You must work to earn gold.'''
else:
if self.stress >10:
msg = "You are too stressed out. Go do relaxing things."
else:
self.gold -= 1
self.books += 1
self.stress += 1
msg = f'''You now have {self.books} books and {self.gold} gold.'''
else:
msg = f'''You can't shop in the {self.location}.'''
return msg
def health(self):
msg = f''' You have a stress level of {self.stress}.\n'''
msg += f''' You have a skill level of {self.skill}.'''
return msg
def work(self):
if self.location == "village":
if self.skill > 0:
self.gold += 1
self.skill -= 1
if self.stress >10:
msg = "You are too stressed out. Go do relaxing things."
else:
self.stress += 1
msg = f'''All in a day's work. You now have {self.gold} gold.'''
else:
msg = f'''You can't work without any skills.'''
else:
msg = f'''There is no work in the {self.location}.'''
return msg
def completion(text, state):
options = [
"black rock city",
"brew",
"forage",
"forest",
"gift",
"gold",
"health",
"location",
"purse",
"relax",
"sell",
"skill level",
"shop",
"study",
"tower",
"travel",
"village",
"work",
]
matches = []
for option in options:
if option.startswith(text):
matches.append(option)
if state >= len(matches):
return None
return matches[state]
def set_readline():
import readline
readline.set_auto_history(True)
readline.set_completer(completion)
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
def save(wiz):
data = ConfigParser()
data['wizard'] = {
"location": wiz.location,
"skill": wiz.skill,
"gold": wiz.gold,
"books": wiz.books,
"stress": wiz.stress,
"ego": wiz.ego,
"potions": wiz.potions,
"mushrooms": wiz.mushrooms,
}
save_file = open("wiz.save", "w")
data.write(save_file)
def load(save_file):
data = ConfigParser()
if os.path.isfile(save_file):
data.read(save_file)
else:
data['wizard'] = {}
wiz = Wizard(**data['wizard'])
return wiz
def wiz_help():
msg = '''\n LOCATIONS: '''
msg += '''\n forest '''
msg += '''\n village '''
msg += '''\n tower '''
msg += '''\n black rock city '''
msg += '''\n'''
msg += '''\n ACTIONS: '''
msg += '''\n brew '''
msg += '''\n forage '''
msg += '''\n gift'''
msg += '''\n gold '''
msg += '''\n location '''
msg += '''\n purse '''
msg += '''\n sell '''
msg += '''\n skill level '''
msg += '''\n shop '''
msg += '''\n study '''
msg += '''\n work '''
msg += '''\n'''
msg += '''\n help '''
msg += '''\n quit '''
msg += '''\n \n Please choose an action or location. '''
return msg
def bye():
print("\nThank you for playing!")
sys.exit()
if __name__ == "__main__":
main()