-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchinesetest.py
More file actions
357 lines (284 loc) · 13.2 KB
/
Copy pathchinesetest.py
File metadata and controls
357 lines (284 loc) · 13.2 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
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
'''
Chinese Test Words
References:
Internet
My mom
Version History:
v1.1 August 2025:
- Add tkinter for popugup GUI select options
- Gamify the heck out of it with Creature class and attack and block options.
V1.0 July 2025:
- Create initial 'game' which is flashcard scoring with points
'''
import sys
import random
import pandas as pd
import tkinter as tk
from tkinter import messagebox # weirdly have to do this in order for it to work
#%% I/O Functions
def dict2csv(words,output='words.csv'):
'''Convert dictionary called words to DataFrame
and save it to csv
'''
df = pd.DataFrame(
[(k, v[0], v[1]) for k, v in words.items()],
columns=["id", "chinese", "english"]
)
# Save to CSV
df.to_csv(output, index=False, encoding='utf-8-sig')
return df
def csv2dict(filename):
'''Ingests in csv file and converts it to a dictionary
where first row is key and 2 other columns are tuple values in a tuple
'''
# Read CSV file
df = pd.read_csv(filename)
# Create dictionary: first column as key, next two columns as tuple
data_dict = dict(zip(df.iloc[:, 0], zip(df.iloc[:, 1], df.iloc[:, 2])))
return data_dict
#%% WORD JUMPS
def jumpword(nextorrandom):
if nextorrandom in ['next','n','下','1']:
word_index = next(temp,None)
elif nextorrandom[0:3] in ['num']:
word_index = int(nextorrandom[3:])
else:
print('Random word used. 随机单词。')
word_index, _ = random.choice(list(words.items()))
return word_index
#%% GUI functions
def choose_option():
global selected_value
selected_value = choice_var.get() # store the selected value
root.destroy() # close the window
#%% Information messages
message_dict = {'theintro': '''
MAGIC LANGUAGE APTITUDE TEST
In this world, language can hold all keys. As you approach the final throne,
an ancient dragon stares back at you. The beast opens its giant maw and speaks
in a thundering voice:
Do you truly think you can block my flaming breath?
Very well, I can go easy on you or it can be hard.
What do you choose, mortal?
''',
'correct': 'You blocked the flaming attack with an appropriate counterspell. You still sustain a little damage.',
'incorrect': 'You mutter senseless words that did nothing to stop the flame ball from hitting your body!!',
'flee': 'You flee from the scene as if a dragon is after you. You live the rest of your life a nameless nobody. You have utterly failed.',
'dying': 'You feel that you are close to dying. You know that if you fail to block one more time you are gone!',
'death': 'You fail to utter the correct words of power and you are struck one more time. You burn up in a pile of ashes.',
'victory': 'You have succeeded where no one else has. Truly you deserve the title of Hero.'
}
#%% Gamified!
class Creature:
def __init__(self, name, attack_power, health, block):
"""
Initialize a Creature.
Args:
name (str): Name of the creature.
attack_power (int or float): Attack strength of the creature.
health (int or float): Current health points of the creature.
block (float): Damage reduction percentage (0 to 1).
"""
self.name = name
self.attack_power = attack_power
self.health = health
self._block = block
@property
def blocked(self):
return self._block
@blocked.setter
def blocked(self, new_value):
if (new_value < 0) or (new_value > 1):
raise ValueError('Damage reduction must be between 0 and 1')
self._block = new_value
def defense_up(self):
if self.name == 'Dragon':
print(f'{self.name} has defense of {self.blocked*100:.0f}%')
if round(self._block,1) <= 0.5:
print('The dragon skin hardens as its defense increases.')
self._block += 0.1
print(f'{self.name} now has defense of {self.blocked*100:.0f}%')
else:
self._block = 0.55 # max it can ever reach
def attack_ready(self):
if self.name == 'Dragon':
print('The dragon gets ready for another blast of terrible flames.')
elif self.name == 'Hero':
print('You steady your hands and cast another spell! You utter these words: ')
print(random.choice(list(words.items()))[1][0]) # random word
def attack(self, target):
"""Attack another creature, reducing their health."""
if not isinstance(target, Creature):
raise ValueError("Target must be a Creature instance.")
# Calculate effective damage after block reduction
damage = self.attack_power * (1 - target.blocked)
target.health -= damage
print(f"{self.name} attacks {target.name} for {damage:.1f} damage!")
print(f"{target.name} now has {target.health:.1f} health.")
def yell(self):
if self.name == 'Dragon':
print('You will never get this treasure. Get ready to face your end!!!')
messagebox.showwarning(f'{self.name} roars!', 'You will never get this treasure. Get ready to face your end!!!')
def is_alive(self):
"""Check if the creature is still alive."""
return self.health > 0
def is_dead(self):
"""Check if the creature is dead."""
if self.health <= 0:
print(f'The {self.name} is dead!')
return self.health <= 0
def __str__(self):
"""String representation of the creature."""
return (f"{self.name} (Attack: {self.attack_power}, "
f"Health: {self.health}, Block: {self.blocked*100:.0f}%)")
#%%
if __name__ == '__main__':
# instantiate dictionary of words from csv file
words = csv2dict('words.csv')
keys_words = list(words.keys())
if '-dict' in sys.argv:
print('Using as dictionary instead of tester...')
# get all chinese words in dictionary
chinesewords = []
for r in words.values():
chinesewords.append(r[0])
while True:
chooseword = input('Enter chinese character or enter Q to quit: ')
if chooseword in chinesewords:
matching_key = next((key for key, value in words.items() if value[0] == chooseword), None)
value = words[matching_key]
print(value[1])
if len(value) > 2:
sentence_dict = words[matching_key][2]
for k,v in value[2].items():
print(k+': ')
print(v[0])
print(v[1])
print('===================================================')
else:
print('No example sentence exists for this word.')
elif chooseword in ['quit','Q','q']:
print('Quitting! 退出!')
break
else:
print('Word not in the dictionary. Try again!')
continue
else: # default option
print(message_dict['theintro'])
# instantiate player
player = Creature("Hero", 100, 100, 0.0) # block attribute changes so set to 0 here for now
# instantiate dragon
dragon = Creature("Dragon", 50, 250, 0.5) # 50% block
##############################
# GUI poup
root = tk.Tk()
root.title("Difficulty Choose")
# Explanation label
tk.Label(root, text=message_dict['theintro'], font=("Arial", 12)).pack(pady=(10, 5))
# Tkinter variable for selection
choice_var = tk.StringVar(value="Easy") # default value
# Radio buttons
tk.Radiobutton(root, text="Easy", variable=choice_var, value="Easy").pack(anchor="w")
tk.Radiobutton(root, text="Hard", variable=choice_var, value="Hard").pack(anchor="w")
# Select button
tk.Button(root, text="Select", command=choose_option).pack(pady=10)
# Run GUI
root.mainloop()
# Use the selected value after window closes
print("Selected:", selected_value)
##############################
# play_mode = input('You reply as such: ')
play_mode = selected_value
if play_mode.lower() in ['hard', 'h', 'reward']:
play_mode = 'hard'
elif play_mode.lower() in ['easy', 'e']:
play_mode = 'easy'
print(f'You have chosen the {play_mode} mode.')
dragon.yell()
temp = iter(words)
word_index = next(temp,None)
while True:
print('Hitpoints: ', player.health)
if player.is_dead():
print(message_dict['death'])
messagebox.showwarning("You have died!!!", message_dict['death'])
break
elif player.health < 5:
print(message_dict['dying'])
messagebox.showwarning("You are close to dying...", message_dict['dying'])
elif dragon.is_dead():
print(message_dict['victory'])
messagebox.showwarning("You won!!!", message_dict['victory'])
break
dragon.attack_ready()
question_word = f'You can block by uttering the magical equivalent of these words:\n{words[word_index][1]}'
print(question_word)
#%%
list_choices = ['a', 'b', 'c', 'd']
random.shuffle(list_choices) # shuffles them
# create dictionary of choices
target_value = words[word_index][0]
dict_choices = {list_choices[0]: target_value,
list_choices[1]: words[random.choice(keys_words)][0],
list_choices[2]: words[random.choice(keys_words)][0],
list_choices[3]: words[random.choice(keys_words)][0],
}
# list_choices = list(dict_choices.items())
# random.shuffle(list_choices)
# dict_choices = dict(list_choices)
correct_choice = next((key for key, value in dict_choices.items() if value == target_value), None)
#%%
print('''
===============================================================
How do you choose to reply?
''')
for key in sorted(dict_choices.keys()):
print(f"\t\t{key}: {dict_choices[key]}")
print('''
===============================================================
''')
##############################
# GUI poup
root = tk.Tk()
root.title("What is the right word to utter here to block the attack?")
# Explanation label
tk.Label(root, text=f'Health: {player.health}', font=("Arial", 12)).pack(pady=(10, 5))
tk.Label(root, text=question_word, font=("Arial", 12)).pack(pady=(10, 5))
# Tkinter variable for selection
choice_var = tk.StringVar(value="a") # default value
# Radio buttons
for key in sorted(dict_choices.keys()):
tk.Radiobutton(root, text=f"{key}: {dict_choices[key]}", variable=choice_var, value=f"{key}").pack(anchor="w")
tk.Radiobutton(root, text="You choose to give up and abandon your quest to forever live in ignomy.", variable=choice_var, value="q").pack(anchor="w")
# Select button
tk.Button(root, text="Select", command=choose_option).pack(pady=10)
# Run GUI
root.mainloop()
# Use the selected value after window closes
print("Selected:", selected_value)
##############################
# chooseword = input('Enter the choice among these: ')
chooseword = selected_value
#%%
if chooseword in ['quit','q']:
print(message_dict['flee'])
messagebox.showinfo("Flee!!!", message_dict['flee'])
# print('Quitting! 退出!')
break
elif chooseword.lower() in correct_choice:
# print('Correct! Moving to next word. 正确!继续下个词。')
print(message_dict['correct'])
player.blocked = 0.9
dragon.attack(player)
word_index = jumpword('next')
# player gets to attack
player.attack_ready()
dragon.defense_up() # dragon defense hardens
player.attack(dragon)
continue
else:
# print('You are incorrect! Try again. 错误!请再次试试。')
print(message_dict['incorrect'])
player.blocked = 0
dragon.attack(player)
continue