-
Notifications
You must be signed in to change notification settings - Fork 0
/
pupChase.py
318 lines (276 loc) · 10.9 KB
/
pupChase.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
# CREATED BY: Diana Arreola
import random
import os
import math
import sys
import puppy as pup
def randomizeNoun():
""" randomeizeNoun returns a random noun (str) from
the nouns.txt file
"""
with open("nouns.txt") as noun_file:
nouns = noun_file.read().split()
random_noun = random.choice(nouns)
random_noun = random_noun.upper()
return random_noun
def addGuessedLetter(codeword,letter,dashed_codeword):
""" addGuessedLetter returns the guessed codeword with the
correct guessed letter added to it
input: the codeword (str), the letter guessed (str)
and the guessed codeword (str)
output: guessed codeword (str)
"""
dashed_codeword = list(dashed_codeword)
for i in range(len(codeword)):
if codeword[i] == letter:
dashed_codeword[i] = letter
print(''.join(dashed_codeword))
return ''.join(dashed_codeword)
def validAnswer(dashed_codeword):
""" validAnswer returns T if the guessed codeword is
guessed correctly and False if the guessed codeword
is not guessed completely
input: the guessed codeword (str)
output: True or False (Boolean)
"""
dashed_codeword = list(dashed_codeword)
for i in range(len(dashed_codeword)):
if dashed_codeword[i] == '_':
return False
return True
def userInput():
""" userInput returns the users input
output: user input (str)
"""
letter_guessed = input("Please enter your guess: \n")
letter_guessed = checkInput(letter_guessed)
letter_guessed = letter_guessed.upper()
return letter_guessed
def playAgain(user_input):
""" playAgain either ends the game or restarts the game
based on the user input.
input: user input (str)
output: game ends or game restarts
"""
if (user_input == "N") | (user_input == "n") | (user_input == "No") | (user_input == "NO")| (user_input == "no"):
print('Goodbye')
sys.exit()
elif (user_input == "Y") | (user_input == "y") | (user_input == "Yes") | (user_input == "yes") | (user_input == "YES"):
print('Lets play again')
return pupChase()
else:
print('I did not understand you. Bye!')
return 0
def checkInput(user_input):
""" checkInput checks whether a users input is a valid
letter. If it is not, ' ' is returned. If it is,
the users input is returned
input: user input (str)
output: ' '(str) or user
"""
intArr = ['1','2','3','4','5','6','7','8','9','0']
specialChars = ['+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^',
'~', '*', '?', ':', '/','<','>','.',',',';','$','%','*']
len_input = len(user_input)
if len_input > 1:
print('Please only guess one letter.\n')
return ' '
elif user_input in intArr:
print('Please only guess letters not numbers.\n')
return ' '
elif user_input in specialChars:
print('Please only guess letters not special characters.\n')
return ' '
else:
return user_input
def correctGuess(chances_left, incorrect_guesses, guessed_codeword):
""" correctGuess handles the case where the user
guesses a letter correctly.
input: chances left to play (str), the codeword (str)
, the array of incorrect guesses of letters (arr),
and the guessed codeword (str)
output: chances left to play (str) and new letter guessed
from user (str)
"""
print("Correct! You're closer to guessing the codeword.\n")
print(pup.x[abs(chances_left-6)])
print("Incorrect guesses: ")
print(' '.join(letter for letter in incorrect_guesses))
print("Codeword: ")
print(guessed_codeword)
print("Number of dictionary matches:")
print(bonusWords(incorrect_guesses,guessed_codeword))
letter_guessed = userInput()
return letter_guessed
def incorrectGuess(chances_left, codeword, incorrect_guesses, guessed_codeword):
""" incorrectGuess handles the case where the user
guesses a letter incorrectly.
input: chances left to play (str), the codeword (str)
, the array of incorrect guesses of letters (arr),
and the guessed codeword (str)
output: chances left to play (str) and new letter guessed
from user (str)
"""
chances_left -= 1
# lost game
if chances_left == 0:
return lostGame(codeword)
# incorrect guess
print("Incorrect! The puppy inches closer to the fence.\n")
print(pup.x[abs(chances_left-6)])
print("Incorrect guesses: ")
print(' '.join(letter for letter in incorrect_guesses))
print("Codeword: ")
print(guessed_codeword)
print("Number of dictionary matches:")
print(bonusWords(incorrect_guesses,guessed_codeword))
letter_guessed = userInput()
return chances_left, letter_guessed
def lostGame(codeword):
""" lostGame prints ou losing statement and asks for
user prefernce
input: the codeword (str)
output: game ends or game restarts based on user
prefernce
"""
print("Incorrect! The puppy escaped!\n")
print(pup.x[6])
print("The codeword is: ", codeword, ".")
answer = input("Would you like to play again (Y/N)?\n")
return playAgain(answer)
def wonGame(codeword):
""" wonGame prints ou winning statement and asks for
user prefernce
input: the codeword (str)
output: game ends or game restarts based on user
preference
"""
print("Correct! You saved your puppy!\n")
print("The codeword is: ", codeword, ".")
answer = input("Would you like to play again (Y/N)?\n")
return playAgain(answer)
def bonusWords(incorrect_guesses, guessed_codeword):
""" bonusWords displays how many words
in the provided dictionary are potentially
correct codewords given the correct and
incorrect letter guesses made so far.
input: the codeword (str), the array of
incorrect letters guessed (arr)
output: an integer which is the amount of matches
in the dict
"""
len_codeword = len(guessed_codeword)
# array of wprd
with open("nouns.txt") as noun_file:
nouns = noun_file.read().split()
# first filter by codeword length
appropriate_length = list(filter(lambda x: len(x)==len_codeword,nouns))
# initialize dictionary
appropriate_letters = {}
for word in appropriate_length:
appropriate_letters[word] = 0
# filter out incorrect letters using the incorrect guesses array
# from the appropriate length array
# Utilizing a dictionary to associate every word in appropriate length
# with a key value that represents the number of incorrect letters in it
filtered_words = []
for i in incorrect_guesses:
for j in appropriate_length:
if i in j:
appropriate_letters[i] += 1
for i in appropriate_letters:
if appropriate_letters[i] == 0:
filtered_words.append(i)
# Find the char and index of the letters in
# the guessed codeword
letterAndIndex = findLetterAndIndex(guessed_codeword)
# Find the possible bonus words
bonusWords = compareStrings(filtered_words, letterAndIndex)
return len(bonusWords)
def findLetterAndIndex(guessed_codeword):
""" bonusWords displays how many words
in the provided dictionary are potentially
correct codewords given the correct and
incorrect letter guesses made so far.
input: the guessed codeword (str)
output: dict of correct guessed letters
associated with its index
"""
# gives letter and index of word
letter = {}
for i in range(len(guessed_codeword)):
if guessed_codeword[i] != '_':
letter[guessed_codeword[i]] = i
return letter
def compareStrings(filtered_words, letterAndIndex):
""" compareStrings is a helper function for bonus
words that compares the current guessed codeword
againt the pre-filtered words
input: an array of pre-filtered words (arr)
and dict of correct letter and index
output: an array of the possible dictionary matched
"""
bonusWords = {}
# initialize dictionary
for word in filtered_words:
bonusWords[word] = 0
for char in letterAndIndex:
for i in range(len(filtered_words)):
if filtered_words[i][letterAndIndex[char]].upper() == char:
bonusWords[filtered_words[i]] += 1
letter_length = len(letterAndIndex)
bonus = []
for word in bonusWords:
if bonusWords[word] == letter_length:
bonus.append(word)
return bonus
def pupChase():
""" Puppy chase returns a game for guessing a codeword
one letter at a time. If the letter does not
exist in the codeword, the puppy is runs closer to
the fence. If the letter exists, the blanks that
correspond to the position of those letters in the
codeword are replaced by the letter. If all the
letters of the codeword are revealed before the
puppy escapes under the fence, you win. Otherwise,
puppy escapes under the fence.
"""
codeword = randomizeNoun()
len_codeword = len(codeword)
guessed_codeword = len_codeword*'_'
incorrect_guesses = []
chances_left = 6
# initial display
print("Puppy Chase\n")
print("Instructions: Get your puppy before it escapes under the fence by guessing letters in the codeword.\n")
print(pup.x[0])
print("Incorrect guesses: ")
print("None\n")
print("Codeword: ")
print(guessed_codeword,"\n")
letter_guessed = userInput()
while chances_left != 0:
if letter_guessed in guessed_codeword:
print("You can only guess that letter once, please try again.\n")
letter_guessed = userInput()
elif letter_guessed in codeword:
guessed_codeword = addGuessedLetter(codeword, letter_guessed, guessed_codeword)
# checks codeword is correct
if validAnswer(guessed_codeword):
return wonGame(codeword)
else:
letter_guessed = correctGuess(chances_left, incorrect_guesses, guessed_codeword)
elif letter_guessed in incorrect_guesses:
print("Already Guessed That!\n")
letter_guessed = userInput()
elif letter_guessed not in codeword:
if letter_guessed != ' ' and letter_guessed != '':
incorrect_guesses.append(letter_guessed)
chances_left,letter_guessed = incorrectGuess(chances_left, codeword, incorrect_guesses, guessed_codeword)
else:
print("I cannot understand your input.\n")
letter_guessed = userInput()
def main():
pupChase()
if __name__== "__main__":
main()