173 lines
5.3 KiB
Python
173 lines
5.3 KiB
Python
import sys
|
|
import threading
|
|
import urllib.request
|
|
from collections import Counter
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "4.0")
|
|
from gi.repository import GLib, Gtk
|
|
|
|
|
|
# --- Core Countdown Logic ---
|
|
def fetch_dictionary():
|
|
"""Download and filter valid English words."""
|
|
url = "https://raw.githubusercontent.com/raun/Scrabble/master/words.txt"
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=10) as response:
|
|
words = response.read().decode("utf-8").splitlines()
|
|
return [
|
|
w.strip().lower()
|
|
for w in words
|
|
if w.isalpha() and 2 <= len(w) <= 9
|
|
]
|
|
except Exception as e:
|
|
print(f"Error fetching dictionary: {e}")
|
|
return []
|
|
|
|
|
|
def solve_countdown(letters, dictionary):
|
|
"""Find and return matching words sorted by length (descending)."""
|
|
letters_count = Counter(letters.lower())
|
|
valid_words = []
|
|
|
|
for word in dictionary:
|
|
if not (Counter(word) - letters_count):
|
|
valid_words.append(word)
|
|
|
|
valid_words.sort(key=lambda w: (-len(w), w))
|
|
return valid_words
|
|
|
|
|
|
# --- GTK 4 Application Window ---
|
|
class CountdownWindow(Gtk.ApplicationWindow):
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs, title="Countdown Solver")
|
|
self.set_default_size(480, 550)
|
|
|
|
self.dictionary = []
|
|
|
|
# --- Main Layout Box ---
|
|
main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
|
|
main_box.set_margin_top(16)
|
|
main_box.set_margin_bottom(16)
|
|
main_box.set_margin_start(16)
|
|
main_box.set_margin_end(16)
|
|
self.set_child(main_box)
|
|
|
|
# --- Input Section ---
|
|
input_box = Gtk.Box(
|
|
orientation=Gtk.Orientation.HORIZONTAL, spacing=8
|
|
)
|
|
|
|
self.entry = Gtk.Entry()
|
|
self.entry.set_placeholder_text("Enter letters (e.g. mdaisuteo)...")
|
|
self.entry.set_hexpand(True)
|
|
self.entry.connect("activate", self.on_solve_clicked) # Trigger on Enter
|
|
input_box.append(self.entry)
|
|
|
|
self.solve_button = Gtk.Button(label="Solve")
|
|
self.solve_button.connect("clicked", self.on_solve_clicked)
|
|
self.solve_button.set_sensitive(False) # Disabled until dictionary loads
|
|
input_box.append(self.solve_button)
|
|
|
|
main_box.append(input_box)
|
|
|
|
# --- Status / Info Label ---
|
|
self.status_label = Gtk.Label(
|
|
label="Downloading word list in background..."
|
|
)
|
|
self.status_label.set_xalign(0)
|
|
main_box.append(self.status_label)
|
|
|
|
# --- Results List Box in Scrolled Window ---
|
|
scrolled = Gtk.ScrolledWindow()
|
|
scrolled.set_vexpand(True)
|
|
|
|
self.list_box = Gtk.ListBox()
|
|
self.list_box.set_selection_mode(Gtk.SelectionMode.NONE)
|
|
scrolled.set_child(self.list_box)
|
|
|
|
main_box.append(scrolled)
|
|
|
|
# --- Load Dictionary Asynchronously ---
|
|
threading.Thread(target=self._load_dictionary_thread, daemon=True).start()
|
|
|
|
def _load_dictionary_thread(self):
|
|
"""Fetch dictionary off the main thread."""
|
|
words = fetch_dictionary()
|
|
# Schedule GUI updates on the main GTK thread
|
|
GLib.idle_add(self._on_dictionary_loaded, words)
|
|
|
|
def _on_dictionary_loaded(self, words):
|
|
"""Update UI state once dictionary is ready."""
|
|
self.dictionary = words
|
|
if words:
|
|
self.status_label.set_text(
|
|
f"Dictionary loaded ({len(words):,} words). Ready!"
|
|
)
|
|
self.solve_button.set_sensitive(True)
|
|
else:
|
|
self.status_label.set_text("Failed to load dictionary. Check connection.")
|
|
|
|
def on_solve_clicked(self, widget):
|
|
"""Handle solve trigger."""
|
|
letters = self.entry.get_text().strip()
|
|
if not letters or not self.dictionary:
|
|
return
|
|
|
|
# Clear existing result rows
|
|
while child := self.list_box.get_first_child():
|
|
self.list_box.remove(child)
|
|
|
|
matches = solve_countdown(letters, self.dictionary)
|
|
|
|
if not matches:
|
|
self.status_label.set_text("No valid words found.")
|
|
return
|
|
|
|
max_len = len(matches[0])
|
|
self.status_label.set_text(
|
|
f"Found {len(matches)} words (Longest: {max_len} letters)"
|
|
)
|
|
|
|
current_len = 0
|
|
for word in matches:
|
|
word_len = len(word)
|
|
|
|
# Insert section header row when length changes
|
|
if word_len != current_len:
|
|
current_len = word_len
|
|
header_label = Gtk.Label()
|
|
header_label.set_markup(
|
|
f"<b>{current_len}-Letter Words</b>"
|
|
)
|
|
header_label.set_xalign(0)
|
|
header_label.set_margin_top(8)
|
|
header_label.set_margin_bottom(4)
|
|
self.list_box.append(header_label)
|
|
|
|
# Word row
|
|
row_label = Gtk.Label(label=f" • {word}")
|
|
row_label.set_xalign(0)
|
|
row_label.set_margin_start(8)
|
|
self.list_box.append(row_label)
|
|
|
|
|
|
# --- GTK Application Entry Point ---
|
|
class CountdownApp(Gtk.Application):
|
|
|
|
def __init__(self):
|
|
super().__init__(application_id="org.countdown.solver")
|
|
|
|
def do_activate(self):
|
|
win = self.props.active_window
|
|
if not win:
|
|
win = CountdownWindow(application=self)
|
|
win.present()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = CountdownApp()
|
|
app.run(sys.argv) |