-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordle.py
executable file
·352 lines (275 loc) · 10.6 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3
# author: Mateusz Adamowski <mateusz at adamowski dot pl>
#
# homepage: https://github.com/mateusza/wordle-game-and-solver
"""Play and solve wordle"""
import argparse
import functools
import random
CHARSETS: dict = {
'american-english': 'abcdefghijklmnopqrstuvwxyz',
'esperanto': 'abcdefghijklmnopqrstuvwxyzĉĝĥĵŝŭ',
'french': 'abcdefghijklmnopqrstuvwxyzù',
'german-medical': 'abcdefghijklmnopqrstuvwxyzßäöü',
'italian': 'abcdefghijklmnopqrstuvwxyzàèéìòù',
'polish': 'aąbcćdeęfghijklłmnńoópqrsśtuvwxyzżź',
}
def verdict(the_guess: str, the_word: str) -> str:
"Verdict"
if len(the_guess) != len(the_word):
raise ValueError(f'Length mismatch: {the_guess=} {the_word=}')
matched = [a == b for a, b in zip(the_guess, the_word)]
mismatched = [letter for ok, letter in zip(matched, the_word) if not ok]
result = ''
for is_ok, guessed_letter in zip(matched, the_guess):
if is_ok:
result += '+'
continue
if guessed_letter in mismatched:
result += '?'
mismatched.remove(guessed_letter)
continue
result += '_'
return result
def match_verdict(the_verdict: str, the_word: str, the_guess: str) -> bool:
"Check if given word would result in given verdict"
return verdict(the_guess, the_word) == the_verdict
class Wordset:
"Set of words to be used in a game or in solving"
words: set
def __init__(self, words=None):
if words is None:
words = []
self.words = {*words}
def get_random(self) -> str:
"Returns random word from set"
return random.choice([*self.words])
def get_random_list(self, length: int = 10) -> list:
"Return random list of words no longer than length."
new_length = min(length, len(self.words))
return random.sample(self.words, new_length)
@classmethod
def load_dict(cls, language: str, length: int = 5) -> 'Wordset':
"Load words from system dictionary"
location: str = '/usr/share/dict'
filename: str = f'{location}/{language}'
charset: set = cls.get_charset(language)
words: set = Wordset.load_words(filename, charset, length)
return cls(words=words)
@staticmethod
@functools.lru_cache(128)
def load_words(filename: str, only_chars=None, length: int = 5) -> set:
"Load words of given length from text file"
words = set()
if only_chars is not None:
charset = set(only_chars)
with open(filename, 'r', encoding='UTF-8') as src_file:
for line in src_file:
word = line.strip().lower()
if len(word) != length:
continue
if not set(word) < charset:
continue
words.add(word)
return words
@staticmethod
def get_charset(language: str) -> str:
"Get charset for given language"
return CHARSETS[language]
class Game:
"Class to play wordle locally"
language: str
wordset: set
charset: set
word: str
won: bool
guesses: list
verdicts: list
def __init__(self, wordset: str, charset: str):
self.wordset = wordset
self.charset = charset
def start(self, word: str = None):
"Reset the game"
if word is not None:
self.wordset.words.add(word)
self.word = word
else:
self.word = self.wordset.get_random()
self.won = False
self.guesses = []
self.verdicts = []
def guess(self, the_guess: str) -> dict:
"Make a guess, change game state"
if the_guess not in self.wordset.words:
...
elif self.won:
...
else:
try:
verd = verdict(the_guess, self.word)
self.guesses.append(the_guess)
self.verdicts.append(verd)
if set(verd) == {'+'}:
self.won = True
except ValueError:
verd = None
result = {
'guess': self.guesses[-1] if self.guesses else None,
'verdict': self.verdicts[-1] if self.verdicts else None,
'won': self.won
}
return result
@classmethod
def new(cls, language: str, word: str = None, length: int = 5):
"Create a new game object, load wordset in given language, start the game."
wordset = Wordset.load_dict(language, length=length)
charset = Wordset.get_charset(language)
game = cls(wordset=wordset, charset=charset)
game.start(word=word)
return game
@staticmethod
def color_verdict(the_verdict: str) -> str:
"Return color-coded verdict"
translations = '_⬛+🟩?🟨'
trans_table = {ord(a): b for a, b in zip(translations[0::2], translations[1::2])}
return the_verdict.translate(trans_table)
class Solver:
"Solve wordle challenge"
language: str
wordset: Wordset
charset: set
guesses: list
verdicts: list
_possible: Wordset
def __init__(self, wordset: str, charset: str):
self.wordset = wordset
self.charset = charset
def start(self):
"Initialize the solver"
self.guesses = []
self.verdicts = []
self._possible = Wordset(words=self.wordset.words)
@classmethod
def new(cls, language: str, length: int = 5):
"Create new solver, load wordset in given language"
wordset = Wordset.load_dict(language, length)
charset = Wordset.get_charset(language)
solver = cls(wordset=wordset, charset=charset)
solver.start()
return solver
def update(self, the_guess, the_verdict):
"Update solver's state, filter out words that don't match"
self.guesses.append(the_guess)
self.verdicts.append(the_verdict)
possible = {w for w in self._possible.words if match_verdict(the_verdict, w, the_guess)}
self._possible = Wordset(words=possible)
def hints(self, list_length: int = 10) -> list:
"Get hints from solver"
return self._possible.get_random_list(list_length)
def guess(self) -> str:
"Get one guess from solver"
return self._possible.get_random()
@property
def count(self) -> int:
"Get number of words matching so far"
return len(self._possible.words)
def play_wordle(language: str, word: str = None, length: int = 5) -> None:
"Play a game in CLI"
game = Game.new(language, word=word, length=length)
while True:
your_guess = input("Your guess: ").lower()
result = game.guess(your_guess)
the_verdict = result['verdict']
if result['guess'] != your_guess:
print("Wrong word, ignoring")
continue
print(Game.color_verdict(the_verdict))
if result['won']:
break
def solve_wordle(language: str, length: int = 5) -> None:
"Solve wordle game in CLI"
hint_len = 8
solver = Solver.new(language, length=length)
print("Type your last guess along with response encoded as follows:")
examples = ['+__+_', '_?___', '+?_??']
for ex in examples:
print("- " + Game.color_verdict(ex) + ' => ' + ex)
print('etc...')
print()
while True:
while True:
hints = ', '.join(solver.hints(hint_len))
more = solver.count - hint_len
print(f'Possible words: {hints}' + (f' ({more} more)' if more > 0 else ''))
state = input('Your last guess and result: ')
try:
guess, the_verdict = state.split(" ")
assert len(guess) == length
assert len(the_verdict) == length
except (AssertionError, ValueError):
print("Wrong value. Try again.")
continue
break
solver.update(guess, the_verdict)
if solver.count == 1:
word = solver.guess()
print(f"Final guess: {word}")
break
if solver.count == 0:
print('Empty word list! No idea!')
break
def demo_mode(language: str, word: str = None, length: int = 5, words_to_try: list = None) -> None:
"""Computer plays against itself"""
if words_to_try is None:
words_to_try = []
game = Game.new(language=language, word=word, length=length)
solver = Solver.new(language=language, length=length)
while True:
print(f"Solver: {solver.count} possible words.")
if len(words_to_try) > 0:
my_guess = words_to_try.pop(0)
else:
my_guess = solver.guess()
print(f'Solver guessing: {my_guess}')
result = game.guess(my_guess)
color_verd = Game.color_verdict(result['verdict'])
print(f'Game response: {color_verd}')
assert result['guess'] == my_guess
if result['won']:
print(f'Winner! Word: {my_guess}')
break
solver.update(my_guess, result['verdict'])
def main(): # pragma: no cover
"The main()"
parser = argparse.ArgumentParser(description='Play or solve wordle')
parser.add_argument('-l', '--language',
nargs=1, default=['american-english'], help='language to use')
parser.add_argument('-s', '--solve', action='store_true', help='solving mode')
parser.add_argument('-d', '--demo', action='store_true', help='let solver play')
parser.add_argument('-w', '--word', nargs=1, help='secret word to guess (testing and demo)')
parser.add_argument('-n', '--length',
nargs=1, default=[5], type=int, help='word length')
parser.add_argument('--pl', action='store_true', help='Set language to Polish')
parser.add_argument('-t', '--try-words',
nargs=1, default=[''],
help='Words to try first in demo mode (comma separated)')
args = parser.parse_args()
if args.pl:
args.language = ['polish']
word = args.word[0] if args.word else None
language = args.language[0] if args.language else None
length = args.length[0]
words_to_try = args.try_words[0].split(',') if args.try_words[0] else []
if args.word:
length = len(word)
try:
if args.solve:
solve_wordle(language=language, length=length)
elif args.demo:
demo_mode(language=language, word=word, length=length, words_to_try=words_to_try)
else:
play_wordle(language=language, word=word, length=length)
except (KeyboardInterrupt, EOFError):
print("Bye")
if __name__ == '__main__': # pragma: no cover
main()