-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.py
294 lines (216 loc) · 7.95 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
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
import random
from flask import Flask, request, jsonify
from flask_cors import CORS
# Initializing flask app
app = Flask(__name__)
CORS(app)
# Input all possible words from file (extracted from wordle website)
def read_words_from_file():
with open('public/five_letter_words.txt', 'r') as file:
words_array = [line.strip() for line in file]
return words_array
possible_words = read_words_from_file()
# Send info
@app.route('/api/start', methods=['POST', 'OPTIONS'])
def start_wordle():
"""
Initializes games buy calculating mathematically best starter word
:route /api/start: From startWordle() on front-end
:returns: A JSON object containing calculated starter word
"""
global possible_words
possible_words = read_words_from_file()
# Calculates mathematically best starter word
suggested_word = bestWord(possible_words, letterFreq(possible_words))
return jsonify({"suggestedWord": suggested_word})
# Recieve info
@app.route('/api/guess', methods=['POST'])
def process_guess():
"""
Calculates best guess given previous guess and color data
:route /api/guess: From proccessBotGuess on front-end
:returns: A JSON object containing next best guess
"""
# Use global version of possible_words to prevent decleration
global possible_words
# Process the guess sent from the React app and return the next guess
current_guess = request.json.get('currentGuess')
letter_colors = request.json.get('letterColors')
# Update the global variable with the filtered list
possible_words = word_remover(letter_colors, current_guess)
suggestion = bestWord(possible_words, letterFreq(possible_words))
possible_words.remove(suggestion)
return jsonify({"nextGuess": suggestion})
"""
API ABOVE
WORDLE ALGORITHM BELOW
"""
def badLetters(result, guess):
"""
Finds incorrect letters in word
:param result: Color feedback in the form "bbybg"
:param guess: Previous guess
:return: List of all letters associated with "b"
"""
bad_letters = []
for i in range(0, 5):
if result[i] == "b":
bad_letters.append(guess[i])
return bad_letters
def partialLetters(result, guess):
"""
Finds correct letters that are misplaced in word
:param result: Color feedback in the form "bbybg"
:param guess: Previous guess
:return: List of all letters associated with "y"
"""
partial_letters = []
for i in range(0, 5):
if result[i] == "y":
partial_letters.append([guess[i], i])
return partial_letters
def correctLetters(result, guess):
"""
Finds fully correct letters in word
:param result: Color feedback in the form "bbybg"
:param guess: Previous guess
:return: List of all letters associated with "g"
"""
correct_letters = []
for i in range(0, 5):
if result[i] == "g":
correct_letters.append([guess[i], i])
return correct_letters
def word_remover(result, guess):
"""
Returns a list of words that don't allign with result or guess
:param result: letters in word or in correct position in format "bbybg"
:param guess: Last guessed word that resulted in param result
:return: array of filtered words that could be possibility of param result
"""
# Use global version of possible_words to prevent decleration
global possible_words
bad_letters = badLetters(result, guess) # list
partial_letters = partialLetters(result, guess) # tuple (letter, position)
correct_letters = correctLetters(result, guess) # tuple (letter, position)
# Make list of yellow and green letters
good_letters = []
for letter in correct_letters:
good_letters.append(letter[0])
for letter in partial_letters:
good_letters.append(letter[0])
# Update bad letters
for letter in bad_letters:
if letter in good_letters:
bad_letters.remove(letter)
no_bad_letters = []
# Removes words that contain a letter marked "b" in result
for word in possible_words:
acceptable_word = True
for bad_letter in bad_letters:
if bad_letter in word:
acceptable_word = False
break
if acceptable_word is True:
no_bad_letters.append(word)
contains_green = []
# Ensures word has the correct letter in position that aligns with "g" from result
for word in no_bad_letters:
acceptable_word = True
for tup in correct_letters:
letter = tup[0]
position = tup[1]
# Check if the word has the correct letter and index as the "g" from result
if word[position] != letter:
acceptable_word = False
break
if acceptable_word is True:
contains_green.append(word)
filtered_yellow = []
# Ensures word does not contain a correct letter in position marked "y"
for word in contains_green:
acceptable_word = True
for tup in partial_letters:
letter = tup[0]
position = tup[1]
if word[position] == letter:
acceptable_word = False
break
if acceptable_word is True:
filtered_yellow.append(word)
contains_yellow = []
# Ensures word has letter marked "y" in it somewhere
for word in filtered_yellow:
acceptable_word = True
for letter in good_letters:
if letter not in word:
acceptable_word = False
break
if acceptable_word is True:
contains_yellow.append(word)
correct_frequency = []
for word in contains_yellow:
acceptable_word = True
for letter in bad_letters:
if letter in good_letters:
if word.count(letter) != good_letters.count(letter):
acceptable_word = False
break
if acceptable_word is True:
correct_frequency.append(word)
return correct_frequency
def letterFreq(possible_words):
"""
Finds frequencies of letters in each position
:param possible_words: List of all possible words
:return: Dictionary of each letter and its associated frequency in possible words
"""
alphabet = "abcdefghijklmnopqrstuvwxyz"
arr = {}
for c in alphabet:
freq = [0, 0, 0, 0, 0]
for i in range(0, 5):
for w in possible_words:
if w[i] == c:
freq[i] += 1
arr.update({c: freq})
return arr
def wordScore(possible_words, frequencies):
"""
Computes a score based off letter frequencies
:param possible_words: List of all possible words
:param frequencies: Dictionary of each letter and its associated frequency in possible words
:return: Score based of letter frequencies
"""
words = {}
max_freq = [0, 0, 0, 0, 0]
for c in frequencies:
for i in range(0, 5):
if max_freq[i] < frequencies[c][i]:
max_freq[i] = frequencies[c][i]
for w in possible_words:
score = 1
for i in range(0, 5):
c = w[i]
score *= 1 + (frequencies[c][i] - max_freq[i]) ** 2
words.update({w: score})
score += random.uniform(0, 1)
return words
def bestWord(possible_words, frequencies):
"""
Finds the best word
:param possible_words: List of all possible words
:param frequencies: Dictionary of each letter and its associated frequency in possible words
:return: best calculated word
"""
max_score = 1000000000000000000 # start with a ridiculous score
best_word = "words" # start with a random word
scores = wordScore(possible_words, frequencies)
for w in possible_words:
if scores[w] < max_score:
max_score = scores[w]
best_word = w
return best_word
# Running app
if __name__ == '__main__':
app.run(debug=True)