-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.py
More file actions
233 lines (194 loc) · 8.88 KB
/
run.py
File metadata and controls
233 lines (194 loc) · 8.88 KB
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
# Wordle Game and Solver
# Name: Jon Paris
# Collaborators: Joey Paris, Nathan Paris
import copy
import random
import time
from solver import Solver
from wordle_common import Knowledge, COLORS, Tools, CONST
import string
GUESSES_FILE = "words-guesses.txt"
def choose_word(wordlist):
return random.choice(wordlist)
def letter_count(answers):
multi3 = []
multi4 = []
for a in answers:
for i in string.ascii_lowercase:
if a.count(i) == 3: multi3.append(a)
if a.count(i) >= 4: multi4.append(a)
print("words with 3 of the same:", str(multi3))
print("words with 4 or more of same:", str(multi4))
def play_wordle(secret_word, wordlist):
total_guesses = 6
remaining_guesses = 6
def decorate_word_with_knowledge(kn: list, word: str) -> str:
decorated_word = ""
local_in_word = []
word = list(word)
for i in range(CONST.WORD_LENGTH):
c = string.ascii_lowercase.index(word[i])
p = 26 + 26 * i
if kn[p] == CONST.YES:
local_in_word.append(c)
color = COLORS.GREEN
elif kn[c] == CONST.YES and (c not in local_in_word or kn[c + 26] == CONST.YES):
local_in_word.append(c)
color = COLORS.YELLOW
else:
color = COLORS.GREY
decorated_word += color + string.ascii_lowercase[c] + COLORS.SPACE_COLOR
return decorated_word + "\n\n"
def show_decorated_keyboard(kn: list):
i = 0
for c in 'qwertyuiopasdfghjklzxcvbnm':
if c == 'a' or c == 'z':
print("\n")
ci = string.ascii_lowercase.index(c)
in_pos = False
for j in range(CONST.WORD_LENGTH):
p = 26 + (26 * j) + ci
if kn[p] == CONST.YES: in_pos = True
i += 1
if in_pos: color = COLORS.GREEN
elif kn[ci] == CONST.YES: color = COLORS.YELLOW
elif kn[ci] == CONST.NO: color = COLORS.GREY
else: color = COLORS.BLACK
print(color + c, end=COLORS.SPACE_COLOR)
print("\n")
# starting welcome
print("Welcome to Wordle!")
print("I am thinking of a word that is " + str(CONST.WORD_LENGTH) + " letters long.")
attempts = ""
k = CONST.EMPTY_KNOWLEDGE
m = ANSWERS
prev_guesses = ()
while remaining_guesses > 0:
print("\nYou have " + str(remaining_guesses) + " guesses left.")
print("Type a " + str(
CONST.WORD_LENGTH) + " letter word, ! for potential word, or !! for potential words with a suggestion")
show_decorated_keyboard(k) # prints keyboard
# request guess
guess = str(input("Please guess a word " + str(len(secret_word)) + " letters long: ")).lower()[
:CONST.WORD_LENGTH]
prev_guesses = prev_guesses + tuple([guess])
# check if they made the correct guess!
if guess == secret_word:
print("Congratulations, you won!")
print("It took you " + str(total_guesses - remaining_guesses + 1) + " guesses")
play_again()
break
# if asking for potential matches show list
if guess == "!":
print("Total: " + str(len(m)) + " " + str(m))
# if asking for potential matches show list and suggest the best match
elif guess == "!!":
print("Total: " + str(len(m)) + " " + str(m))
print("Suggested guess: " + solver.get_suggestion_stable(k, prev_guesses))
elif len(guess) != CONST.WORD_LENGTH:
print("Sorry, " + guess + " is not a " + str(len(secret_word)) + " letter word. Try again.")
elif guess not in wordlist:
print("Sorry, " + guess + " is not a word I know. Try again.")
# review guess
else:
k = Knowledge.update_knowledge(k, secret_word, guess)
m = solver.get_matches(k)
attempts += decorate_word_with_knowledge(k, guess)
print(attempts)
remaining_guesses -= 1
if remaining_guesses == 0:
print("Sorry, you ran out of guesses. The word was " + secret_word + ".")
play_again()
break
def suggestions_only():
total_guesses = 6
remaining_guesses = total_guesses
prev_guesses = ()
k = CONST.EMPTY_KNOWLEDGE
# starting welcome
print("Welcome to Wordle Helper!")
print("I help you guess what Wordle word is if it is " + str(CONST.WORD_LENGTH) + " letters long.")
while remaining_guesses > 0:
print("\nYou have " + str(remaining_guesses) + " guesses left.")
print("You will first your guess " + str(
CONST.WORD_LENGTH) + " letters long to the fist question. Then the response the game gives you.")
# request guess
guess = str(input("Type your guess:")).lower()[:CONST.WORD_LENGTH]
prev_guesses = prev_guesses + tuple([guess])
print("Type the response you got in order.")
print("'G' if in the right position")
print("'Y' yellow if in the word but not in the position, and ")
print("'R' if not in the word. Example: GRRYR")
new_knowledge = str(input("Type the response you got:")).upper()
if len(guess) == CONST.WORD_LENGTH and len(new_knowledge) == CONST.WORD_LENGTH:
if new_knowledge == "GGGGG":
print("Congrats! You won in " + str(total_guesses - remaining_guesses + 1) + " guesses!")
break
k = Knowledge.update_knowledge_from_colors(k, guess, new_knowledge)
matches = solver.get_matches(k)
total_matches = len(matches)
print("Total: " + str(total_matches) + " " + str(matches))
hint = str(input("Want a suggestion? (y/n)")).lower()[0]
if hint == "y":
print("Suggested guess: " + solver.get_suggestion_stable(k, prev_guesses))
print(k)
remaining_guesses -= 1
else:
print("try again. either your guess or feedback was the wrong length")
def play_again():
again = str(input("Play Again? (y/n)")).lower()[0]
if again == "y":
play_wordle(choose_word(ANSWERS), WORDS)
else:
print("bye!")
if __name__ == "__main__":
# Play wordle mode
WORDS = tuple(Tools.load_words(GUESSES_FILE))
ANSWERS = WORDS[:CONST.ANSWERS_LEN]
print("do you want to:")
print("A. Play Wordle here!")
print("B. Get help playing wordle somewhere else.")
print("C. Autoplay and Populate DB!")
print("D. Optimize Solver!")
print("E. Count of multiple use of letters in words")
print("F. Purge Database to best solutions")
print("G. Print solutions for starting word")
print("H. Speed test to solve")
print("I. Profile performance of solver")
hard = True if str(input("Hard Mode (y/n):")).lower() == 'y' else False
menu = str(input("Your Choice:")).lower()
if menu == 'a':
solver = Solver(WORDS, {"optimize": False, "hard": hard, "fast": True})
play_wordle(choose_word(ANSWERS), WORDS)
elif menu == 'b':
solver = Solver(WORDS, {"optimize": False, "hard": hard, "fast": True})
suggestions_only()
elif menu == 'c':
s = Solver(WORDS, {"optimize": False, "hard": hard, "fast": False})
s.auto_play()
elif menu == 'd':
top_level = int(input("top-level estimates to test (recommend '100'): "))
next_levels = int(input("next-level estimates to test (recommend '8'): "))
starting_word = str(input("word starting guess to optimize for (e.g. 'reast'): ")).lower()
s = Solver(WORDS, {"fast": False, "optimize": True, "next_levels": next_levels, "top_level": top_level, "starting_word": starting_word, "hard": hard})
s.auto_play()
elif menu == 'e':
letter_count(ANSWERS)
elif menu == 'f':
s = Solver(WORDS, {"fast": False, "optimize": True, "next_levels": 1, "top_level": 15, "starting_word": "", "hard": hard})
s.purge_unused()
elif menu == 'g':
starting_word = str(input("Starting word to print solutions (e.g. 'salet'): ")).lower()
s = Solver(WORDS, {"fast": False, "optimize": True, "next_levels": 0, "top_level": 0, "starting_word": starting_word, "to_print": True, "hard": hard})
s.auto_play()
elif menu == 'h':
starting_word = str(input("Starting word to print solutions (e.g. 'salet'): ")).lower()
top_level = int(input("top-level estimates to test (recommend '100'): "))
next_levels = int(input("next-level estimates to test (recommend '8'): "))
start_time = time.time()
s = Solver(WORDS, {"fast": False, "optimize": True, "next_levels": next_levels, "top_level": top_level, "starting_word": starting_word, "hard": hard})
s.auto_play()
print(str(time.time() - start_time), "seconds")
elif menu == "i":
s = Solver(WORDS, {"starting_word": "salet", "hard": hard})
s.auto_play_profile()