-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
447 lines (386 loc) · 16.3 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# Wordle cheater program by Aiden C
# Built for Comp Sci 12/1/2022
# --------------------- ATTENTION: For Debugging In Console Set To True:
debugistrue = False
# Importing
import sorters
from sorters import *
import tkinter
from tkinter import *
import time
import math
import os
#Rescheduling Windows Timer Issues
if os.name == "nt":
from ctypes import windll
timeBeginPeriod = windll.winmm.timeBeginPeriod
timeBeginPeriod(1)
# Defining Window
master = Tk()
master.configure(bg="#121213")
master.geometry("400x630")
master.title("Wordle Solver")
# Prereqs for tkinter
pixeler = tkinter.PhotoImage(width=1, height=1)
clonehandlerlist = []
# Define non-changing tkinter widgets
Label(master, text="Wordle Bot", font="Verdana 35 bold", fg="white", bg="#121213").place(relx=0.5, y=5, anchor=N)
watermark = Label(master, text="Wordle Solver By Aiden C For Comp Sci 2022", font="Verdana 7", fg="white", bg="#121213")
Frame(master, bg="#3a3a3c").place(relx=0.5, y=75, relwidth=1, height=2, anchor=N)
finishbutton = Button(master, width=25, height=25, font="Verdana 18 bold", bg="#121213", activebackground="#121213", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: finish())
instructions = Label(master, text="Use the keyboard to type in your first word \n Click the gray squares until they match wordle! \n You get to pick your own starter word", fg="white", font="Verdana 10 bold", bg="#121213")
restartbutton = Button(master, width=25, height=25, font="Verdana 18 bold", bg="#121213", activebackground="#121213", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: reload())
# Read text file
with open('fivewords.txt') as words:
allwords = words.readlines()
allwords = [x.strip().lower() for x in allwords]
# Pre-Defining Computer Analysis Variables
badletters = []
letters = ["", "", "", "", ""]
yellowletters = ["", "", "", "", ""]
debugyellowletters = []
if not debugistrue:
print("\nOutputs:")
# Pre-Defining user input variables
userinput = []
charmap = ["0", "0", "0", "0", "0"]
# Pre-Defining Global Variables
entry = 0
nextplace = True
tutorialsteps = "bbg"
runningani = False
def reload():
global entry
global nextplace
global tutorialsteps
global runningani
global userinput
global charmap
global yellowletters
global letters
global badletters
global allwords
global clonehandlerlist
global boxes
with open('fivewords.txt') as words:
allwords = words.readlines()
allwords = [x.strip().lower() for x in allwords]
rrgb = 58
grgb = 58
brgb = 60
frames = 20
for p in range(0, frames):
for i in range(0, len(clonehandlerlist)):
clonehandlerlist[i].config(text="")
clonehandlerlist[i].config(bg="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))), activebackground="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))))
master.update()
time.sleep(0.01)
rrgb = rrgb - int(((58 - 18) / frames))
grgb = grgb - int(((58 - 18) / frames))
brgb = brgb - int(((60 - 19) / frames))
for i in range(0, len(clonehandlerlist)):
clonehandlerlist[i].destroy()
clonehandlerlist = []
badletters = []
letters = ["", "", "", "", ""]
yellowletters = ["", "", "", "", ""]
userinput = []
charmap = ["0", "0", "0", "0", "0"]
nextplace = True
tutorialsteps = "bbg"
runningani = False
frames2 = 10
for j in range(0, frames2):
for f in range(0, 5):
boxes[f].place(y=(100 + (75 * entry) - ((75 * entry) / frames2) * j))
master.update()
time.sleep(0.01)
for f in range(0, 5):
boxes[f].place(y=100)
boxes[f].config(text="", bg="#3a3a3c", activebackground="#3a3a3c")
entry = 0
rrgb = 18
grgb = 18
brgb = 19
frames = 50
for p in range(0, frames):
instructions.config(fg="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))))
master.update()
time.sleep(0.01)
rrgb = rrgb + int(((255 - 18) / frames))
grgb = grgb + int(((255 - 18) / frames))
brgb = brgb + int(((255 - 19) / frames))
instructions.config(fg="#FFFFFF")
nextbutton.config(text="Next")
# Function to play tutorial hide animation
def hidetutorial():
global tutorialsteps
global entry
global runningani
if (not "b" in tutorialsteps) and (entry == 0):
tutorialsteps = "ggb"
rrgb = 255
grgb = 255
brgb = 255
frames = 25
for p in range(0, frames):
if instructions["fg"] != "#121213":
instructions.config(fg="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))))
master.update()
time.sleep(0.01)
rrgb = rrgb - int(((255 - 18) / frames))
grgb = grgb - int(((255 - 18) / frames))
brgb = brgb - int(((255 - 19) / frames))
instructions.config(fg="#121213")
# Optional debug function for debugging - prints all important variables
def debug():
global letters
global letters
global yellowletters
global userinput
global charmap
global allwords
global debugistrue
if debugistrue:
print("\n-------------------------------------------------------------------- Screen Currently Displaying: " + str(userinput) + " --------------------------------------------------------------------------")
print("Mapped Colors: " + str(charmap))
print("\nGreen Letters: " + str(letters))
print("Yellow Letters: " + str(debugyellowletters))
print("Grey Letters: " + str(badletters))
print("\nEntry #: " + str(entry))
if len(allwords) < 50:
print("\nAll Words: " + str(allwords))
# Function to read the user's input
def readcharmap():
global badletters
global yellowletters
global letters
global userinput
global charmap
global debugyellowletters
viewdnum = 0
for char in charmap:
if char == "0":
if userinput[viewdnum] not in letters:
badletters.append(userinput[viewdnum])
if char == "1":
yellowletters[viewdnum] = (yellowletters[viewdnum] + userinput[viewdnum])
if char == "2":
letters[viewdnum] = userinput[viewdnum]
if letters[viewdnum] in badletters:
badletters.remove(letters[viewdnum])
viewdnum += 1
debugyellowletters = yellowletters
# Function to change color of UI boxes in gui
def changecolor(box):
global clonehandlerlist
global charmap
global boxes
global tutorialsteps
# Since this is part of the tutorial, I need to update the tutorial steps completed variable
tutorialsteps = tutorialsteps[0] + "g" + tutorialsteps[2]
if charmap[box] == "0":
boxes[box].config(bg="#b59f3b", activebackground="#b59f3b")
charmap[box] = "1"
elif charmap[box] == "1":
boxes[box].config(bg="#538d4e", activebackground="#538d4e")
charmap[box] = "2"
elif charmap[box] == "2":
boxes[box].config(bg="#3a3a3c", activebackground="#3a3a3c")
charmap[box] = "0"
hidetutorial()
# Function I got off of the internet to clone widgets (I barely know how it works)
def clone(widget):
parent = widget.nametowidget(widget.winfo_parent())
cls = widget.__class__
clone = cls(parent)
for key in widget.configure():
clone.configure({key: widget.cget(key)})
return clone
# Function to animate the pressing of the next button
def newwordani(prev, char):
global userinput
global entry
global boxes
global clonehandlerlist
# Handle animations for creating box clones
tempxconfig = []
for f in range(0, 5):
tempxconfig.append(clone(boxes[f]))
for x in range(0, 5):
clonehandlerlist.append(tempxconfig[x])
for u in range(0, 5):
tempxconfig[u].config(command=lambda: None, text=str(prev[u]))
tempxconfig[u].place(x=(master.winfo_width() / 2 - ((u - 2) * -75)), y=100 + ((entry - 1) * 75), anchor=N)
if char[u] == "0":
tempxconfig[u].config(bg="#3a3a3c", activebackground="#3a3a3c")
elif char[u] == "1":
tempxconfig[u].config(bg="#b59f3b", activebackground="#b59f3b")
elif char[u] == "2":
tempxconfig[u].config(bg="#538d4e", activebackground="#538d4e")
# New boxes animation
for k in range(0, 25):
for h in range(0, 5):
boxes[h].place(y=((100 + ((entry - 1) * 75)) + (k*3)))
master.update()
time.sleep(0.001)
for i in range(0, 5):
boxes[i].config(text=userinput[i])
for j in range(1, 30):
boxes[i].config(font=("Verdana %s bold" % j))
master.update()
time.sleep(0.001)
# Animation to show the checkmark button
if (entry == 1):
rrgb = 18
grgb = 18
brgb = 19
frames = 20
for p in range(0, frames):
finishbutton.config(bg="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))), activebackground="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))))
restartbutton.config(bg="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))), activebackground="#%s" % (sorters.rgb_to_hex((rrgb, grgb, brgb))))
master.update()
time.sleep(0.01)
rrgb = rrgb + int(((83 - 18) / frames))
grgb = grgb + int(((141 - 18) / frames))
brgb = brgb + int(((78 - 19) / frames))
finishbutton.config(text="✓")
restartbutton.config(text="↺")
# Defining changing GUI widgets
box1 = Button(master, width=60, height=60, font="Verdana 30 bold", bg="#3a3a3c", activebackground="#3a3a3c", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: changecolor(0))
box2 = Button(master, width=60, height=60, font="Verdana 30 bold", bg="#3a3a3c", activebackground="#3a3a3c", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: changecolor(1))
box3 = Button(master, width=60, height=60, font="Verdana 30 bold", bg="#3a3a3c", activebackground="#3a3a3c", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: changecolor(2))
box4 = Button(master, width=60, height=60, font="Verdana 30 bold", bg="#3a3a3c", activebackground="#3a3a3c", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: changecolor(3))
box5 = Button(master, width=60, height=60, font="Verdana 30 bold", bg="#3a3a3c", activebackground="#3a3a3c", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: changecolor(4))
nextbutton = Button(master, text="Next", width=200, height=50, font="Verdana 30 bold", bg="#538d4e", activebackground="#538d4e", activeforeground="lightgrey", highlightthickness=0, bd=0, fg="white", compound="center", padx=0, pady=0, image=pixeler, command=lambda: nextfunc())
# Function to dynamically move widgets upon GUI window size change
def resize():
global nextplace
for i in range(0, 5):
boxes[i].place(x=(master.winfo_width() / 2 - ((i - 2) * -75)), anchor=N)
for f in range(0, len(clonehandlerlist)):
clonehandlerlist[f].place(x=(master.winfo_width() / 2 - (((f - ((math.ceil((f + 1) / 5) - 1) * 5)) - 2) * -75)), anchor=N)
if nextplace:
nextbutton.place(y=(master.winfo_height() - 80), x=(master.winfo_width() / 2), anchor=N)
watermark.place(relx=0.5, y=(master.winfo_height() - 5), anchor=S)
if entry >= 1:
finishbutton.place(x=(master.winfo_width() / 2) - 175, y=(master.winfo_height() - 40), anchor=N)
restartbutton.place(x=(master.winfo_width() / 2) + 175, y=(master.winfo_height() - 40), anchor=N)
instructions.place(x=(master.winfo_width() / 2), y=180, anchor=N)
# Function to run when the check-mark button is pressed - Define all characters as green and update colors
def finish():
global charmap
charmap = ["2", "2", "2", "2", "2"]
nextbutton.config(text="Winner!")
for i in range(0, 5):
boxes[i].config(bg="#538d4e", activebackground="#538d4e")
# Function to run when the next button is pressed
def nextfunc():
global entry
global nextplace
global allwords
global badletters
global yellowletters
global letters
global userinput
global charmap
global tutorialsteps
global runningani
global debugyellowletters
if debugistrue:
print ("\n\n\n")
debug()
# Don't run the function twice at once because as wyatt proved it causes errors
if not runningani:
runningani = True
# Return win if all blocks are green
if (not "0" in charmap) and (not "1" in charmap):
nextbutton.config(text="Winner!")
# Go to next if they aren't
elif (entry < 5) and (len(userinput) == 5):
# If the tutorial isn't done yet, mark it as finshed and update GUI
if tutorialsteps != "ggb":
tutorialsteps = "ggg"
hidetutorial()
else:
instructions.config(fg="#121213")
# Reset GUI and apply new word
readcharmap()
allwords = sortgrayyellowgreen(allwords, badletters, yellowletters, letters, debugistrue)
prevuserimput = userinput
prevcharmap = charmap
yellowletters = ["", "", "", "", ""]
userinput = []
charmap = ["0", "0", "0", "0", "0"]
entry += 1
temptop = topword(allwords, debugistrue, entry)
print("- " + str(temptop))
for j in range(0, 5):
userinput.append(temptop[j])
for i in range(0, 5):
boxes[i].config(bg="#3a3a3c", activebackground="#3a3a3c")
boxes[i].config(text="")
newwordani(prevuserimput, prevcharmap)
# If the input isn't complete, play button vibrating animation
elif nextplace:
nextplace = False
pos = 0
for r in range(0, 10):
pos += 2
nextbutton.place(x=((master.winfo_width() / 2) + pos))
master.update()
time.sleep(0.001)
for t in range(0, 2):
for k in range(0, 20):
pos -= 2
nextbutton.place(x=((master.winfo_width() / 2) + pos))
master.update()
time.sleep(0.001)
for k in range(0, 20):
pos += 2
nextbutton.place(x=((master.winfo_width() / 2) + pos))
master.update()
time.sleep(0.001)
for r in range(0, 10):
pos -= 2
nextbutton.place(x=((master.winfo_width() / 2) + pos))
master.update()
time.sleep(0.001)
nextbutton.place(x=(master.winfo_width() / 2))
nextplace = True
runningani = False
# Read and apply keypresses
def onKeyPress(event):
global userinput
global boxes
global tutorialsteps
# Another part of the tutorial, needs to update tutorial string
tutorialsteps = "g" + tutorialsteps[1] + tutorialsteps[2]
# .isalpha() determines if the key pressed is a letter or not
#if entry == 0:
if (event.char).isalpha() and (len(userinput) <= 4):
userinput.append(event.char)
elif (event.char) == "\x08":
userinput = userinput[:-1]
elif (event.char) == "\r":
nextfunc()
elif (event.char) == "1" or (event.char) == "2" or (event.char) == "3" or (event.char) == "4" or (event.char) == "5":
changecolor(int(event.char)-1)
for i in range(0, 5):
if len(userinput) > i:
boxes[i].config(text=str(userinput[i]))
else:
boxes[i].config(text="")
#elif (event.char) == "\r":
#nextfunc()
hidetutorial()
# Array that defines box variable names for box cloning
boxes = [box1, box2, box3, box4, box5]
# Setting the initial y position of the first boxes
for i in range(0, 5):
boxes[i].place(y=100)
# Binding tkinter listeners to functions
master.bind("<Configure>", lambda event: resize())
master.bind('<KeyPress>', onKeyPress)
# Start the gui!
mainloop()