Skip to content

Commit 70d535e

Browse files
committed
Bug fix + Code improvements
1 parent 8dbfa5e commit 70d535e

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

wordle_solver/wordle_solver.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
# - the letters that are present but not in the right position
1414
# - the letters that are absent
1515
# - the letters that are correct and their position in a list
16+
# - the word that is currently being tested
1617

1718
class Finder:
1819
def __init__(self):
1920
self.possible_words = get_list_of_words()
2021
self.present_letters = set([])
2122
self.absent_letters = set([])
2223
self.word = [''] * 5
24+
self.word_to_try = "slate" # Creators recommend “Slate” as starting word
2325

2426

2527
# Function that is called by the KeyboardListener
@@ -90,18 +92,24 @@ def get_list_of_words():
9092

9193

9294
# Algorithm that solve the wordle
93-
def solving_algorithm(word, res, finder):
95+
def solving_algorithm(res, finder):
9496
print("Starting solving algorithm")
97+
word = finder.word_to_try
9598

9699
# Compare the word with the results of the wordle
97100
for letter in range(len(word)):
98101
if res[letter] == 1: # Case when the status of the letter is "correct"
99102
print(f"Letter {word[letter]} is correct")
100103
finder.word[letter] = word[letter]
101104
print(finder.word)
105+
102106
elif res[letter] == 0: # Case when the status of the letter is "present" (present but at the wrong position)
103107
print(f"Letter {word[letter]} is present")
104108
finder.present_letters.add(word[letter])
109+
# We keep all the words that don't match the pattern of the word entered
110+
finder.possible_words = list(
111+
filter(lambda x_word: not check_match(word[letter], x_word[letter]), finder.possible_words))
112+
105113
else: # Case when the status of the letter is "absent"
106114
print(f"Letter {word[letter]} is absent")
107115
if word[letter] not in finder.present_letters:
@@ -122,6 +130,9 @@ def solving_algorithm(word, res, finder):
122130
finder.possible_words = list(
123131
filter(lambda x_word: check_match(x_word[i], finder.word[i]), finder.possible_words))
124132

133+
# Update the next word to try
134+
finder.word_to_try = finder.possible_words[0]
135+
125136
print("List of possible words updated !\n")
126137

127138
print("letter not in the right position : ", finder.present_letters)
@@ -152,25 +163,14 @@ def main():
152163
# Get the game rows
153164
game_rows = browser.find_elements(By.CLASS_NAME, 'Row-module_row__dEHfN')
154165

155-
# Creators recommend “Slate” as starting word
156-
first_string = "slate"
157-
158-
# Enter the first word
159-
enter_word(first_string)
160-
res = get_row_results(game_rows[0])
161-
solving_algorithm(first_string, res, finder)
162-
guesses_left -= 1
163-
164-
time.sleep(1)
165-
166-
# Enter the next words
166+
# Enter words until the game is over or the wordle is solved
167167
for i in range(guesses_left, 0, -1):
168-
enter_word(finder.possible_words[0])
169-
res = get_row_results(game_rows[guesses_left + 1 - i])
170-
solving_algorithm(finder.possible_words[0], res, finder)
168+
enter_word(finder.word_to_try)
169+
res = get_row_results(game_rows[guesses_left - i])
170+
solving_algorithm(res, finder)
171171
if len(finder.possible_words) == 1:
172-
enter_word(finder.possible_words[0])
173-
print(f"The word is : {finder.possible_words[0]}\n")
172+
enter_word(finder.word_to_try)
173+
print(f"The word is : {finder.word_to_try}\n")
174174
break
175175
time.sleep(1)
176176

0 commit comments

Comments
 (0)