forked from tiyd-python-2015-05/mystery-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
183 lines (137 loc) · 3.77 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
import mystery_word as mw
import os
def main():
"""
Runs the main loop named loop
and calls play_again to ask
if you want to play again
"""
words = import_words('/usr/share/dict/words')
loop(words)
while play_again():
loop(words)
os.system('clear')
def loop(words):
"""
The main loop
displays information to the user
and processes input from the user
"""
os.system('clear')
welcome()
word_list = ask_difficulty(words)
word = mw.random_word(word_list)
guesses = []
correct_guess = []
number_of_letters(word)
while True:
if len(guesses) > 7:
you_lose(word)
break
this_guess = guess()
while this_guess in guesses or this_guess in correct_guess:
print("You already guessed {}!".format(this_guess.upper()))
this_guess = guess()
guesses.append(this_guess)
if this_guess == 'quit':
os.system('clear')
break
if guesses[-1] in word:
correct_guess.append(guesses.pop())
if mw.is_word_complete(word, correct_guess):
you_win(word)
break
os.system('clear')
print("Incorrect Guesses ({}, 8 max): [ {} ]".format(len(guesses),
" ".join(guesses).upper()))
print(mw.display_word(word, correct_guess))
def you_lose(word):
"""
Displays a lost game message
"""
os.system('clear')
print("You did not guess the word!")
print("The word was: {}".format(word))
def you_win(word):
"""
Displays a won game message
"""
os.system('clear')
print("You guessed the word!\nCongratulations!")
print("It was {}".format(word))
def play_again():
"""
Asks if player wants to play again
and returns True if they do
False otherwise
"""
yes_no = input("Would you like to play again? [Y]es [n]o : ")
if len(yes_no) > 1 or yes_no.lower() not in 'yn':
return play_again()
if yes_no == 'y':
os.system('clear')
return True
else:
os.system('clear')
return False
def ask_difficulty(words):
"""
Asks the player what difficulty
and sets the difficulty based
on their choice (if valid)
"""
diff = input("What difficulty? [E]asy [m]edium [h]ard : ")
word_list = []
diff = diff.lower()
if diff not in 'emh':
return ask_difficulty(words)
os.system('clear')
if diff == 'e':
word_list = mw.easy_words(words)
elif diff == 'm':
word_list = mw.medium_words(words)
else:
word_list = mw.hard_words(words)
return word_list
def number_of_letters(word):
"""
Displays the length of the word
"""
print("The number of letters in your word is: {}".format(len(word)))
def welcome():
"""
Welcomes the player to the game
"""
print("Welcome to the Mystery Word game!")
def guess():
"""
Takes player input of a single letter
or 'quit'
and returns the guess if valid, repeats
if not, and quits the main loop if 'quit'
"""
this_guess = input("Type \"quit\" to exit. Your Guess: ")
if len(this_guess) > 1 or not this_guess.isalpha():
if this_guess.lower() == 'quit':
return 'quit'
print("Not a letter. Guess again:")
return guess()
return this_guess
def import_words(path):
"""
Imports the list of words from the
specified path
requires one word per line
"""
text = ''
with open(path, 'r') as file:
text = file.readlines()
return clean_text(text)
def clean_text(text):
"""
Strips the text, lowercases it
and returns it in list form
"""
return [line.strip().lower() for line in text]
if __name__ == '__main__':
main()