-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangMan.py
345 lines (248 loc) · 7.22 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
"A simple HangMan game that uses words from all the nouns in english language"
import random
FILENAME = '91K nouns.txt'
"""
Open the file based on the filename cand create a list of
that contains all words.
PARAMETER:
filename: str
The filename.
RETURN:
words: list
A list filled with all of the words
"""
def open_file(filename : str) -> list:
# Change this variable to access another text file
file = open(filename, 'r')
# Put the content in a list.
words = file.read().splitlines()
file.close()
return words
"""
Select a random word and split the word into a list.
PARAMETER:
words: list
The list contains all the words retrieved from the file.
RETURN:
answer: list
A list contains the strings with the letters separated.
"""
def select_random_word(words: list) -> list:
# Select a random word in the list of words and place it into a new list
num_of_words = len(words)
selected_num = random.randint(0, num_of_words - 1)
selected_word = words[selected_num]
selected_word = selected_word.lower()
answer = list(selected_word)
return answer
"""
Create a new list based on the selected word and replace it with '_'
PARAMETER:
selected_word: list
The selected_word in a list with its letters separated.
RETURN:
hidden_word: list
The word hidden by '_'
"""
def hide_word (selected_word: list) -> list:
# Create a new list that will be filled with '_'
hidden_word = ['_'] * len(selected_word)
# Some noun contains symbol with '-' so we need to check and replace that area in the hidden list
count = 0
for x in selected_word:
if x == '-':
hidden_word[count] = '-'
count += 1
return hidden_word
"""
Welcome the user with the starting message.
"""
def welcome():
print("Welcome to Python Hangman!")
print("A random Noun will be selected as the Hidden Word!")
print("You have 6 Chances to guess the correct letters in the word.")
print("When you're ready, press 'enter'!\n")
"""
Function for when the user fails the first time
"""
def first_fail():
print(" ----- ")
print(" | | ")
print(" | O ")
print(" | ")
print(" | ")
print(" | ")
print("--------")
"""
Function for when the user fails the second time
"""
def second_fail():
print(" ----- ")
print(" | | ")
print(" | O ")
print(" | | ")
print(" | ")
print(" | ")
print("--------")
"""
Function for when the user fails the third time
"""
def third_fail():
print(" ----- ")
print(" | | ")
print(" | O ")
print(" | |\\ ")
print(" | ")
print(" | ")
print("--------")
"""
Function for when the user fails the fourth time
"""
def fourth_fail():
print(" ----- ")
print(" | | ")
print(" | O ")
print(" | /|\\ ")
print(" | ")
print(" | ")
print("--------")
"""
Function for when the user fails the fifth time
"""
def fifth_fail():
print(" ----- ")
print(" | | ")
print(" | O ")
print(" | /|\\ ")
print(" | / ")
print(" | ")
print("--------")
"""
Function for when the user fails the sixth time
"""
def sixth_fail():
print(" ----- ")
print(" | | ")
print(" | O ")
print(" | /|\\ ")
print(" | / \\ ")
print(" | ")
print("--------")
"""
Display the current status of the Hangman based on how many times the user has failed.
PARAMETER:
fail_count: int
The number of times the user has failed.
"""
def display_fail(fail_count: int):
if (fail_count == 1):
first_fail()
elif (fail_count == 2):
second_fail()
elif (fail_count == 3):
third_fail()
elif (fail_count == 4):
fourth_fail()
elif (fail_count == 5):
fifth_fail()
elif (fail_count == 6):
sixth_fail()
"""
Check whether the user has inputted a valid letter
PARAMETER:
character: str
The letter the user has entered.
RETURN:
A boolean on whether the letter is valid.
"""
def letter_validity(character: str) -> bool:
# Check if the character is an alphabet and that it doesn't exceed the length of 1
if (character.isalpha() == False or len(character) > 1):
return False
return True
"""
Checked whether the letter the user inputted was already used.
PARAMETER:
character: str
The character the user entered.
user_letters: list
List of letters the user has already entered.
RETURN:
A boolean that indicate whether its true or false.
"""
def already_used_letters(character: str, used_letters: list) -> bool:
for x in used_letters:
if (x == character):
return True
return False
"""
Checked whether the letter the user inputted was already used.
PARAMETER:
character: str
The character the user entered.
user_letters: list
List of letters the user has already entered.
RETURN:
A boolean that indicate whether its true or false.
"""
def victory_validity(hidden_word: list) -> bool:
for x in hidden_word:
if (x == '_'):
return False
return True
def main():
# Initialise variables
words = open_file(FILENAME)
word = select_random_word(words)
correct_word = ''.join(word)
hidden_word = hide_word(word)
used_letters = []
number_of_fails = 0
number_of_correct_letters = 0
for x in word:
if (x != '-'):
continue
number_of_correct_letters += 1
# Welcome the user
welcome()
while(True):
user_input = input()
if (user_input == ""):
break
else:
print("Please enter a valid command.")
print("The Hidden Word: " + str(hidden_word) + "\n")
print("Used Letters: " + str(used_letters) + "\n")
while(True):
in_word = False
user_letter = input("Please enter a guess: ")
# Check if user's input is actually a letter.
if (letter_validity(user_letter) == False):
print("Please enter a letter.")
elif (already_used_letters(user_letter, used_letters) == True):
print("You have already used this letter, please enter another one!")
else:
index = 0
# Go through and check if the user's guess is in the word
for x in word:
if (user_letter.lower() == x):
hidden_word[index] = user_letter
in_word = True
index += 1
used_letters.append(user_letter)
if (in_word == False):
number_of_fails += 1
if (number_of_fails >= 6):
print("GAME OVER!! YOU HAVE LOST")
print("The word is " + str(correct_word) + "!")
break
# Check if the user has won
if (victory_validity(hidden_word) == True):
print("Congragulations, you have Won!")
print("The word is " + str(correct_word) + "!")
break
# Show the user the status of the hangman
display_fail(number_of_fails)
print("The Hidden Word: " + str(hidden_word) + "\n")
print("Used Letters: " + str(used_letters) + "\n")
main()