From 40b309bffc6375d6c7071992a9b1589b53da9ed6 Mon Sep 17 00:00:00 2001 From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 21 Nov 2022 17:45:36 -0500 Subject: [PATCH] very slow puzzle generator, data sifting, pruned out non letters --- Main/__pycache__/game.cpython-310.pyc | Bin 1861 -> 1861 bytes Main/data.py | 79 ++++++++++++++++++++++++++ Main/game.py | 2 +- Main/main.py | 2 +- Main/prune.py | 2 +- Main/pruned_words.json | 2 - Main/puzzle_generator.py | 66 +++++++++++++++++++++ Main/setup.py | 25 ++++---- 8 files changed, 163 insertions(+), 15 deletions(-) create mode 100644 Main/data.py diff --git a/Main/__pycache__/game.cpython-310.pyc b/Main/__pycache__/game.cpython-310.pyc index fc0e4e0d83b010c1b93d1e6b65be3402f45887c1..2ff0263b2079f3038a55bce19f53589e2c2f5212 100644 GIT binary patch delta 22 dcmX@gca)DepO=@5fq{X6{Y`bU longest_substr: + longest_substr = c + # print(w[prev:val]) + prev = val + c = len(w) - prev + if c > longest_substr: + longest_substr = c + # print(w[prev:len(w)]) + + return longest_substr - 1 + + +longest = 0 +longest_value = "" +for word in word_list: + l = non_vowel_substr(word) + if l >= longest: + longest_value = word + longest = l +print(f'The longest substring that has no vowels is in "{longest_value}" and has length {longest}.') diff --git a/Main/game.py b/Main/game.py index 712ab32..f6bf5e0 100644 --- a/Main/game.py +++ b/Main/game.py @@ -9,7 +9,7 @@ class Game: def __init__(self, imported_file_name=None): if imported_file_name is None: length, width = 4,4 - self.letters = [random.choice(alphabet) for i in range(length * width)] + self.letters = [random.choice(alphabet) for i in range(length * width)] else: with open(imported_file_name,"r") as f: file_contents = f.read().split("\n") diff --git a/Main/main.py b/Main/main.py index 3c0238c..beee3c6 100644 --- a/Main/main.py +++ b/Main/main.py @@ -4,7 +4,7 @@ from setup import * from game import Game -scene = Game("puzzles/templates/puzzle.txt") +scene = Game() is_running = True delta = 1000/fps diff --git a/Main/prune.py b/Main/prune.py index d4ea849..2bce199 100644 --- a/Main/prune.py +++ b/Main/prune.py @@ -9,7 +9,7 @@ print(min(word_list, key=len)) pruned_word_list = dict() for word in word_list: - if len(word) >= 3: + if len(word) >= 3 and "-" not in word: pruned_word_list[word] = 1 diff --git a/Main/pruned_words.json b/Main/pruned_words.json index b1b8540..759d003 100644 --- a/Main/pruned_words.json +++ b/Main/pruned_words.json @@ -160504,7 +160504,6 @@ "jealousness": 1, "jeames": 1, "jean": 1, - "jean-christophe": 1, "jeanette": 1, "jeany": 1, "jeanie": 1, @@ -160512,7 +160511,6 @@ "jeannette": 1, "jeannie": 1, "jeanpaulia": 1, - "jean-pierre": 1, "jeans": 1, "jear": 1, "jebat": 1, diff --git a/Main/puzzle_generator.py b/Main/puzzle_generator.py index e69de29..a78249e 100644 --- a/Main/puzzle_generator.py +++ b/Main/puzzle_generator.py @@ -0,0 +1,66 @@ +from random import choice +from queue import Queue +from string import ascii_lowercase +import json + +length, width = 4, 4 +with open("pruned_words.json", 'r') as f: + word_list = json.load(f) +alphabet = ascii_lowercase +weights = [8.464812190575197, 1.8290213382006486, 4.377463842181293, 3.238497204031964, 10.77374340292079, + 1.1220794892410257, 2.3639146242995768, 2.64283880414943, 8.95783107183879, 0.15584834882323761, + 0.7667338050095298, 5.577710795267362, 3.009862381185312, 7.195528282969081, 7.200021981915867, + 3.2521214123037474, 0.16781246449047607, 7.0440305216612025, 7.162211941733039, 6.607483411238427, + 3.7628002442053465, 0.945823450296398, 0.6405381447778209, 0.29978982655180736, 2.0193881642839093, + 0.4220928558487209] + +class Tile: + + def __init__(self, x, y, letter): + self.letter = letter + self.neighbors = set() + for i in range(-1, 2): + for j in range(-1, 2): + if not (i == 0 and j == 0): + tx, ty = x + i, y + j + if (0 <= tx < width) and (0 <= ty < length): + self.neighbors.add((tx, ty)) + + +def find_words(b): + words = set() + q = Queue() + for i in b: + q.put((b[i].letter, i, set())) + + while not q.empty(): + word, current, seen = q.get() + if current in seen: + continue + else: + seen.add(current) + + if len(word) >= length * width: + break + + if word in word_list: + words.add(word) + + for i in b[current].neighbors: + q.put((word + b[i].letter, i, seen.copy())) + return words + + +board = {(i, j): Tile(i, j, choice(alphabet)) for i in range(width) for j in range(length)} + +for i, val in enumerate(board.values()): + print(val.letter, end=" ") + if (i + 1) % 4 == 0: + print() + +print(find_words(board)) +# c l t a +# h x d o +# r a m u +# t f k g +# {'haku', 'aku', 'trah', 'toma', 'fam', 'gud', 'frt', 'amdt', 'kadu', 'aft', 'rad', 'adam', 'tao', 'arf', 'fax', 'art', 'max', 'rat', 'adat', 'oda', 'dao', 'tod', 'tax', 'taku', 'mat', 'kudo', 'rha', 'omar', 'dar', 'douma', 'haft', 'moat', 'fram', 'dum', 'odum', 'rah', 'doug', 'moa', 'chat', 'xat', 'udo', 'dart', 'fado', 'kaf', 'mota', 'mud', 'dug', 'trad', 'odax', 'tom', 'cham', 'amu', 'xcl', 'udom', 'kam', 'raku', 'tar', 'toad', 'hld', 'tou', 'rada', 'mot', 'arx', 'trf', 'mad', 'mudar', 'mart', 'toa', 'mxd', 'maku', 'duo', 'cha', 'mado', 'toda', 'doum', 'mfr', 'tra', 'haf', 'oat', 'chart', 'dat', 'rax', 'dom', 'doat', 'adar', 'gum', 'dak', 'dato', 'ada', 'chad', 'adm', 'duka', 'marx', 'fart', 'chaft', 'mug', 'hat', 'ham', 'dumka', 'tad', 'ram', 'kat', 'tfr', 'kart', 'tram', 'oud', 'hcl', 'dam', 'ado', 'daft', 'dot', 'mah', 'duma', 'moud', 'raft', 'mar', 'dah', 'atom', 'far', 'mou', 'tam', 'hart', 'tahr', 'frat', 'hak', 'adatom', 'fra', 'char', 'toug', 'had', 'oad', 'fad', 'fat', 'doa', 'mod'} diff --git a/Main/setup.py b/Main/setup.py index 5b5364e..0a2c2f7 100644 --- a/Main/setup.py +++ b/Main/setup.py @@ -8,12 +8,10 @@ import random with open("pruned_words.json", 'r') as f: word_list = json.load(f) - - pygame.init() SCREEN_WIDTH, SCREEN_HEIGHT = 720, 720 dimensions = pygame.Vector2(SCREEN_WIDTH, SCREEN_HEIGHT) -center = pygame.Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT/2) +center = pygame.Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) screen = pygame.display.set_mode(dimensions, pygame.RESIZABLE) pygame.display.set_caption("Word Hunt") @@ -21,13 +19,20 @@ clock = pygame.time.Clock() fps = 60 alphabet = string.ascii_lowercase +weights = [8.464812190575197, 1.8290213382006486, 4.377463842181293, 3.238497204031964, 10.77374340292079, + 1.1220794892410257, 2.3639146242995768, 2.64283880414943, 8.95783107183879, 0.15584834882323761, + 0.7667338050095298, 5.577710795267362, 3.009862381185312, 7.195528282969081, 7.200021981915867, + 3.2521214123037474, 0.16781246449047607, 7.0440305216612025, 7.162211941733039, 6.607483411238427, + 3.7628002442053465, 0.945823450296398, 0.6405381447778209, 0.29978982655180736, 2.0193881642839093, + 0.4220928558487209] + class Colour: - WHITE = (255,255,255) - GRAY = (255/2,255/2,255/2) - LIGHT_GRAY = (255*2/3,255*2/3,255*2/3) - BLACK = (0,0,0) + WHITE = (255, 255, 255) + GRAY = (255 / 2, 255 / 2, 255 / 2) + LIGHT_GRAY = (255 * 2 / 3, 255 * 2 / 3, 255 * 2 / 3) + BLACK = (0, 0, 0) RED = (255, 0, 0) - GREEN = (0,255,0) - BLUE = (0,0,255) - YELLOW = (255/2, 255/2,0) + GREEN = (0, 255, 0) + BLUE = (0, 0, 255) + YELLOW = (255 / 2, 255 / 2, 0) -- 2.54.0