-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwordle_old.py
More file actions
394 lines (336 loc) · 12.9 KB
/
Copy pathwordle_old.py
File metadata and controls
394 lines (336 loc) · 12.9 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
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
"""
FEATURES:
* play as much as you want unlike original wordle
* toggle full screen mode with <F11>
TODO:
[X] new_game not working, make it work
[X] add a virtual keyboard
[X] make the boxes border white if it contains a letter that has not been checked yet
[X] highlight used letters on the virtual keyboard
[ ] toast to show error messages
[ ] an overlay window to ask if you wanna play another game
[ ] after solving a puzzle a timer will start for 1 hour and you can't solve another puzzle unitl it ends
[ ] add help menu
[ ] add settings menu
[ ] add a way to not pick the same random word again (store all the words already picked by the program)
[ ] add a option to add a custom wordlist
[ ] add a score keeping mechanism
[ ] create a tkinter starting template or templates
[ ] create a tkinter cookbook
tkinter cookbook ideas:
* starting templates
* good practices
* toggle fullscreen mode
* switching between multiple frames in a single toplevel window
* sample scripts to make an executable using pyinstaller
"You're a dumbo"
"Go to Pre-KG and learn English"
"Wow, you're so bad"
"""
from tkinter import messagebox, ttk
from pathlib import Path
import tkinter as tk
import random
import string
import sys
WORD_LEN = 5
MAX_TRIES = 6
COLOR_BORDER_HIGHLIGHT = "#565758"
COLOR_BLANK = "#121213"
COLOR_INCORRECT = "#3a3a3c"
COLOR_HALF_CORRECT = "#b59f3b"
COLOR_CORRECT = "#538d4e"
BOX_SIZE = 55
PADDING = 3
try:
BASE_PATH = Path(sys._MEIPASS)
except AttributeError:
BASE_PATH = Path(".")
VALID_WORDS_WORDLIST = BASE_PATH / "wordlists/wordle-allowed-guesses.txt"
ANSWERS_WORDLIST = BASE_PATH / "wordlists/wordle-answers.txt"
APP_ICON = BASE_PATH / "assets/wordle_logo_32x32.ico"
BACKSPACE_ICON = BASE_PATH / "assets/backspace.png"
HELP_ICON = BASE_PATH / "assets/help.png"
SETTINGS_ICON = BASE_PATH / "assets/settings.png"
MANUAL_IMAGE = BASE_PATH / "assets/manual_image2.png"
ANSWERS = set(word.upper() for word in open(ANSWERS_WORDLIST).read().splitlines())
ALL_WORDS = set(word.upper() for word in open(VALID_WORDS_WORDLIST).read().splitlines()) | ANSWERS
class Manual(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
# sticky="ns" occupies extra space above and below but not on the sides
self.grid(sticky="ns")
# container = tk.Frame(self, bg=COLOR_BLANK)
# container.grid()
# f = tk.Frame(container, bg=COLOR_BLANK)
# tk.Label(f, text="HOW TO PLAY")
self.manual_image = tk.PhotoImage(file=MANUAL_IMAGE)
tk.Label(self, image=self.manual_image).grid(sticky="nswe")
class Wordle(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
# sticky="ns" occupies extra space above and below but not on the sides
self.grid(sticky="ns")
self.master.title("Wordle - A Word Game")
# self.master.state("zoomed")
# self.master.resizable(False, False)
self.master.iconbitmap(APP_ICON)
# self.master.iconphoto(False, tk.PhotoImage(file="wordle_logo_32x32.png"))
self.fullscreen = False
self.master.bind("<F11>", self.fullscreen_toggle)
self.master.bind("<Return>", self.check_word)
self.master.bind("<BackSpace>", self.remove_letter)
self.master.bind("<Key>", self.enter_letter)
self.init_ui()
self.new_game()
def fullscreen_toggle(self, event=None):
if self.fullscreen:
self.master.wm_attributes("-fullscreen", False)
self.fullscreen = False
else:
self.master.wm_attributes("-fullscreen", True)
self.fullscreen = True
def new_game(self):
self.answer = random.choice(list(ANSWERS)).upper()
self.words = [""] * 6
self.correct_letters = set()
self.half_correct_letter = set()
self.incorrect_letters = set()
# reset the labels and keyboard
for i in range(MAX_TRIES):
self.current_word = i
self.update_labels()
self.current_word = 0
self.update_keyboard()
def congratulate(self):
title = ["Genius", "Magnificent", "Impressive", "Splendid", "Great", "Phew"][self.current_word]
message = "Wanna Play Another Game?"
if messagebox.askyesno(title, message):
self.new_game()
else:
self.master.destroy()
def humiliate(self):
title = "Better Luck Next Time!"
message = f"One More Game?\n(BTW the word was {self.answer}.)"
if messagebox.askyesno(title, message):
self.new_game()
else:
self.master.destroy()
def init_ui(self):
self.icons = {
"settings": tk.PhotoImage(file=SETTINGS_ICON),
"help": tk.PhotoImage(file=HELP_ICON),
"backspace": tk.PhotoImage(file=BACKSPACE_ICON),
}
# ==> top bar ==>
container = tk.Frame(self, bg=COLOR_BLANK, height=40)
container.grid(sticky="we")
container.grid_columnconfigure(1, weight=1)
# help button
tk.Button(
container,
image=self.icons["help"],
bg=COLOR_BLANK,
border=0,
cursor="hand2",
).grid(row=0, column=0)
# title
tk.Label(
container,
text="WORDLE",
fg="#d7dadc",
bg=COLOR_BLANK,
font=("Helvetica Neue", 28, "bold"),
).grid(row=0, column=1)
# settings button
tk.Button(
container,
image=self.icons["settings"],
bg=COLOR_BLANK,
border=0,
cursor="hand2",
).grid(row=0, column=2)
# <== top bar <==
# separator
ttk.Separator(self).grid(sticky="ew")
tk.Frame(self, bg=COLOR_BLANK, height=40).grid()
# ==> main game grid ==>
# if there is extra space then give it to main game grid
self.rowconfigure(3, weight=1)
container = tk.Frame(self, bg=COLOR_BLANK)
container.grid()
self.labels = []
for i in range(MAX_TRIES):
row = []
for j in range(WORD_LEN):
cell = tk.Frame(
container,
width=BOX_SIZE,
height=BOX_SIZE,
highlightthickness=1,
highlightbackground=COLOR_INCORRECT,
)
cell.grid_propagate(0)
cell.grid_rowconfigure(0, weight=1)
cell.grid_columnconfigure(0, weight=1)
cell.grid(row=i, column=j, padx=PADDING, pady=PADDING)
t = tk.Label(
cell,
text="",
justify="center",
font=("Helvetica Neue", 24, "bold"),
bg=COLOR_BLANK,
fg="#d7dadc",
highlightthickness=1,
highlightbackground=COLOR_BLANK,
)
t.grid(sticky="nswe")
row.append(t)
self.labels.append(row)
# <== main game grid <==
# bottom empty separator
tk.Frame(self, bg=COLOR_BLANK, height=40).grid()
# ==> virtual keyboard ==>
container = tk.Frame(self, bg=COLOR_BLANK)
container.grid()
# add all the alphabets
self.keyboard_buttons = {}
for i, keys in enumerate(["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"]):
row = tk.Frame(container, bg=COLOR_BLANK)
row.grid(row=i, column=0)
for j, c in enumerate(keys):
if i == 2: # leave one column for the ENTER button in the last row
j += 1
cell = tk.Frame(
row,
width=40,
height=55,
highlightthickness=1,
highlightbackground=COLOR_INCORRECT,
)
cell.grid_propagate(0)
cell.grid_rowconfigure(0, weight=1)
cell.grid_columnconfigure(0, weight=1)
cell.grid(row=0, column=j, padx=PADDING, pady=PADDING)
btn = tk.Button(
cell,
text=c,
justify="center",
font=("Helvetica Neue", 13),
bg=COLOR_BLANK,
fg="#d7dadc",
cursor="hand2",
border=0,
command=lambda c=c: self.enter_letter(key=c),
)
btn.grid(sticky="nswe")
self.keyboard_buttons[c] = btn
# add the enter and delete buttons
for col, text, func in ((0, "ENTER", self.check_word), (8, "", self.remove_letter)):
cell = tk.Frame(
row,
width=75,
height=55,
highlightthickness=1,
highlightbackground=COLOR_INCORRECT,
)
cell.grid_propagate(0)
cell.grid_rowconfigure(0, weight=1)
cell.grid_columnconfigure(0, weight=1)
cell.grid(row=0, column=col, padx=PADDING, pady=PADDING)
btn = tk.Button(
cell,
text=text,
justify="center",
font=("Helvetica Neue", 13),
bg=COLOR_BLANK,
fg="#d7dadc",
cursor="hand2",
border=0,
command=func,
)
btn.grid(row=0, column=0, sticky="nswe")
# set the image for delete button
btn.configure(image=self.icons["backspace"])
# <== virtual keyboard <==
def update_keyboard(self):
for key, btn in self.keyboard_buttons.items():
if key in self.correct_letters:
btn["bg"] = COLOR_CORRECT
elif key in self.half_correct_letter:
btn["bg"] = COLOR_HALF_CORRECT
elif key in self.incorrect_letters:
btn["bg"] = COLOR_INCORRECT
else:
btn["bg"] = COLOR_BLANK
def update_labels(self, colors=None):
word = self.words[self.current_word]
for i, label in enumerate(self.labels[self.current_word]):
try:
letter = word[i]
except IndexError:
letter = ""
label["text"] = letter
if colors:
label["bg"] = colors[i]
label["highlightbackground"] = colors[i]
else:
label["bg"] = COLOR_BLANK
label["highlightbackground"] = COLOR_BORDER_HIGHLIGHT if letter else COLOR_BLANK
def check_word(self, event=None):
print("checking word:", self.words[self.current_word])
word = self.words[self.current_word]
if len(word) < WORD_LEN:
messagebox.showinfo("You're an Idiot.", "Not Enough Letters.")
return
if word not in ALL_WORDS:
messagebox.showinfo("You're an Idiot.", "Word is not in wordlist.")
return
colors = []
freq = {c: self.answer.count(c) for c in self.answer}
for x, y in zip(word, self.answer):
if x == y:
colors.append(COLOR_CORRECT)
self.correct_letters.add(x)
elif freq.get(x, 0) > 0:
colors.append(COLOR_HALF_CORRECT)
self.half_correct_letter.add(x)
freq[x] -= 1
else:
self.incorrect_letters.add(x)
colors.append(COLOR_INCORRECT)
self.update_labels(colors)
self.update_keyboard()
if word == self.answer:
self.congratulate()
self.current_word += 1
if self.current_word >= MAX_TRIES:
self.humiliate()
def remove_letter(self, event=None):
if self.words[self.current_word]:
print(self.words[self.current_word][-1], "was deleted.")
self.words[self.current_word] = self.words[self.current_word][:-1]
self.update_labels()
def enter_letter(self, event=None, key=None):
key = key or event.keysym.upper()
if key in string.ascii_uppercase:
print(key, "was entered.")
self.words[self.current_word] += key
# prevent user from enterering excess letters
self.words[self.current_word] = self.words[self.current_word][:WORD_LEN]
self.update_labels()
if __name__ == "__main__":
# initialize the app
root = tk.Tk()
root.configure(bg=COLOR_BLANK)
app = Wordle(root, bg=COLOR_BLANK)
# center the frame
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
# run the app
app.mainloop()
"""
https://www.reddit.com/r/wordle/comments/s4tcw8/a_note_on_wordles_word_list/
wordle answers: https://gist.github.com/cfreshman/a03ef2cba789d8cf00c08f767e0fad7b
wordle allowed words: https://gist.github.com/cfreshman/cdcdf777450c5b5301e439061d29694c
"""