-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhangman.py
365 lines (303 loc) · 8.91 KB
/
hangman.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
365
""" A revised game of hangman with two game modes. You can guess words at 3
difficulty levels or guess a common phrase. In each mode you only have 6
chances to guess the right letter. You also have the option to play again. """
import random
import sys
import json
class Game:
def __init__(self):
self.chances = 6
self.high_scores = {}
self.missed = []
self.discovered = []
self.display = ""
self.username = ""
self.wins = 0
self.losses = 0
self.load_highscore()
self.get_username()
@staticmethod
def choose_game_type():
"""
Allows the user to choose between the two game types: words or phrases.
:return:
"""
while True:
options = ['p', 'w', 'P', 'W']
choice = input("Choose game type: w for words. p for phrases: ")
if choice.isalpha():
if len(choice) == 1:
if choice in options:
choice = choice.lower()
break
else:
print(bcolors.WARNING + "That's not one of the options I gave you. Try again." + bcolors.ENDC)
else:
print(bcolors.WARNING + "I only need one letter. Try again." + bcolors.ENDC)
else:
print(bcolors.WARNING + "Letters only, gosh. Try again." + bcolors.ENDC)
return choice
def get_username(self):
"""
Allows the user to choose a username
:return:
"""
name = input("What's your name? ")
message = f"Welcome back, {name}" if self.high_scores.get(name) else f"Nice to meet you, {name}"
print(message)
print("I'll be keeping track of your highscore. Good Luck!\n")
self.username = name
@staticmethod
def choose_difficulty():
"""
Allows the user to choose the difficulty of a word game. chooses between
easy, medium and hard wordlists.
:return:
"""
options = ['e', 'E', 'n', 'N', 'h', 'H']
while True:
choice = input("Choose difficulty: e for easy. n for normal. h for hard. ")
if choice.isalpha():
if len(choice) == 1:
if choice in options:
choice = choice.lower()
break
else:
print(bcolors.WARNING + "That's not one of the options I gave you. Try again." + bcolors.ENDC)
else:
print(bcolors.WARNING + "I only need one letter. Try again." + bcolors.ENDC)
else:
print(bcolors.WARNING + "Letters only, gosh. Try again." + bcolors.ENDC)
return choice
@staticmethod
def choose_topic():
"""
Allows the user to choose the topic of the phrases for a phrase. There are currently
options to choose idioms or slogans
:return:
"""
options = ['i', 'I', 's', 'S']
while True:
choice = input("Choose difficulty: i for idioms. s for slogans: ")
if choice.isalpha():
if len(choice) == 1:
if choice in options:
choice = choice.lower()
break
else:
print(bcolors.WARNING + "That's not one of the options I gave you. Try again." + bcolors.ENDC)
else:
print(bcolors.WARNING + "I only need one letter. Try again." + bcolors.ENDC)
else:
print(bcolors.WARNING + "Letters only, please. Try again." + bcolors.ENDC)
return choice
@staticmethod
def word_game_setup(difficulty):
"""
Set up game by choosing the target word from a file based on difficulty
:param difficulty:
:return:
"""
filename_map = {
'e': 'easy_hang_words.txt',
'n': 'normal_hang_words.txt',
'h': 'hang_words.txt'
}
filename = filename_map.get(difficulty, 'easy_hang_words.txt')
with open(filename, 'r') as open_file:
all_text = open_file.read()
word_list = all_text.split("\n")
target = random.choice(word_list)
return target
@staticmethod
def phrase_game_setup(topic):
"""
Set up game by choosing the target phrase from a file based on topic
:param topic:
:return:
"""
d = {
'i': "idioms.txt",
's': "slogans.txt"
}
with open(d[topic], 'r') as open_file:
all_text = open_file.read()
phrase_list = all_text.split("\n")
target = phrase_list[random.randrange(0, len(phrase_list))]
target = target.lower()
return target
def guess(self, targeted, game_type):
"""
Asks the user to submit a letter guess, then validates the input,
shows the letters they missed or reveals the word with the letters
they correctly guessed.
:param targeted:
:param game_type:
:return:
"""
correct_guess_text = [
"You guessed it!",
"Nice job!",
"That's right!",
"I see what you did there.",
"Keep it up!",
"Just a few more to go!"
]
target = targeted
self.display = ""
guess = input("Guess a letter or type 'solve': ")
if guess.isalpha():
if len(guess) == 1:
guess = guess.lower()
if guess in self.discovered:
print(bcolors.WARNING + "You already guessed that. Try again." + bcolors.ENDC)
elif guess in target:
self.discovered.append(guess)
print(correct_guess_text[random.randrange(0, len(correct_guess_text))])
else:
if guess not in self.missed:
self.missed.append(guess)
print("That's not right. Try again.")
self.chances -= 1
else:
print(bcolors.WARNING + "You already guessed that. Try again." + bcolors.ENDC)
print("Missed Letters: ", self.missed)
elif guess in ["solve", "exit", "quit"]:
if guess == "solve":
if game_type == 'p':
solution = input("Enter the full phrase: ")
else:
solution = input("Enter the full word: ")
result = "win" if solution == target else "lose"
self.win_lose(result, game_type, target)
else:
print("Thanks for playing")
sys.exit()
else:
print(bcolors.WARNING + "You can only guess one letter at a time." + bcolors.ENDC)
else:
print(bcolors.WARNING + "Try guessing a letter" + bcolors.ENDC)
self.display = ""
for n in target:
self.display += (n + " ") if n in self.discovered else (' / ' if n == ' ' else '_ ')
print(self.display)
def play(self):
"""
Starts the game, reviews the guessing progress and shares the victory or defeat message
:return:
"""
wants_to_play = True
while wants_to_play:
self.chances = 6
game_type = self.choose_game_type()
if game_type == 'w':
difficulty = self.choose_difficulty()
target = self.word_game_setup(difficulty)
elif game_type == 'p':
topic = self.choose_topic()
target = self.phrase_game_setup(topic)
else:
raise Exception("How did this even happen?")
challenge = ""
for letter in target:
challenge += ' / ' if letter == ' ' else '_ '
print("Alright, let's get started. Can you solve this? : \n")
print(challenge)
while self.chances > 0:
self.guess(target, game_type)
print(f"You have {self.chances} guesses remaining.")
print()
target_words = self.display.split("/")
current_guess = "".join(word.replace(" ", "") for word in target_words)
if current_guess == target:
self.win_lose("win", game_type, target)
wants_to_play = self.play_again()
break
else:
self.win_lose("loss", game_type, target)
wants_to_play = self.play_again()
def win_lose(self, result, game_type, target):
"""
Print the win/loss result.
:param result:
:param game_type:
:param target:
:return:
"""
message = ""
if result == "win":
print(bcolors.OKGREEN + "You win!" + bcolors.ENDC)
self.wins += 1
message = f"You've won {self.wins} time{'s' if self.wins != 1 else ''}"
elif result == "loss":
print(bcolors.FAIL + "Game Over! You Lose" + bcolors.ENDC)
self.losses += 1
message = f"You've lost {self.losses} time{'s' if self.losses != 1 else ''}"
print(f"The correct {'phrase' if game_type == 'p' else 'word'} was: {target}")
print(message)
def play_again(self):
"""
Ask whether the user would like to play again
:return:
"""
self.save_highscore()
again = input("Wanna play again? Y/N: ")
if again.lower() == 'y':
print("Yay! Let's go again" + '\n')
self.reset_values()
return True
else:
print("Thanks for playing!")
rounds = self.wins + self.losses
percent = (float(self.wins) / rounds) * 100
print(f"You played {rounds} times")
print(f"You won {percent:.1f}% of games!")
return False
def reset_values(self):
"""
Resets global game value variables
:return:
"""
self.chances = 6
self.missed = []
self.discovered = []
self.display = ""
def save_highscore(self):
"""
Saves the high score of the current user
:return:
"""
current_score = self.wins
old_score = self.high_scores.get(self.username)
if old_score is None or current_score > old_score:
self.high_scores[self.username] = current_score
with open("highscores.json", "w") as f:
json.dump(self.high_scores, f, indent=6)
def load_highscore(self):
"""
Load the high scores
:return:
"""
with open('highscores.json') as f:
self.high_scores = json.load(f)
class bcolors:
"""
CLI colors
"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
if __name__ == "__main__":
"""
The action starts here
"""
print("Welcome to Hang_words.")
print("You can quit the game by typing 'exit' or 'quit' instead of guessing a letter. ")
print()
g = Game()
g.play()