From 8f8e9511539ca7af7a5a89e8fc8149beda55d35e Mon Sep 17 00:00:00 2001 From: adithyagenie Date: Wed, 30 Nov 2022 18:27:18 +0530 Subject: [PATCH] Black and isort final run --- credentials.pickle | Bin 169 -> 256 bytes maze/menu.py | 3 +- maze/modules/PlayerBase_func.py | 93 +++++++++++++++++++------- maze/modules/about.py | 12 +++- maze/modules/maze.py | 30 +++++---- maze/modules/maze_saveandload.py | 15 +++-- pong.py | 14 +++- snake.py | 18 +++++- starter.py | 2 +- wordle/dictionary.py | 108 ++++++++++++++++++++----------- wordle/solver.py | 17 +++-- wordle/wordle-solver.py | 6 +- wordle/wordle.py | 68 +++++++++++-------- 13 files changed, 261 insertions(+), 125 deletions(-) diff --git a/credentials.pickle b/credentials.pickle index 093f73c6bfa8ef379e10626b444f90187ad697c5..c8fb88635c0b936830d55f17f67eaab9b359a638 100644 GIT binary patch delta 120 zcmZ3<*uXR)kx_qQl2J|uOQ~K1%hXr~2&kQ+(Zi8kl$ugfS&%xVhcmw-Ex#xwu^@9w z4_jhEL40P)lpYR~M2ocKL{qaVJ?tQ{?9|FBJqi}478a&Srj|yQsisMWCME_(sph6> R$w?*_mKG`IX{J+3^#B_$CVc<^ delta 33 ncmZo*TFE#ek 1: - for i in range(len(s1)): - lexicalCategories.append(s1[i]["lexicalCategory"]['id']) - if "verb" in lexicalCategories: - baseindex = s1[lexicalCategories.index("verb")]['entries'][0]['senses'][0] - defn = (baseindex['shortDefinitions'][0]) - if "synonyms" in baseindex: - no = 2 if len(baseindex["synonyms"]) > 3 else len(baseindex["synonyms"]) - while no: - synonyms.append(baseindex["synonyms"][no]["text"]) - no -= 1 - synonyms.reverse() - elif "noun" in lexicalCategories: - baseindex = s1[lexicalCategories.index("noun")]['entries'][0]['senses'][0] - defn = (baseindex['shortDefinitions'][0]) - if "synonyms" in baseindex: - no = 3 if len(baseindex["synonyms"]) > 3 else len(baseindex["synonyms"]) - while no: - synonyms.append(baseindex["synonyms"][no]["text"]) - no -= 1 - synonyms.reverse() + try: + if len(s1) > 1: + for i in range(len(s1)): + lexicalCategories.append(s1[i]["lexicalCategory"]["id"]) + if "verb" in lexicalCategories: + baseindex = s1[lexicalCategories.index("verb")]["entries"][0]["senses"][ + 0 + ] + defn = baseindex["shortDefinitions"][0] + if "synonyms" in baseindex: + no = ( + 2 + if len(baseindex["synonyms"]) > 3 + else len(baseindex["synonyms"]) + ) + while no: + synonyms.append(baseindex["synonyms"][no]["text"]) + no -= 1 + synonyms.reverse() + elif "noun" in lexicalCategories: + baseindex = s1[lexicalCategories.index("noun")]["entries"][0]["senses"][ + 0 + ] + defn = baseindex["shortDefinitions"][0] + if "synonyms" in baseindex: + no = ( + 3 + if len(baseindex["synonyms"]) > 3 + else len(baseindex["synonyms"]) + ) + while no: + synonyms.append(baseindex["synonyms"][no]["text"]) + no -= 1 + synonyms.reverse() + else: + baseindex = s1[0]["entries"][0]["senses"][0] + defn = baseindex["shortDefinitions"][0] + if "synonyms" in baseindex: + no = ( + 3 + if len(baseindex["synonyms"]) > 3 + else len(baseindex["synonyms"]) + ) + while no: + synonyms.append(baseindex["synonyms"][no]["text"]) + no -= 1 + synonyms.reverse() else: - baseindex = s1[0]['entries'][0]['senses'][0] - defn = (baseindex['shortDefinitions'][0]) + baseindex = s1[0]["entries"][0]["senses"][0] + defn = baseindex["shortDefinitions"][0] if "synonyms" in baseindex: no = 3 if len(baseindex["synonyms"]) > 3 else len(baseindex["synonyms"]) while no: synonyms.append(baseindex["synonyms"][no]["text"]) no -= 1 synonyms.reverse() - else: - baseindex = s1[0]['entries'][0]['senses'][0] - defn = (baseindex['shortDefinitions'][0]) - if "synonyms" in baseindex: - no = 3 if len(baseindex["synonyms"]) > 3 else len(baseindex["synonyms"]) - while no: - synonyms.append(baseindex["synonyms"][no]["text"]) - no -= 1 - synonyms.reverse() + except: + print("err") + defn = synonyms = "" return defn, synonyms diff --git a/wordle/solver.py b/wordle/solver.py index 625a476..e9a1f66 100644 --- a/wordle/solver.py +++ b/wordle/solver.py @@ -1,5 +1,6 @@ words = open("words.txt").read().split("\n") + def partition(word, words): # Get how many words will remain for each possible response partitions = [] @@ -8,9 +9,10 @@ def partition(word, words): for c in "MCW": for d in "MCW": for e in "MCW": - partitions.append(len(reduce(word, a+b+c+d+e, words))) + partitions.append(len(reduce(word, a + b + c + d + e, words))) return partitions + def reduce(word, result, words): # word: 5-letter word (lowercase) # result: 5-letter str consisting of M, C, W (misplaced, correct, wrong) @@ -19,16 +21,19 @@ def reduce(word, result, words): nres = [] for w in res: if s == "M": - if w[i] != word[i] and word[i] in w: nres.append(w) + if w[i] != word[i] and word[i] in w: + nres.append(w) if s == "C": - if w[i] == word[i]: nres.append(w) + if w[i] == word[i]: + nres.append(w) if s == "W": if w[i] != word[i]: - if not(word[i] in w) or word.count(word[i]) > 1: + if not (word[i] in w) or word.count(word[i]) > 1: nres.append(w) res = nres return res + print("WORDLE SOLVER") print("=============") # First guess is precomputed @@ -42,8 +47,8 @@ while result != "CCCCC": opt_size = float("inf") for word in words: p = partition(word, words) - avg_partition_size = sum(p)/len(p) + avg_partition_size = sum(p) / len(p) if opt_size > avg_partition_size: opt_size = avg_partition_size opt = word - # print(p) \ No newline at end of file + # print(p) diff --git a/wordle/wordle-solver.py b/wordle/wordle-solver.py index 4b7bb42..9ada072 100644 --- a/wordle/wordle-solver.py +++ b/wordle/wordle-solver.py @@ -8,7 +8,9 @@ correctletters = ["-"] * 5 while maxguesses: try: - correctguess = input("\nEnter correct letter positions (use - for other letters): ").lower() + correctguess = input( + "\nEnter correct letter positions (use - for other letters): " + ).lower() uselessguess = input("Enter all incorrect letters without space: ").lower() wantedguess = input("Enter all letters of incorrect postiions: ").lower() @@ -48,4 +50,4 @@ while maxguesses: except KeyboardInterrupt: print("\n\nThe word was found!") break - maxguesses -= 1 \ No newline at end of file + maxguesses -= 1 diff --git a/wordle/wordle.py b/wordle/wordle.py index 4e353d9..8769758 100644 --- a/wordle/wordle.py +++ b/wordle/wordle.py @@ -1,9 +1,10 @@ -# A slightly more readable version of wordle-curses +import curses +import random +import time -import curses, random, time -from wordle.dictionary import defnsyn import maze.menu import maze.modules.maze as m1 +from wordle.dictionary import defnsyn quitwordle = False words = open("wordle\\words.txt", "r").read().split("\n") @@ -15,7 +16,7 @@ completionMessages = [ "Splendid!", "Amazing!", "Great!", - "Good!" + "Good!", ] # Draw one row of the board @@ -30,6 +31,7 @@ def writeWord(s, word, remark, y): curses.color_pair(colorPairBindings[color]), ) + # Score a word def score(guess, word, alphabet): res = [" "] * 5 @@ -37,8 +39,12 @@ def score(guess, word, alphabet): # First process correct letters for i, c in enumerate(guess): - if c == word[i]: #checking if guess letter corresponds to letter of the same index in chosen word - charIndex = ord(c) - 97 # 97 corresponds to a - gives alphabet number. (eg. h = 8) + if ( + c == word[i] + ): # checking if guess letter corresponds to letter of the same index in chosen word + charIndex = ( + ord(c) - 97 + ) # 97 corresponds to a - gives alphabet number. (eg. h = 8) counts[charIndex] += 1 res[i] = "c" # correct spot alphabet[charIndex] = "c" @@ -48,7 +54,9 @@ def score(guess, word, alphabet): if c != word[i]: charIndex = ord(c) - 97 counts[charIndex] += 1 - if c in word and word.count(c) >= counts[charIndex]: # if freq of letters in guess lesser than freq in word + if ( + c in word and word.count(c) >= counts[charIndex] + ): # if freq of letters in guess lesser than freq in word res[i] = "w" # wrong spot if alphabet[charIndex] != "c": alphabet[charIndex] = "w" @@ -58,6 +66,7 @@ def score(guess, word, alphabet): return "".join(res), alphabet + # Render current board # Updates alphabet use state + renders colours of guesses def render(s, guesses, alphabet): @@ -74,15 +83,16 @@ def render(s, guesses, alphabet): writeWord(s, w, r, i * 2 + 7) s.addstr(len(guesses) * 2 + 10, 0, " ") + # Accept word from user input def getWord(s, y): word = "" while True: - writeWord(s, word, "u" * len(word), y) # u = default blue colour + writeWord(s, word, "u" * len(word), y) # u = default blue colour k = s.getch() - if k == 8: # backspace + if k == 8: # backspace word = word[:-1] - elif k == 27: # esc + elif k == 27: # esc global quitwordle quitwordle = True return "hello" @@ -91,43 +101,42 @@ def getWord(s, y): elif chr(k).isalpha() and len(word) < 5: word += chr(k) + # Run one game of Wordle def run(s): s.clear() - word = random.choice(words) #chosen word + word = random.choice(words) # chosen word with open("log.txt", "a") as f: - f.write("Chosen word: "+ word+"\n") + f.write("Chosen word: " + word + "\n") defn, synonyms = defnsyn(word) - guesses = [] # stores each guess and its result - alphabet = ["u"] * 26 # current status of each letter whether used or not + guesses = [] # stores each guess and its result + alphabet = ["u"] * 26 # current status of each letter whether used or not # c = correct positon, w = correct letter but not position, n = wrong letter, u = not used # "ccccc" means all letters are in correct spot while not (len(guesses)) or (guesses[-1][1] != "ccccc" and len(guesses) < 6): if quitwordle: return - render(s, guesses, alphabet) # Update current state of board from start + render(s, guesses, alphabet) # Update current state of board from start guess = getWord(s, len(guesses) * 2 + 7).lower() - if not (guess in words): # Check if given word is valid + if not (guess in words): # Check if given word is valid s.addstr(len(guesses) * 2 + 10, 0, "INVALID WORD", curses.color_pair(1)) s.refresh() time.sleep(1) continue res, alphabet = score(guess, word, alphabet) guesses.append([guess, res]) - render(s, guesses, alphabet) #Renders final board + render(s, guesses, alphabet) # Renders final board # Ending spiel s.addstr(len(guesses) * 2 + 6, 0, "╰─┴─┴─┴─┴─╯") if guesses[-1][1] != "ccccc": - s.addstr(len(guesses) * 2 + 8, 0, "No more tries - the word was " + word.upper()) - else: s.addstr( - len(guesses) * 2 + 8, - 0, - completionMessages[len(guesses)] + len(guesses) * 2 + 8, 0, "No more tries - the word was " + word.upper() ) + else: + s.addstr(len(guesses) * 2 + 8, 0, completionMessages[len(guesses)]) if defn and synonyms: - s.addstr(len(guesses) * 2 + 9, 0, word+": ", curses.color_pair(2)) + s.addstr(len(guesses) * 2 + 9, 0, word + ": ", curses.color_pair(2)) synonyms = ", ".join(synonyms) s.addstr(len(guesses) * 2 + 9, 8, defn) s.addstr(len(guesses) * 2 + 10, 0, "Some synonyms: ", curses.color_pair(2)) @@ -137,6 +146,7 @@ def run(s): finalscore = allocatescore(guesses) return finalscore + def allocatescore(guesses): finalscore = 0 if len(guesses) <= 3: @@ -149,6 +159,7 @@ def allocatescore(guesses): finalscore = 20 return finalscore + # Main function def main(s): # Initialize colors @@ -164,10 +175,11 @@ def main(s): curses.init_pair(p[0], p[1], curses.COLOR_BLACK) # Run game finalscore = run(s) - with open("log.txt", "a") as f: - f.write(str(finalscore)+"\n") - while s.getch() == -1: - pass + if not finalscore: + finalscore = 0 + else: + while s.getch() == -1: + pass m1.play(s, executeguest=True, outerscore=finalscore, outergame="wordle") maze.menu.menu(s) - return \ No newline at end of file + return