-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.py
167 lines (141 loc) · 6.37 KB
/
wordle.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
#Nikhill Andrew
import random
from display_utility import green
from display_utility import yellow
from display_utility import grey
from words import words
def check_word(secret, guess):
"""Checks the guessed word (guess) against the secret word (secret) and returns a clue.
The clue is a list of strings indicating the appropraite color that is associated with each letter in the guess based on the secret"""
clue = ["grey", "grey", "grey", "grey", "grey"]
RemSec = "" #String to remember all non-green characters of the secret
RemGue = "" #String to remember all non-green characters of the guess
length = len(guess)
for i in range(length): #This block of code checks to see if a character is green (correct letter in the correct spot)
if guess[i] == secret[i]:
clue[i] = "green"
RemSec = RemSec + "-"
RemGue = RemGue + "-"
else:
RemSec = RemSec + secret[i]
RemGue = RemGue + guess[i]
for t in range(length): #This block of code checks to see if a character is either yellow (correct letter in wrong sport) or grey (wrong letter completely)
if RemGue[t] == "-":
continue
elif RemGue[t] in RemSec:
clue[t] = "yellow"
RemSec = RemSec.replace(RemGue[t], "-", 1)
else:
clue[t] = "grey"
return clue
def known_word(clues):
"""Iterates over clues (a list of tuples) and returns a string that shows the user which letters they have guessed correctly in the correct position (green letters from all guessed words)"""
OutputList = ["_","_","_","_","_"]
for tup in clues: #This block of code iterates through each tuple in 'clues' to find the green characters from each guess
rang = len(tup[1]) #rang is short for range and tup is short for tuple
for index in range(rang):
if (tup[1])[index] == "green":
OutputList[index] = (tup[0])[index]
else:
continue
Output = ""
for elem in OutputList: #Turns the OutputList elements into a string
Output = Output + elem
return Output
def no_letters(clues):
"""Iterates through the list of tuples containing guesses and clues and returns a string containing all of the grey letters contained in guesses, these are characters not found anywhere in the secret word"""
grey = ""
for tupl in clues: #This block of code iterates through clues to first find if a letter in a guess is green or yellow or grey and only adds grey letters to the 'grey' string
ran = len(tupl[1])
guess = tupl[0]
for ind in range(ran):
color = (tupl[1])[ind]
letter = (tupl[0])[ind]
if color == "green" or color == "yellow":
if letter in grey:
grey = grey.replace(letter, "")
else:
continue
elif color == "grey":
if letter in guess[0:ind]:
continue
elif letter in grey:
continue
else:
grey = grey + letter
else:
continue
grey = alpha(grey)
return grey
def yes_letters(clues):
"""Iterates over the list of tuples, clues, and outputs a string indicating which guessedbletters are in the secret word based on yellow and green hints given so far"""
yes = ""
for tu in clues: #This block of code iterates through clues to find green or yellow letters
length = len(tu[1])
for j in range(length):
color = (tu[1])[j]
lett = (tu[0])[j]
if color == 'green' or color == 'yellow':
if lett in yes:
continue
else:
yes = yes+lett
else:
continue
yes = alpha(yes)
return yes
def game(secret):
"""Implements a playable wordle game which interacts with a user and prompts them to guess the secret word.
It will continuously check the user's guesses against the 'secret' parameter and perform all other functions of the wordle game by calling previous functions.
This function ends when the user's guess is equal to the secret word. It does not return anything"""
clues = []
count = 0
l_caseSecret = secret.lower()
print('Known: ',known_word(clues))
print('Green/Yellow Letters: ',yes_letters(clues))
print('Grey Letters: ', no_letters(clues))
while count < 6: #This while loop performs the required actions needed with each guess made by the user
guess = input('> ')
capt_guess = guess.upper()
if (len(guess) != 5) or (guess not in words):
print('Not a word. Try again')
elif guess == l_caseSecret:
tup = (capt_guess, check_word(l_caseSecret,guess))
clues.append(tup)
count = 6
else:
if count == 5:
tup = (capt_guess, check_word(l_caseSecret,guess))
clues.append(tup)
count = count + 1
else:
tup = (capt_guess, check_word(l_caseSecret,guess))
clues.append(tup)
print_word(clues)
print("Known:",known_word(clues))
print("Green/Yellow Letters:", yes_letters(clues))
print("Grey Letters:", no_letters(clues))
count = count+1
print_word(clues) #This code will execute only after the user has guessed the secret word correctly or they have used all six guesses.
capt_secret = secret.upper()
print("Answer:", capt_secret)
def print_word(clues):
"""Prints each guess that has been made and stored in the list 'clues' with the corresponding background color"""
for word in clues:
for index in range(len(word[1])):
color = (word[1])[index]
letter = (word[0])[index]
if color == 'green':
green(letter)
elif color == 'yellow':
yellow(letter)
else:
grey(letter)
print("")
def alpha(str):
"""Returns an alphabetized version of the string str"""
ret = ''.join(sorted(str)) #sorted will sort its argument from lowest to highest value. .join will join all items into one string
return ret
if __name__ == "__main__":
rand = random.randint(0,len(words)-1)
game(words[rand])