"git@gitlab.caltech.edu:cs24-19fa/git_rec_nano.git" did not exist on "fe9da9425e57258c97ca417c058018be0735089a"
Commit e8e765a9 authored by Adam Blank's avatar Adam Blank
Browse files

add wordle starter

parent cf745189
Showing with 178831 additions and 0 deletions
+178831 -0
This diff is collapsed.
UPPERCASE_LETTERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
LOWERCASE_LETTERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
def uppercase(word):
out = ""
for x in word:
if x in LOWERCASE_LETTERS:
out += UPPERCASE_LETTERS[LOWERCASE_LETTERS.index(x)]
else:
out += x
return out
def convert_list_to_strs(lst):
lst_copy = []
for x in lst:
lst_copy.append(str(x))
return lst_copy
def is_answer(s, yn):
return s is not None and len(s) > 0 and uppercase(s[0]) in yn
def prompt_yes_no(prompt):
result = None
while not is_answer(result, set(["Y", "N"])):
result = input(prompt)
return is_answer(result, set(["Y"]))
def ask_for_choice(prompt, valid_choices):
choices = convert_list_to_strs(valid_choices)
choice = None
while not choice:
proposed = input(prompt)
if proposed in choices:
choice = proposed
else:
print("That's not a valid choice! Try one of: " +
", ".join(choices) + ".")
return proposed
import random
from words import get_wordle_dictionary
from game_utils import *
SHOW_ANSWER: bool = True
WORD_LENGTH: int = 5
WORDLE_DICTIONARY: list[str] = get_wordle_dictionary(WORD_LENGTH)
def is_valid_guess(guess: str):
guess = guess.strip().upper()
if len(guess) != WORD_LENGTH:
print("You can only guess words with FIVE letters!")
return False
if guess not in WORDLE_DICTIONARY:
print("That's not a valid word with five letters!")
return False
return True
def how_many_exact_matches(word1: str, word2: str) -> int:
"""
Returns the number of characters that appear identically
at the same position in word1 and word2.
"""
...
def get_counts_of_letter(word: str, letter: str) -> int:
"""
Returns the number of occurrences of letter in word.
"""
...
def get_counts_of_letters(word: str) -> dict[str, int]:
"""
Returns a dictionary that maps all the distinct letters in word to
the number of times they appear in word.
For example, if word is "hello", this function would return
{"h": 1, "e": 1, "l": 2, "o": 1}.
"""
...
def how_many_overall_matches(guess: str, answer: str) -> int:
...
def how_many_misplaced_matches(guess, answer):
overall = how_many_overall_matches(guess, answer)
exact = how_many_exact_matches(guess, answer)
return overall - exact
def play_wordle_game():
answer = random.choice(WORDLE_DICTIONARY)
if SHOW_ANSWER:
print("(DEBUG) This answer is \"" + answer + "\"")
guess = None
while guess != answer:
# print("Nice job! That's correct!")
# print("You got " + str(exact) + " letter(s) exactly correct!")
# print("You got " + str(misplaced) + " letter(s) misplaced!")
again = True
while again:
play_wordle_game()
again = prompt_yes_no('Would you like to play again (y/n)? ')
from io import TextIOWrapper
DICTIONARY_FILE: str = '07/dictionary.txt'
def get_dictionary() -> list[str]:
words: list[str] = []
f: TextIOWrapper = open(DICTIONARY_FILE)
for word in f.readlines():
word = word.strip()
words.append(word)
f.close()
return words
def get_wordle_dictionary(word_length: int) -> list[str]:
words: list[str] = []
for word in get_dictionary():
if len(word) == word_length:
words.append(word)
return words
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment