|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import sys |
| 4 | +import random |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +from argparse import ArgumentParser |
| 9 | + |
| 10 | +import requests |
| 11 | + |
| 12 | +__version__ = "0.0.2" |
| 13 | + |
| 14 | + |
| 15 | +def prepare_wordlist(): |
| 16 | + response = requests.get( |
| 17 | + "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt" |
| 18 | + ) |
| 19 | + if response.ok: |
| 20 | + with open("wordlist-en.txt", "w") as f: |
| 21 | + for line in response.text.split("\n"): |
| 22 | + word = line.strip() |
| 23 | + if len(word) == 5: |
| 24 | + f.write(f"{word}\n") |
| 25 | + |
| 26 | + |
| 27 | +def valid_word(word): |
| 28 | + if len(word) == 5: |
| 29 | + return word in word_table.keys() |
| 30 | + else: |
| 31 | + return False |
| 32 | + |
| 33 | + |
| 34 | +def check_guess(guess, available_letters): |
| 35 | + guess = guess.lower() |
| 36 | + if guess == secret_word: |
| 37 | + return True, secret_word.upper() |
| 38 | + |
| 39 | + feedback = "?????" |
| 40 | + secret_word_copy = secret_word |
| 41 | + |
| 42 | + # Mark all correct letters and remove from secret_word_copy |
| 43 | + for i, c in enumerate(secret_word_copy): |
| 44 | + if c == guess[i]: |
| 45 | + feedback = feedback[:i] + c.upper() + feedback[i + 1 :] |
| 46 | + secret_word_copy = secret_word_copy[:i] + "*" + secret_word_copy[i + 1 :] |
| 47 | + guess = guess[:i] + "*" + guess[i + 1 :] |
| 48 | + |
| 49 | + for i, c in enumerate(guess): |
| 50 | + if c == "*": |
| 51 | + continue |
| 52 | + |
| 53 | + if c in secret_word_copy: |
| 54 | + feedback = feedback[:i] + c + feedback[i + 1 :] |
| 55 | + secret_word_copy = secret_word_copy.replace(c, "*", 1) |
| 56 | + else: |
| 57 | + feedback = feedback[:i] + "*" + feedback[i + 1 :] |
| 58 | + available_letters = available_letters.replace(c, "") |
| 59 | + |
| 60 | + return False, feedback, available_letters |
| 61 | + |
| 62 | + |
| 63 | +def main(secret_word, word_table): |
| 64 | + attempts = 0 |
| 65 | + max_attempts = 6 |
| 66 | + |
| 67 | + guesses = {} |
| 68 | + feedbacks = [] |
| 69 | + available_letters = "abcdefghijklmnopqrstuvwxyz" |
| 70 | + |
| 71 | + def show_grid(): |
| 72 | + for i in range(max_attempts): |
| 73 | + if i < len(feedbacks): |
| 74 | + print(feedbacks[i]) |
| 75 | + else: |
| 76 | + print("?????") |
| 77 | + |
| 78 | + print(f"\nAttempt {attempts+1}/{max_attempts}") |
| 79 | + print(f"\n{available_letters}\n----") |
| 80 | + |
| 81 | + # start yourdle |
| 82 | + while attempts < max_attempts: |
| 83 | + show_grid() |
| 84 | + guess = input("Your guess:").strip() |
| 85 | + if valid_word(guess): |
| 86 | + if guess not in guesses.keys(): |
| 87 | + attempts += 1 |
| 88 | + guesses[guess] = 1 |
| 89 | + done, feedback, available_letters = check_guess( |
| 90 | + guess, available_letters |
| 91 | + ) |
| 92 | + feedbacks.append(feedback) |
| 93 | + if done: |
| 94 | + print( |
| 95 | + f"You guessed the secret word: {secret_word.upper()} in {attempts} attempts" |
| 96 | + ) |
| 97 | + show_grid() |
| 98 | + break |
| 99 | + else: |
| 100 | + if attempts >= max_attempts: |
| 101 | + print(f"You failed to guess the word: {secret_word.upper()}") |
| 102 | + break |
| 103 | + else: |
| 104 | + continue |
| 105 | + else: |
| 106 | + print(f"You have already guessed {guess.upper()}.") |
| 107 | + else: |
| 108 | + print(f"{guess.upper()} is not a valid word.") |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + parser = ArgumentParser( |
| 113 | + prog="yourdle.py", description="A basic Wordle for your terminal" |
| 114 | + ) |
| 115 | + |
| 116 | + parser.add_argument( |
| 117 | + "--word-file", |
| 118 | + help="Path to a wordlist with one 5 letter word per line", |
| 119 | + default="wordlist-en.txt", |
| 120 | + ) |
| 121 | + parser.add_argument("--secret", help="A chosen word to try guess", default=None) |
| 122 | + |
| 123 | + args = parser.parse_args() |
| 124 | + |
| 125 | + if not os.path.exists(args.word_file): |
| 126 | + prepare_wordlist() |
| 127 | + |
| 128 | + word_table = {} |
| 129 | + # load the words into a word_table |
| 130 | + with open(args.word_file, "r") as f: |
| 131 | + word = f.readline() |
| 132 | + while word: |
| 133 | + word_table[word.strip()] = 1 |
| 134 | + word = f.readline() |
| 135 | + |
| 136 | + if args.secret is None: |
| 137 | + secret_word = random.choice(list(word_table.keys())) |
| 138 | + else: |
| 139 | + secret_word = args.secret |
| 140 | + |
| 141 | + main(secret_word, word_table) |
0 commit comments