-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
284 lines (217 loc) · 7.09 KB
/
hangman.py
File metadata and controls
284 lines (217 loc) · 7.09 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
#!/usr/bin/env python
import sys
import os
import random
game_dictionary = 'dictionary_850_words.txt'
game_word = ""
game_word_letters = []
blanked_word = ""
player_guesses = []
number_wrong_guesses = 0
"""Simple terminal implementation of the classic Hangman game"""
def welcome():
print("Welcome to\n")
print("""
_
| |
| |__ __ _ _ __ __ _ _ __ ___ __ _ _ __
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \\
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/
""")
def random_word():
with open(os.path.join(sys.path[0], game_dictionary), "r") as f:
# variable to hold the dictionary of words used in the game
dictionary_data = f.read().splitlines()
return random.choice(dictionary_data)
def check_status():
# check the current player_guesses[] against the game_word_letters[]
correct_guesses = 0
for letter in game_word_letters:
if letter in player_guesses:
# check the number of correct letters against the number of
# distinct letters in the game_word
correct_guesses += 1
if correct_guesses == len(game_word_letters):
return "winner"
else:
continue
# elif letter not in player_guesses:
else:
return "next_guess"
def play():
# print ("check_status = " + check_status())
if check_status() == "next_guess" and number_wrong_guesses < 10:
guess = input("\nEnter a letter to see if it is contained in the word: ")
player_guesses.append(guess)
check_guess(guess)
print(blanked_word)
print("\nYour guesses are: " + str(player_guesses))
print("You have " + str(10 - number_wrong_guesses) + " guesses remaining.\n\n\n")
play()
elif check_status() == "next_guess" and number_wrong_guesses == 10:
print("\nToo bad partner, you lost. Better luck next time :-)\n")
print("The word you were looking for was: " + game_word)
exit()
# elif check_status() == "winner":
else:
print("""
_ _ _ _
| | | | | | (_)
___ ___ _ __ __ _ _ __ __ _| |_ _ _| | __ _| |_ _ ___ _ __ ___
/ __/ _ \| '_ \ / _` | '__/ _` | __| | | | |/ _` | __| |/ _ \| '_ \/ __|
| (_| (_) | | | | (_| | | | (_| | |_| |_| | | (_| | |_| | (_) | | | \__ \\
\___\___/|_| |_|\__, |_| \__,_|\__|\__,_|_|\__,_|\__|_|\___/|_| |_|___/
__/ |
|___/
Congrats, you beat me ;-P""")
exit()
def check_guess(guess):
global number_wrong_guesses
global blanked_word
# check if the guessed letter is in the hidden word
if guess in game_word:
# find out how many times the letter appears
no_occurences = game_word.count(guess)
if no_occurences == 1:
print("\nYour guess appears once in the word.\n")
else:
print("\nYour guess appears " + str(no_occurences) + " times in the word.\n")
# replace the underscore in the blanked word with the correctly guessed letter
substring_idx = 0
while no_occurences > 0:
if substring_idx == 0:
# case when the guessed letter is the first occurence of the letter in the game_word
substring_idx = game_word.find(guess, substring_idx)
else:
# case when the guessed letter is not the first occurence of the letter of the game_word
substring_idx = game_word.find(guess, substring_idx+1)
no_occurences -= 1
blanked_word_list = list(blanked_word)
blanked_word_list[(substring_idx*2)] = guess
blanked_word = "".join(blanked_word_list)
else:
print("\nUnlucky, your guess was incorrect.")
# enter a elif block to determine which animation to draw
if number_wrong_guesses == 0:
print("""
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 1:
print("""
|
|
|
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 2:
print("""
+---+
|
|
|
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 3:
print("""
+---+
| |
|
|
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 4:
print("""
+---+
| |
O |
|
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 5:
print("""
+---+
| |
O |
| |
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 6:
print("""
+---+
| |
O |
/| |
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 7:
print("""
+---+
| |
O |
/|\ |
|
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 8:
print("""
+---+
| |
O |
/|\ |
/ |
|
=========
""")
number_wrong_guesses += 1
elif number_wrong_guesses == 9:
print("""
+---+
| |
O |
/|\ |
/ \ |
|
=========
""")
number_wrong_guesses += 1
if __name__ == "__main__":
# say hello
welcome()
# select a word for the game
game_word = random_word().lower()
# turn the game_word string into a list of individual characters
# and add unique members to the game_word_letters list
for letter in list(game_word):
# check if letter exists in game_word_letters or not
if letter not in game_word_letters:
game_word_letters.append(letter)
# generate the blanked_word and display
print("The word you must guess looks like this:\n")
blanked_word = "_ " * len(game_word)
print(blanked_word)
# main function of the game
play()