forked from tiyd-python-2015-05/mystery-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
335 lines (230 loc) · 9.81 KB
/
interface.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
import os
import mystery_word
import demon_words
###############################################################################
def clean_text(text):
"""Takes a string line by line, strips leading and trailing whitespace
characters, and lowercases. Returns a list of the lines.
Keyword Arguments:
text -- the string to be processed
"""
return [line.strip().lower() for line in text]
###############################################################################
def import_words(filepath):
"""Opens a file and places each line from that file in a list, then
calls clean_text(text) to remove whitespace and lowercase. Returns a list.
Keyword Arguments:
filepath -- the path to the file you wish to read
"""
with open(filepath) as file:
text = file.readlines()
return clean_text(text)
#clean_text only really works for files with one word per line.
###############################################################################
def gates_of_hell():
"""Returns True or False depending on user input. Is used to switch from
normal mode to "demon" mode (controlled by demon_words.py)
"""
are_open = input("Would you like to play on DEMON mode (y/n) ??? ").lower()
if are_open == "y":
return True
elif are_open == "n":
return False
else:
print("That is not a valid choice. Please choose again.")
return gates_of_hell()
###############################################################################
def choose_difficulty():
"""From a default imported library of words, filters by word length based
on user input and returns a list of the filtered words.
"""
words = import_words('/usr/share/dict/words')
difficulty = input("What difficulty would you like? [E]asy, [M]edium, or [H]ard? ").lower()
if difficulty == 'e':
return mystery_word.easy_words(words)
elif difficulty == 'm':
return mystery_word.medium_words(words)
elif difficulty == 'h':
return mystery_word.hard_words(words)
else:
print("That is not a valid choice. Please choose again.")
return choose_difficulty()
###############################################################################
def guess(word_choice):
"""Takes a string and intitializes two empty lists. Asks the user
to guess a letter in the string. If the guess is 'quit' the function breaks.
If the guess is in the string it adds that letter to a list of correct
guesses and if it is not in the string it adds the letter to another list.
The function will continue to loop and ask for user input until either the
list of correct guesses contains all the letters in the string or the list
of incorrect guesses reaches a length of 8, in which case the function will
return a win or loss message (dependent on outcome).
Keyword Arguments:
word_choice -- the string the user will try to guess
"""
word = word_choice
incorrect_guesses = []
correct_guesses = []
while win_lose(word, correct_guesses, incorrect_guesses):
guess = guess_input((correct_guesses + incorrect_guesses))
#^handles user input
if guess != 'quit':
if check_guess(word, guess):
correct_guesses.append(guess)
else:
incorrect_guesses.append(guess)
else:
os.system('clear')
print("Goodbye! Thanks for Playing!")
break
os.system('clear')
guess_display(word, correct_guesses, incorrect_guesses)
###############################################################################
def guess_input(guess_list):
"""Takes a list. Checks to see if input from user is:
>equal to the string 'quit'
>comprised of alphabet characters and is no longer
than a single character (if not 'quit')
>Not already in the given list.
If input passes the given conditions, return the input. Otherwise, print
a message and rerun the function.
Keyword Arguments:
guess_list -- the list of guesses to check input against.
"""
guess = input("(Type \"quit\" to exit) Please guess a letter >>> ").lower()
if guess == 'quit' or (guess.isalpha() and len(guess) < 2):
if guess not in guess_list:
return guess
else:
print("You already guessed that letter!")
return guess_input(guess_list)
else:
print("Invalid guess. Please try again.")
return guess_input(guess_list)
###############################################################################
def check_guess(word, guess):
"""Takes two strings. Checks if 2nd string is in the 1st string and returns
True if it is, False if it is not.
Keyword Arguments:
word -- string to be checked against.
guess -- string must be in the 'word' argument for function to return True
"""
if guess in word:
return True
else:
return False
###############################################################################
def guess_display(word, correct_guesses, incorrect_guesses):
"""Takes a string and two lists. Prints a user-friendly output based on
the length of the lists and how the contents of the first list matches the
characters in the string.
Keyword Arguments:
word -- string that correct_guesses is checked against
correct_guesses -- list that is checked against "word"
incorrect_guesses -- list whose length and contents is used in first line
of output
"""
number_incorrect = str(len(incorrect_guesses))
incorrect = " ".join(incorrect_guesses)
print("Incorrect guesses({}/8): [{}]\n".format(number_incorrect, incorrect.upper()))
print(mystery_word.display_word(word, correct_guesses) + "\n")
###############################################################################
def win_lose(word, correct_guesses, incorrect_guesses, word_list = None):
"""Checks if the user has won or lost the game. If the
list of correct guesses contains all the letters in the string or the list
of incorrect guesses reaches a length of 8 the function will
return a win or loss message (dependent on outcome) which will in turn
return False. If neither of condition is met, the function returns True.
Keyword Arguments:
word -- the string correct_guesses is compared to.
correct_guesses -- list of characters compared to 'word' to determine if
user has won.
incorrect_guesses -- list of characters. If length of list exceeds 7, user
has lost.
word_list -- Default None. If a list is input the function will hand that
list to the you_lose function instead of the first argument ("word")
"""
if type(word_list) == list:
choice = word_list
else:
choice = word
if mystery_word.is_word_complete(word, correct_guesses):
return you_win(word)
elif len(incorrect_guesses) > 7:
return you_lose(choice)
else:
return True
###############################################################################
def you_win(word):
"""Clears the screen and informs the user they have won, shows them the
full word they guessed, and returns False.
Keyword Arguments:
word -- the string the user was trying to guess
"""
os.system('clear')
print("Congratulations! You Won!!")
print("You correctly guessed the Word!")
print("It was {}".format(word.upper()))
return False
###############################################################################
def you_lose(word):
"""Clears the screen and informs the user they have lost, shows them the
full word they failed to guess, and returns False. If the input to the
function is a list, it will pick a word at random to reveal to the user
(used for "demon" mode)
Keyword Arguments:
word -- the string the user was trying to guess/the list of possible words
for "demon" mode
"""
if type(word) == list:
word = mystery_word.random_word(word)
os.system('clear')
print("I'm sorry, but you've run out of guesses!")
print("The word was {}".format(word.upper()))
return False
###############################################################################
def welcome_rules():
"""Clears the screen and prints a welcome message"""
os.system('clear')
print("Welcome to the Mystery Word Game!")
print("Guess the word one letter at a time.")
###############################################################################
def word_length(word):
"""Takes a string. Determines the length of string and prints a statement
informing user of the length.
Keyword Arguments:
word -- string whose length is determined
"""
length = str(len(word))
print("Your word is {} letters long.".format(length))
###############################################################################
def play_again():
"""Asks the user for input and, if input is valid, returns True or False
depending on the input. If input invalid, reruns function.
"""
yes_no = input("Would you like to play again (y/n)? ").lower()
if yes_no not in "yn" or len(yes_no) > 1:
return play_again()
elif yes_no == "y":
return True
else:
return False
###############################################################################
def main():
"""Initializes game and controls game flow."""
welcome_rules()
if gates_of_hell():
return demon_words.main()
else:
words = choose_difficulty()
word = mystery_word.random_word(words)
word_length(word)
guess(word)
if play_again():
return main()
else:
os.system('clear')
return
###############################################################################
if __name__ == '__main__':
main()