13
13
# - the letters that are present but not in the right position
14
14
# - the letters that are absent
15
15
# - the letters that are correct and their position in a list
16
+ # - the word that is currently being tested
16
17
17
18
class Finder :
18
19
def __init__ (self ):
19
20
self .possible_words = get_list_of_words ()
20
21
self .present_letters = set ([])
21
22
self .absent_letters = set ([])
22
23
self .word = ['' ] * 5
24
+ self .word_to_try = "slate" # Creators recommend “Slate” as starting word
23
25
24
26
25
27
# Function that is called by the KeyboardListener
@@ -90,18 +92,24 @@ def get_list_of_words():
90
92
91
93
92
94
# Algorithm that solve the wordle
93
- def solving_algorithm (word , res , finder ):
95
+ def solving_algorithm (res , finder ):
94
96
print ("Starting solving algorithm" )
97
+ word = finder .word_to_try
95
98
96
99
# Compare the word with the results of the wordle
97
100
for letter in range (len (word )):
98
101
if res [letter ] == 1 : # Case when the status of the letter is "correct"
99
102
print (f"Letter { word [letter ]} is correct" )
100
103
finder .word [letter ] = word [letter ]
101
104
print (finder .word )
105
+
102
106
elif res [letter ] == 0 : # Case when the status of the letter is "present" (present but at the wrong position)
103
107
print (f"Letter { word [letter ]} is present" )
104
108
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
+
105
113
else : # Case when the status of the letter is "absent"
106
114
print (f"Letter { word [letter ]} is absent" )
107
115
if word [letter ] not in finder .present_letters :
@@ -122,6 +130,9 @@ def solving_algorithm(word, res, finder):
122
130
finder .possible_words = list (
123
131
filter (lambda x_word : check_match (x_word [i ], finder .word [i ]), finder .possible_words ))
124
132
133
+ # Update the next word to try
134
+ finder .word_to_try = finder .possible_words [0 ]
135
+
125
136
print ("List of possible words updated !\n " )
126
137
127
138
print ("letter not in the right position : " , finder .present_letters )
@@ -152,25 +163,14 @@ def main():
152
163
# Get the game rows
153
164
game_rows = browser .find_elements (By .CLASS_NAME , 'Row-module_row__dEHfN' )
154
165
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
167
167
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 )
171
171
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 " )
174
174
break
175
175
time .sleep (1 )
176
176
0 commit comments