-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
executable file
·282 lines (250 loc) · 9.53 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
#!/usr/bin/env python
import requests
import random
import string
import time
import sys
import os
os.system("") # enable ANSI escape codes on Windows.
TITLE = """
_ _
| | | |
| |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
| __ |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/ """
DICTIONARY_URL = "https://randomwordgenerator.com/json/words_ws.json"
""" $
www
/. .\
[ 7 ]
.___}~^~{___.
( | | )
/ )\ /( \
( { | | } )
`@ {_____} @'
/ | \
( /^\ )
| ) ( |
/__/ \__\
"""
def timed(func):
def inner(*args, **kwargs):
start = time.time()
out = func(*args, **kwargs)
stop = time.time()
return (out, stop - start)
return inner
class Hangman:
def __init__(self, wins=0, losses=0, is_replay=False, *args, **kwargs):
if not is_replay:
self.print_intro()
self.best_time = None
self.guesses = []
self.chances = 6
self.wins = wins
self.losses = losses
print(" Getting secret word... Please wait...", end='\r')
self.secret_word = self.get_random_word().upper()
self.clear_line()
self.board = '_' * len(self.secret_word)
self.head = """ $
| || ,^.
| || | |
| || `.'
| ||"""
self.body = """
| ||
| ||
| ||"""
self.legs = """
| ||
| ||
| ||
| || _________________________"""
if is_replay:
for _ in range(38):
self.clear_line()
self.move_to_line_above()
print("\033D", end='\r') # scroll up a line
self.print_board(is_replay)
def print_intro(self):
print(TITLE)
def print_board(self, is_replay=False):
alphabet = ' '.join(self.red(self.strikethrough(i)) if i in self.guesses else i for i in string.ascii_uppercase)
padding = 8 * (len(self.secret_word) - self.board.count('_'))
best = f"{self.best_time:.2f}" if self.best_time is not None else ""
display = f""" _______________,,.
/_____________.;;'/|
|"____ _______;;;]/ Wins: {self.wins} Losses: {self.losses} Best Time: {best}
| || //' ;
| ||//' $
| |//' $
| /' {self.head}
| || {self.body}
| || {self.legs}
| || / /|
| ||/ _____ / /
| || /|___/ / /|
| || / | / / /||
|_|/ /__|_/ / / ||
/ / /| ||
/ / / | ||
/ / / | |
/_________________________/ /
|_________________________|/ {' '.join(self.underline(i) if i != "_" else i for i in self.board):^{52 + padding}}
| || | ||
| || | || {alphabet}
| || | ||
| || | ||
| || | |"""
self.display_size = len(display.split('\n'))
print(display)
def get_random_word(self):
page = requests.get(DICTIONARY_URL)
if page.ok:
try:
data = page.json()
except: # simplejson.error.JSONDecodeError or json.JSONDecodeError
raise Exception("Cannot access the random word dictionary!")
if data.get("data", None) is not None:
return data["data"][random.randint(0, len(data["data"]))]["word"]["value"]
raise Exception("Cannot access the random word dictionary!")
def get_guess(self):
guess = input("Guess: ") # on line 1
# now on line 2
if len(guess) != 1 or not guess.isalpha():
self.move_to_line_above() # now on line 1
self.clear_line() # clear line 1 ("Guess: blah")
print("Invalid guess! Should be a single alphabetical character!") # now on line 1
# now on line 2
while len(guess) != 1 or not guess.isalpha():
self.clear_line() # clear if more than one iteration
guess = input("Guess: ") # now on line 3
self.move_to_line_above() # now on line 2
# ends on line 2
self.clear_line()
self.move_to_line_above() # now on line 1
self.clear_line()
return guess.upper()
def start(self):
did_win, round_time = self.play_round()
if did_win:
self.best_time = round_time
self.refresh_board()
print("You won!" if did_win else f"You died... The secret word was {self.secret_word}")
replay = input("Would you like to play again? y/n: ").lower() in ['y', 'yes', 'yea']
while replay:
self.__init__(self.wins, self.losses, True)
did_win, round_time = self.play_round()
if self.best_time is not None and round_time < self.best_time and did_win:
self.best_time = round_time
elif self.best_time is None and did_win:
self.best_time = round_time
self.refresh_board()
print("You won!" if did_win else f"You died... The secret word was {self.secret_word}")
replay = input("Would you like to play again? y/n: ").lower() in ['y', 'yes', 'yea']
@timed
def play_round(self):
did_win = False
while self.chances > 0:
guess = self.get_guess()
if guess in self.guesses:
continue
self.guesses.append(guess)
if guess not in self.secret_word:
self.chances -= 1
# draw next body part
if self.chances == 5:
self.head = """ $
| || www
| || /. .\\
| || [ 7 ]
| || }-^-{"""
elif self.chances == 4:
self.head = """ $
| || www
| || /. .\\
| || [ 7 ]
| || __}-^-{__"""
self.body = """ | |
| || \ /
| || | |
| || {_____}"""
elif self.chances == 3:
self.head = """ $
| || www
| || /. .\\
| || [ 7 ]
| || .___}-^-{__"""
self.body = """ ( | |
| || / )\ /
| || ( { | |
| || `@ {_____}"""
elif self.chances == 2:
self.head += "_."
self.body = """ ( | | )
| || / )\ /( \\
| || ( { | | } )
| || `@ {_____} @'"""
elif self.chances == 1:
self.legs = """ / |
| || ( /
| || | )
| || /__/
| || _________________________"""
elif self.chances ==0:
self.legs = """ / | \\
| || ( /^\\ )
| || | ) ( |
| || /__/ \\__\\
| || _________________________"""
self.head = self.head.replace('.', 'x', 2)
else:
self.board = self.assign_char_by_indices(self.board, guess, self.find_indices(self.secret_word, guess))
if self.board == self.secret_word:
self.wins += 1
did_win = True
self.refresh_board()
if did_win:
break
else: # lost
self.losses += 1
self.refresh_board()
return did_win
def refresh_board(self):
for i in range(self.display_size):
self.clear_line()
self.move_to_line_above()
self.print_board()
@staticmethod
def clear_line():
print("\033[2K", end='\r')
@staticmethod
def move_to_line_above():
print("\033[F", end='\r')
@staticmethod
def find_indices(string, char):
return [i for i, letter in enumerate(string) if letter == char]
@staticmethod
def assign_char_by_indices(original_string, char, indices):
return ''.join(char if i in indices else j for i, j in enumerate(original_string))
@staticmethod
def red(string):
return f"\033[31m{string}\033[0m"
@staticmethod
def underline(string):
return f"\033[4m{string}\033[0m"
@staticmethod
def strikethrough(string):
return f"\033[9m{string}\033[0m"
def main():
try:
game = Hangman()
game.start()
except KeyboardInterrupt:
print('\n\n')
if __name__ == "__main__":
main()