-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
438 lines (354 loc) · 17.5 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
import tkinter as tk
from tkinter import ttk
from collections import Counter
import codecs
import os
class HangmanSolverApp:
def __init__(self, root):
"""
Initialize the HangmanSolverApp with the given root Tkinter window.
Set up the UI components and layout.
Args:
root (tk.Tk): The root Tkinter window.
"""
self.root = root
self.root.title("Hangman Solver App")
# Disable window resizing
self.root.resizable(False, False)
# Color configuration
self.bg_color = "#24283b" # Background color
self.fg_color = "#7dcfff" # Foreground color
self.widget_color = "#1a1b26" # Widget color
# Set up UI components with colors
self.root.configure(bg=self.bg_color)
self.style = ttk.Style()
self.style.configure('TLabel', background=self.bg_color, foreground=self.fg_color)
self.style.configure('TFrame', background=self.bg_color)
self.style.configure('TButton', background=self.widget_color, foreground=self.fg_color)
self.style.map('TButton',
background=[('pressed', '#24283b'), ('active', '#1a1b26')],
foreground=[('pressed', 'white'), ('active', self.fg_color)])
self.word_list_dir = 'wordLists' # Directory containing word list files
self.words = [] # List to hold words from the selected word list
# Initialize word_entries as an empty list for entry widgets
self.word_entries = []
# Validation function
self.validate_cmd = root.register(self.validate_number)
# Number of fields entry
self.num_label = ttk.Label(root, text="Enter number of fields:")
self.num_label.grid(row=0, column=0, padx=5, pady=5)
self.num_entry = tk.Entry(root, bg=self.widget_color, fg=self.fg_color, insertbackground='white',
borderwidth=0, validate="key", validatecommand=(self.validate_cmd, '%P'))
self.num_entry.grid(row=0, column=1, padx=5, pady=5)
# Word list dropdown menu
self.word_list_label = ttk.Label(root, text="Select word list:")
self.word_list_label.grid(row=0, column=2, padx=5, pady=5)
self.word_list_var = tk.StringVar()
self.word_list_menu = ttk.Combobox(root, textvariable=self.word_list_var)
self.word_list_menu['values'] = self.get_word_lists()
self.word_list_menu.grid(row=0, column=3, padx=5, pady=5)
self.word_list_menu.bind("<<ComboboxSelected>>", self.load_selected_word_list)
# Generate Fields button
self.generate_button = tk.Button(root, text="Generate Fields", command=self.generate_fields,
bg=self.widget_color, fg=self.fg_color, borderwidth=0,
activebackground="#24283b", relief="flat")
self.generate_button.grid(row=0, column=4, padx=5, pady=5)
# Frame for entry fields
self.fields_frame = ttk.Frame(root)
self.fields_frame.grid(row=1, column=0, columnspan=5, padx=5, pady=5)
# Most common letters section
self.common_label = ttk.Label(root, text="Most common letters:")
self.common_label.grid(row=2, column=5, padx=5, pady=(0, 5), sticky='w')
self.common_frame = ttk.Frame(root)
self.common_frame.grid(row=3, column=5, padx=5, pady=5, sticky='n')
# Frame for displaying words (replaced canvas with frame to avoid scrolling)
self.words_frame = ttk.Frame(root)
self.words_frame.grid(row=3, column=0, columnspan=4, padx=5, pady=5, sticky='nsew')
# Pagination controls
self.pagination_frame = ttk.Frame(root)
self.pagination_frame.grid(row=4, column=0, columnspan=5, padx=5, pady=5)
self.prev_button = tk.Button(self.pagination_frame, text="Previous", command=self.prev_page, state=tk.DISABLED,
bg=self.widget_color, fg=self.fg_color, borderwidth=0,
activebackground="#24283b", relief="flat")
self.prev_button.grid(row=0, column=0, padx=5)
self.next_button = tk.Button(self.pagination_frame, text="Next", command=self.next_page,
bg=self.widget_color, fg=self.fg_color, borderwidth=0,
activebackground="#24283b", relief="flat")
self.next_button.grid(row=0, column=1, padx=5)
# Top bar layout
self.top_frame = ttk.Frame(root)
self.top_frame.grid(row=0, column=0, columnspan=7, padx=5, pady=5, sticky='ew')
# Number of fields entry
self.num_label = ttk.Label(self.top_frame, text="Enter number of fields:")
self.num_label.grid(row=0, column=0, padx=5, pady=5)
self.num_entry = tk.Entry(self.top_frame, bg=self.widget_color, fg=self.fg_color, insertbackground='white',
borderwidth=0, validate="key", validatecommand=(self.validate_cmd, '%P'))
self.num_entry.grid(row=0, column=1, padx=5, pady=5)
# Word list dropdown menu
self.word_list_label = ttk.Label(self.top_frame, text="Select word list:")
self.word_list_label.grid(row=0, column=2, padx=5, pady=5)
self.word_list_var = tk.StringVar()
self.word_list_menu = ttk.Combobox(self.top_frame, textvariable=self.word_list_var)
self.word_list_menu['values'] = self.get_word_lists()
self.word_list_menu.grid(row=0, column=3, padx=5, pady=5)
self.word_list_menu.bind("<<ComboboxSelected>>", self.load_selected_word_list)
self.generate_button = tk.Button(self.top_frame, text="Generate Fields", command=self.generate_fields,
bg=self.widget_color, fg=self.fg_color, borderwidth=0,
activebackground="#24283b", relief="flat")
self.generate_button.grid(row=0, column=4, padx=5, pady=5)
# Exclude letters section
self.exclude_label = ttk.Label(self.top_frame, text="Exclude letters:")
self.exclude_label.grid(row=0, column=5, padx=5, pady=5, sticky='w')
self.exclude_entry = tk.Entry(self.top_frame, bg=self.widget_color, fg=self.fg_color, insertbackground='white', borderwidth=0)
self.exclude_entry.grid(row=0, column=6, padx=5, pady=5, sticky='w')
self.exclude_entry.bind("<KeyRelease>", self.update_words)
# Configure row and column weights
root.grid_rowconfigure(1, weight=1)
root.grid_rowconfigure(2, weight=0)
root.grid_rowconfigure(3, weight=0)
root.grid_rowconfigure(4, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=0)
root.grid_columnconfigure(2, weight=0)
root.grid_columnconfigure(3, weight=0)
root.grid_columnconfigure(4, weight=0)
root.grid_columnconfigure(5, weight=0)
root.grid_columnconfigure(6, weight=0)
# Pagination settings
self.words_per_page = 10
self.current_page = 0
# Load default word list if available
self.load_default_word_list()
def validate_number(self, new_value):
"""
Validate if the new value is a number.
Args:
new_value (str): The new value to validate.
Returns:
bool: True if the new value is a number, otherwise False.
"""
if new_value == "" or new_value.isdigit():
return True
return False
def get_word_lists(self):
"""
Retrieve a list of word list files from the word list directory.
Returns:
list: A list of word list filenames with a '.txt' extension.
"""
return sorted([f for f in os.listdir(self.word_list_dir) if f.endswith('.txt')])
def load_selected_word_list(self, event):
"""
Load words from the selected word list file and display them.
Args:
event (tk.Event): The event triggered by selecting a word list from the dropdown menu.
"""
selected_file = self.word_list_var.get()
if not selected_file:
# No selection, so default to the first file alphabetically
word_lists = self.get_word_lists()
if word_lists:
selected_file = word_lists[0]
self.word_list_var.set(selected_file)
file_path = os.path.join(self.word_list_dir, selected_file)
self.words = self.load_words(file_path)
self.display_words()
def load_default_word_list(self):
"""
Load the default word list if available when the application starts.
"""
word_lists = self.get_word_lists()
if word_lists:
default_file = word_lists[0]
self.word_list_var.set(default_file)
self.load_selected_word_list(None)
def load_words(self, filename):
"""
Load words from a given file into a list.
Args:
filename (str): The path to the file containing the words.
Returns:
list: A list of words read from the file.
"""
with codecs.open(filename, 'r', encoding='utf-8') as file:
words = file.read().splitlines()
return words
def generate_fields(self):
"""
Create entry fields for each letter in the word pattern based on user input.
Clears any previous fields and updates the display with the new fields.
"""
for widget in self.fields_frame.winfo_children():
widget.destroy()
try:
num_fields = int(self.num_entry.get())
except ValueError:
return
self.word_entries = []
for i in range(num_fields):
word_entry = tk.Entry(self.fields_frame, width=5, bg=self.widget_color, fg=self.fg_color, insertbackground='white', borderwidth=0)
word_entry.grid(row=0, column=i, padx=2, pady=2)
word_entry.bind("<KeyRelease>", self.update_words)
self.word_entries.append(word_entry)
self.display_words()
def update_common_letters(self, words):
"""
Update the display of the most common letters from the filtered list of words,
excluding letters that have already been entered in the input fields.
Args:
words (list): A list of filtered words used to determine the most common letters.
"""
letter_counter = Counter()
for word in words:
letter_counter.update(word) # Count all letters, not just unique ones
# Exclude letters from input fields
entered_letters = set(self.get_entered_letters())
excluded_letters = self.get_excluded_letters()
all_excluded_letters = entered_letters.union(excluded_letters)
for letter in all_excluded_letters:
if letter in letter_counter:
del letter_counter[letter]
common_letters = letter_counter.most_common(10)
# Clear previous common letters display
for widget in self.common_frame.winfo_children():
widget.destroy()
# Display most common letters
for i, (letter, count) in enumerate(common_letters):
common_label = tk.Label(self.common_frame, text=f"{letter}: {count}", bg=self.bg_color, fg=self.fg_color)
common_label.grid(row=i, column=0, padx=5, pady=2)
def get_entered_letters(self):
"""
Retrieve the letters that have been entered in the input fields.
Returns:
set: A set of letters that have been entered in the input fields.
"""
entered_letters = set()
for entry in self.word_entries:
letter = entry.get().strip()
if letter:
entered_letters.add(letter)
return entered_letters
def get_pattern(self):
"""
Construct the current pattern based on the entries in the fields_frame.
Returns:
str: The pattern string with letters and '.' for empty fields.
"""
pattern = []
for entry in self.word_entries:
char = entry.get()
pattern.append(char if char else '.')
return ''.join(pattern)
def get_excluded_letters(self):
"""
Retrieve the letters that should be excluded from the word list.
Returns:
set: A set of excluded letters based on user input.
"""
return set(self.exclude_entry.get())
def filter_words(self, pattern):
"""
Filter the list of words to match the given pattern and exclude specified letters.
Args:
pattern (str): The pattern to match against the words.
Returns:
list: A list of words that match the pattern and do not contain excluded letters.
"""
excluded_letters = self.get_excluded_letters()
filtered_words = []
for word in self.words:
if len(word) == len(pattern):
match = True
for i, (p, w) in enumerate(zip(pattern, word)):
if p != '.' and p != w:
match = False
break
if match:
# Check for unique occurrences of each specified letter in the pattern
counts = {char: pattern.count(char) for char in pattern if char != '.'}
for char, count in counts.items():
if word.count(char) != count:
match = False
break
if match and not any(letter in word for letter in excluded_letters):
filtered_words.append(word)
return filtered_words
def display_words(self):
"""
Display the words in the words_frame with pagination.
"""
pattern = self.get_pattern()
filtered_words = self.filter_words(pattern)
# Clear previous word display
for widget in self.words_frame.winfo_children():
widget.destroy()
# Display words for the current page
self.show_words(filtered_words)
# Update most common letters display
self.update_common_letters(filtered_words)
def update_words(self, event=None):
"""
Update the displayed list of words based on the current pattern and excluded letters.
This method also updates the display of the most common letters in the filtered words.
Args:
event (tk.Event, optional): An optional event argument for key release events.
"""
pattern = self.get_pattern()
filtered_words = self.filter_words(pattern)
# Clear previous word display
for widget in self.words_frame.winfo_children():
widget.destroy()
# Display words for the current page
self.show_words(filtered_words)
# Update most common letters display
self.update_common_letters(filtered_words)
def show_words(self, words):
"""
Show the filtered words with pagination.
Args:
words (list): A list of filtered words to display.
"""
# Calculate pagination
start_index = self.current_page * self.words_per_page
end_index = start_index + self.words_per_page
page_words = words[start_index:end_index]
# Display words for the current page
y_position = 0 # Initial vertical position
for word in page_words:
word_label = tk.Label(self.words_frame, text=word, bg=self.bg_color, fg=self.fg_color)
word_label.grid(row=y_position, column=0, padx=5, pady=5, sticky='w')
y_position += 1 # Adjust vertical spacing between words
# Add empty labels if fewer than 10 words are displayed
for _ in range(len(page_words), self.words_per_page):
empty_label = tk.Label(self.words_frame, text=" ", bg=self.bg_color)
empty_label.grid(row=y_position, column=0, padx=5, pady=5, sticky='w')
y_position += 1
# Update pagination buttons
self.update_pagination_buttons(words)
def update_pagination_buttons(self, words):
"""
Update the state of the pagination buttons based on the current page.
Args:
words (list): The full list of words to determine the pagination state.
"""
total_pages = (len(words) + self.words_per_page - 1) // self.words_per_page
self.prev_button.config(state=tk.NORMAL if self.current_page > 0 else tk.DISABLED)
self.next_button.config(state=tk.NORMAL if self.current_page < total_pages - 1 else tk.DISABLED)
def prev_page(self):
"""
Go to the previous page of words.
"""
if self.current_page > 0:
self.current_page -= 1
self.display_words()
def next_page(self):
"""
Go to the next page of words.
"""
if self.current_page < (len(self.words) + self.words_per_page - 1) // self.words_per_page - 1:
self.current_page += 1
self.display_words()
if __name__ == "__main__":
root = tk.Tk()
app = HangmanSolverApp(root)
root.mainloop()