1
- # https://www.wordunscrambler.net/word-list/wordle-word-list for the list of words
1
+ # https://www.wordunscrambler.net/word-list/wordle-word-list
2
+ # for the list of words
2
3
3
4
from selenium import webdriver
4
5
from selenium .webdriver .common .by import By
@@ -21,7 +22,9 @@ def __init__(self):
21
22
self .present_letters = set ([])
22
23
self .absent_letters = set ([])
23
24
self .word = ['' ] * 5
24
- self .word_to_try = "slate" # Creators recommend “Slate” as starting word
25
+
26
+ # Creators recommend “Slate” as starting word
27
+ self .word_to_try = "slate"
25
28
26
29
27
30
# Function that is called by the KeyboardListener
@@ -33,7 +36,8 @@ def on_release(key):
33
36
34
37
# Get the status of the letters in the wordle
35
38
def get_row_results (game_row ):
36
- tiles = game_row .find_elements (By .XPATH , ".//*[contains(@class, 'Tile-module_tile__')]" )
39
+ tiles = game_row .find_elements (
40
+ By .XPATH , ".//*[contains(@class, 'Tile-module_tile__')]" )
37
41
row_results = []
38
42
res_to_int = {
39
43
"correct" : 1 ,
@@ -57,7 +61,8 @@ def enter_word(word):
57
61
time .sleep (2 )
58
62
59
63
60
- # Check word length, used in get_list_of_words() if the source list contains words with different length
64
+ # Check word length, used in get_list_of_words()
65
+ # if the source list contains words with different length
61
66
def check_word_length (word ):
62
67
if len (word ) != 5 :
63
68
return False
@@ -73,7 +78,8 @@ def check_letter_in_word(letter, word):
73
78
return False
74
79
75
80
76
- # Check if the letter in the finder object is the same as the letter in the possible answer
81
+ # Check if the letter in the finder object
82
+ # is the same as the letter in the possible answer
77
83
def check_match (finder_word_letter , possible_word_letter ):
78
84
if finder_word_letter == possible_word_letter :
79
85
return True
@@ -98,43 +104,58 @@ def solving_algorithm(res, finder):
98
104
99
105
# Compare the word with the results of the wordle
100
106
for letter in range (len (word )):
101
- if res [letter ] == 1 : # Case when the status of the letter is "correct"
107
+ # Case when the status of the letter is "correct"
108
+ if res [letter ] == 1 :
102
109
print (f"Letter { word [letter ]} is correct" )
103
110
finder .word [letter ] = word [letter ]
104
111
print (finder .word )
105
112
if word [letter ] in finder .absent_letters :
106
113
finder .absent_letters .remove (word [letter ])
107
114
108
- elif res [letter ] == 0 : # Case when the status of the letter is "present" (present but at the wrong position)
115
+ # Case when the status of the letter is "present"
116
+ # (present but at the wrong position)
117
+ elif res [letter ] == 0 :
109
118
print (f"Letter { word [letter ]} is present" )
110
119
finder .present_letters .add (word [letter ])
111
- # We keep all the words that don't match the pattern of the word entered
120
+ # We keep all the words that don't match
121
+ # the pattern of the word entered
112
122
finder .possible_words = list (
113
- filter (lambda x_word : not check_match (word [letter ], x_word [letter ]), finder .possible_words ))
123
+ filter (lambda x_word :
124
+ not check_match (word [letter ], x_word [letter ]),
125
+ finder .possible_words ))
114
126
115
127
else : # Case when the status of the letter is "absent"
116
128
print (f"Letter { word [letter ]} is absent" )
117
129
if word [letter ] not in finder .present_letters :
118
130
finder .absent_letters .add (word [letter ])
119
131
120
- # We keep all the words that don't match the pattern of the word entered
132
+ # We keep all the words that don't match
133
+ # the pattern of the word entered
121
134
finder .possible_words = list (
122
- filter (lambda x_word : not check_match (word [letter ], x_word [letter ]), finder .possible_words ))
135
+ filter (lambda x_word :
136
+ not check_match (word [letter ], x_word [letter ]),
137
+ finder .possible_words ))
123
138
124
139
print ("\n " )
125
140
print ("Updating list of possible words ..." )
126
141
127
142
# Update list of words
128
143
for absent in finder .absent_letters :
129
144
finder .possible_words = list (
130
- filter (lambda x_word : not check_letter_in_word (absent , x_word ), finder .possible_words ))
145
+ filter (lambda x_word :
146
+ not check_letter_in_word (absent , x_word ),
147
+ finder .possible_words ))
131
148
for present in finder .present_letters :
132
149
finder .possible_words = list (
133
- filter (lambda x_word : check_letter_in_word (present , x_word ), finder .possible_words ))
150
+ filter (lambda x_word :
151
+ check_letter_in_word (present , x_word ),
152
+ finder .possible_words ))
134
153
for i in range (len (finder .word )):
135
154
if finder .word [i ] != "" :
136
155
finder .possible_words = list (
137
- filter (lambda x_word : check_match (x_word [i ], finder .word [i ]), finder .possible_words ))
156
+ filter (lambda x_word :
157
+ check_match (x_word [i ], finder .word [i ]),
158
+ finder .possible_words ))
138
159
139
160
# Update the next word to try
140
161
finder .word_to_try = finder .possible_words [0 ]
@@ -149,7 +170,8 @@ def solving_algorithm(res, finder):
149
170
150
171
def main ():
151
172
# Start the browser
152
- browser = webdriver .Firefox (service = FirefoxService (GeckoDriverManager ().install ()))
173
+ browser = webdriver .Firefox (
174
+ service = FirefoxService (GeckoDriverManager ().install ()))
153
175
browser .get ("https://www.nytimes.com/games/wordle/index.html" )
154
176
155
177
# Create the finder object (cf. class Finder)
@@ -162,12 +184,14 @@ def main():
162
184
print ("Starting program\n " )
163
185
listener .join ()
164
186
165
- # With "suppress=True", duplicate key presses are not sent to the application
166
- # but for some reason I need to add a delay for the first input to be sent.
187
+ # With "suppress=True", duplicate key presses are not sent
188
+ # to the application but for some reason I need to add a delay
189
+ # for the first input to be sent.
167
190
time .sleep (1 )
168
191
169
192
# Get the game rows
170
- game_rows = browser .find_elements (By .XPATH , "//*[contains(@class, 'Row-module_row__')]" )
193
+ game_rows = browser .find_elements (
194
+ By .XPATH , "//*[contains(@class, 'Row-module_row__')]" )
171
195
172
196
# Enter words until the game is over or the wordle is solved
173
197
for i in range (guesses_left , 0 , - 1 ):
0 commit comments