-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
239 lines (196 loc) · 8.02 KB
/
main.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
import random
WORD_SIZE = 9
MAX_TURNS = 6
class WordleSolver:
data: list
possible_answers: list
turn: int = 1
correct_with_position: dict = {} # green
present_but_incorrect_position: str = "" # yellow
present_but_incorrect_position_dict: dict = {} # yellow
not_present: str = "" # gray
hard_mode: bool = False
def __init__(self, lang: str) -> None:
self.lang = lang
d = [x.replace("\n", "") for x in open(f"languages/{self.lang}.dat", "r")]
self.all_words_list = [x for x in d if len(x) == WORD_SIZE and "-" not in d]
self.possible_answers = self.all_words_list
def get_next_input(self):
if self.turn == 1:
print(
f"There are {len(self.all_words_list)} words. introduce this randomly selected one:"
)
self.curr_choice = random.choice(self.all_words_list)
else:
potential_next_answers = self.get_possible_final_answer()
self.possible_answers = potential_next_answers
if len(potential_next_answers) == 1:
print(f'Answer found! introduce "{potential_next_answers[0]}" to win')
exit()
print(f"\nThere are {len(potential_next_answers)} potential answers")
if (
len(potential_next_answers) > MAX_TURNS - self.turn
and not self.hard_mode
):
choose_another = self.get_missing_letters()
if len(choose_another) > 0:
potential_next_answers = choose_another
print(
"""
since there are more potential answers than turns remaining,
introduce a random word containing none of the already guessed letters
(or enter "p" to see all possible answers)"""
)
self.curr_choice = random.choice(potential_next_answers)
print("Introduce this randomly selected word from the list")
self.turn += 1
print(f"\t{self.curr_choice}")
return self.curr_choice
def get_possible_final_answer(self):
potential_next_answers = []
for word in self.all_words_list:
is_possible_answer = True
for pos, letter in self.correct_with_position.items():
if not word[pos] == letter:
is_possible_answer = False
for (
pos,
incorrect_pos_letters,
) in self.present_but_incorrect_position_dict.items():
for letter in incorrect_pos_letters:
if word[pos] == letter:
is_possible_answer = False
for letter in self.present_but_incorrect_position:
if not letter in word:
is_possible_answer = False
for letter in self.not_present:
if letter in word:
is_possible_answer = False
if is_possible_answer:
potential_next_answers.append(word)
return potential_next_answers
def get_missing_letters(self):
words_with_unseen_letters = []
for word in self.all_words_list:
contains_seen_letters = False
for letter in [
*self.present_but_incorrect_position,
*self.not_present,
*self.correct_with_position.values(),
]:
if letter in word:
contains_seen_letters = True
if not contains_seen_letters:
words_with_unseen_letters.append(word)
return words_with_unseen_letters
def handle_input(self, pattern: str):
for i in range(WORD_SIZE):
state = pattern[i]
letter = self.curr_choice[i]
if state == "g":
self.correct_with_position[i] = letter
for i in range(WORD_SIZE):
state = pattern[i]
letter = self.curr_choice[i]
if state == "y":
if letter not in self.present_but_incorrect_position:
self.present_but_incorrect_position += letter
if not self.present_but_incorrect_position_dict.get(i):
self.present_but_incorrect_position_dict[i] = ""
if letter not in self.present_but_incorrect_position_dict[i]:
self.present_but_incorrect_position_dict[i] += letter
for i in range(WORD_SIZE):
state = pattern[i]
letter = self.curr_choice[i]
if state == "-":
if letter not in self.not_present:
if (
letter not in self.correct_with_position.values()
and letter not in self.present_but_incorrect_position
):
self.not_present += letter
def reset(self):
self.turn = 1
self.correct_with_position = {}
self.present_but_incorrect_position = ""
self.present_but_incorrect_position_dict = {}
self.not_present = ""
self.__init__(self.lang)
if __name__ == "__main__":
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-W", default=5, required=False, type=int)
parser.add_argument("--words", default=5, required=False, type=int)
parser.add_argument("-H", default=False, required=False, type=bool)
parser.add_argument("--hard-mode", default=False, required=False, type=bool)
args = parser.parse_args()
print(
"""
WORDLE BREAKER
instructions:
1. Select language
2. Choose action
a. Introduce the shown word on wordle
b. Introduce "c", and then introduce your choosen word
c. Introduce "p" to see all possible answers, then go to 2
d. introduce "r" to restart
3. Write on the terminal the results using the following format:
- for gray (no match)
y for yellow (match, incorrect order)
g for green (match, correct order)
For example, 🟨🟩⬜⬜🟩 -> "yg--g"
"""
)
WORD_SIZE = args.W or args.words or 5
accepted_lang = [l.replace(".dat", "") for l in os.listdir("languages")]
lang = input(f"Introduce language ({', '.join(accepted_lang)}): ").lower()
assert lang in accepted_lang, f"Only {', '.join(accepted_lang)} es supported"
game = WordleSolver(lang)
if args.H or args.hard_mode:
game.hard_mode = True
while True:
game.get_next_input()
while True:
try:
pattern = input(
"""
Introduce pattern or:
- introduce p to see all possible answers
- introduce c to choose your own word):
Your input:
"""
)
if pattern == "p":
print(game.possible_answers)
print(f"\nRecommended next guess:\t{game.curr_choice}")
continue
if pattern == "r":
game.reset()
game.get_next_input()
continue
if pattern == "c":
while True:
try:
word = input("Introduce the chosen word: ")
assert type(word) == str, "Word must be a string"
assert (
len(word) == WORD_SIZE
), f"word should be {WORD_SIZE} characters long"
game.curr_choice = word
break
except AssertionError as e:
print(e)
continue
assert type(pattern) == str, "Pattern must be a string"
assert (
len(pattern) == WORD_SIZE
), f"Pattern should be {WORD_SIZE} characters long"
assert set(pattern) <= set(
"-yg"
), "Patten must be composed of -, y and g"
break
except AssertionError as e:
print(e)
continue
game.handle_input(pattern)