main.py
Python script, ASCII text executable, with very long lines (381)
1#!/usr/bin/python3 2 3__version__ = "v0.1" 4 5from termcolor import colored as col 6import colorama 7from random import choice 8import time 9import argparse 10 11colorama.init() 12 13logo = col("K", "light_yellow", attrs=["bold"]) + col("O", "light_grey", attrs=["bold"]) + col("N", "light_green", attrs=["bold"]) + col("S", "light_green", attrs=["bold"]) + col("O", "light_grey", attrs=["bold"]) + col("L", "light_yellow", attrs=["bold"]) + col("D", "light_green", attrs=["bold"]) + col("L", "light_green", attrs=["bold"]) + col("E", "light_grey", attrs=["bold"]) 14 15 16parser = argparse.ArgumentParser(prog="tty-wordle", description="A Wordle for your console.") 17parser.add_argument("-a", "--analyser", dest="analyser", action="store_true", help="add the analyser, a row of all found letters") 18parser.add_argument("-p", "--placeholder", dest="placeholder", action="store_true", help="add a word placeholder") 19parser.add_argument("-x", "--hard", dest="hard", action="store_true", help="enable hard Wordle: you must use all found letters in your guesses") 20parser.add_argument("--delay", dest="delay", type=float, default=1, help="time to show error messages for, in seconds") 21parser.add_argument("-l", "--list", dest="words", default="./guesses.txt", help="file to pull allowed guesses from") 22parser.add_argument("-n", "--answers", dest="answers", default="./answers.txt", help="file to pull a random answer from") 23parser.add_argument("-t", "--tries", dest="guesses", type=int, default=6, help="number of tries") 24args = parser.parse_args() 25analyser = args.analyser 26placeholder = args.placeholder 27hard = args.hard 28errorDelay = args.delay 29guesses = args.guesses 30with open(args.words) as file: 31words = [line.rstrip().upper() for line in file] 32with open(args.answers) as file: 33answers = [line.rstrip().upper() for line in file] 34word = choice(answers) 35 36 37def clearLines(n=1): 38up = '\033[1A' 39clear = '\x1b[2K' 40for i in range(n): 41print(up, end=clear) 42 43 44def getPlaced(letter): 45return col("[" + letter + "]", "white", "on_green", attrs=["bold"]) 46 47 48def getFound(letter): 49return col("(" + letter + ")", "white", "on_yellow") 50 51 52def getBad(letter): 53return col(" " + letter + " ", "white", "on_grey") 54 55 56placed = {} 57found = set() 58bad = set() 59 60 61def getWord(guess): 62global word, placed, found, bad 63ci = 0 64formatted = "" 65correct = True 66 67for c in guess: 68if c == word[ci]: 69formatted += getPlaced(c) 70placed[c] = ci 71elif c in word: 72formatted += getFound(c) 73correct = False 74found.add(c) 75else: 76formatted += getBad(c) 77correct = False 78bad.add(c) 79ci += 1 80return formatted, correct 81 82 83def getPlaceholder(): 84global word 85placeholder = col(len(word) * "[ ]", "light_grey", "on_grey", attrs=["dark"]) 86return placeholder 87 88 89# Information 90print(f"{ logo } {__version__} ({ col('tty-wordle --help', 'light_blue') } for more information)") 91 92alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 93print(f"Guess a {len(word)}-letter word") 94print(f"{ getPlaced(choice(alphabet)) } - letter is in the word and at the correct position") 95print(f"{ getFound(choice(alphabet)) } - letter appears in the word, but at another position") 96print(f"{ getBad(choice(alphabet)) } - letter isn't anywhere in the word") 97 98 99# Game 100i = 1 101while i <= guesses: 102if placeholder: 103print(getPlaceholder()) 104try: 105guess = input(f"Your Guess ({i}/{guesses}) > ").upper() 106except KeyboardInterrupt: 107try: 108print() 109clearLines() 110giveup = input(f"{ col('Give up?', 'light_red') } (y/n/^C) > ").upper() 111if giveup == "Y" or giveup == "YES": 112clearLines(1 + placeholder) 113print(f"{ col('Game Over:', 'light_red', attrs=['bold']) } You gave up at guess {i}/{guesses}.") 114break 115else: 116clearLines(1 + placeholder) 117continue 118except KeyboardInterrupt: 119print() 120exit() 121 122if hard: 123invalid = False 124for c in found: 125if c not in guess: 126invalid = True 127for c in placed.keys(): 128if c != guess[placed[c]]: 129invalid = True 130if invalid: 131print(f"{ col('Error:', 'light_red', attrs=['bold']) } Hard Mode: Please use all revealed hints") 132time.sleep(errorDelay) 133clearLines(2) 134continue 135if guess not in words or len(guess) != len(word): 136if not guess: 137clearLines() 138continue 139else: 140print(f"{ col('Error:', 'light_red', attrs=['bold']) } Invalid word") 141try: 142time.sleep(errorDelay) 143clearLines(2 + placeholder) 144continue 145except KeyboardInterrupt: 146print() 147clearLines(3 + placeholder) 148continue 149else: 150clearLines(1 + (analyser if i > 1 else 0) + placeholder) 151formatted, correct = getWord(guess) 152print(str(i) + ". " + formatted) 153if analyser: 154print("Letters:", end=" ") 155for c in placed.keys(): 156print(getPlaced(c), end="") 157for c in found: 158print(getFound(c), end="") 159for c in bad: 160print(getBad(c), end="") 161print() 162if correct: 163print(f"{ col('Game Over:', 'light_green', attrs=['bold']) } You guessed the word in {i} tries.") 164break 165if i == guesses and not correct: 166print(f"{ col('Game Over:', 'light_red', attrs=['bold']) }: You lost.") 167i += 1 168print(f"The word was { col(word, color='light_green', attrs=['underline']) }") 169