From 44a3c1713d4102f085d76e2ec58fefae01745b04 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 2 Feb 2022 08:21:46 +0000 Subject: [PATCH] Implement coloured output & rework logic to display like wordle --- src/pydle.py | 63 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/src/pydle.py b/src/pydle.py index ee2ff70..e000061 100644 --- a/src/pydle.py +++ b/src/pydle.py @@ -66,29 +66,53 @@ def intro(): # Prints the results, a thank-you message and gracefully exits def game_over(user_was_right, correct_word): - coloured_correct_word = add_colour(Fore.GREEN, ''.join(correct_word).capitalize()) if user_was_right: - print("Congratulations! The word was:", coloured_correct_word) + print("Congratulations! You figured it out!") # No need to display the word again? - Goudham else: - print("Ran out of guesses! The word was:", coloured_correct_word) + print("Ran out of guesses! The word was:", add_colour(Fore.GREEN, ''.join(correct_word).capitalize())) input("Thanks for playing! Press ENTER to exit :)") sys.exit(0) -# Takes in the users guess and the myriad lists involved in the game logic, and compares the letters in the guess -# with the word to be guessed. -# Inserts the letter into the appropriate list, based on its presence & position (or lack thereof) +# Store how many times each letter appears in the word - Goudham +def count_letter_frequency(word): + letter_frequency = {} + for letter in word: + letter_frequency[letter] = letter_frequency[letter] + 1 if letter in letter_frequency else 1 + return letter_frequency + + +# Initialise the letter frequency map to count up the way as a letter is encountered out of position - Goudham +def initialise_letter_frequency(word): + letter_frequency = {} + for letter in word: + letter_frequency[letter] = 0 + return letter_frequency + + # So this is like a massive rework right, it's basically like the actual wordle game with colouring - Goudham -def check_letter_in_word(users_guess_input, word_to_be_guessed, users_guess_results): - correct = [] - for i in range(len(users_guess_input)): - if word_to_be_guessed[i] == users_guess_input[i]: - correct.append(users_guess_input[i]) - users_guess_results[i] = add_colour(Fore.GREEN, users_guess_input[i]) - elif users_guess_input[i:] in word_to_be_guessed and users_guess_input[i] not in correct: - users_guess_results[i] = add_colour(Fore.YELLOW, users_guess_input[i]) +def get_wordle_string(users_guess_input, word_to_be_guessed): + green_letter_map = count_letter_frequency(word_to_be_guessed) + yellow_letter_map = initialise_letter_frequency(word_to_be_guessed) + raw_letters = [] + wordle_string = [] + + # Doing a first pass to just check the correct letters and updating 'green_letter_map' - Goudham + for index, letter in enumerate(users_guess_input): + if word_to_be_guessed[index] == letter: + green_letter_map[letter] = green_letter_map[letter] - 1 + wordle_string.append(add_colour(Fore.GREEN, letter)) # Green if it's in the right place - Goudham else: - users_guess_results[i] = users_guess_input[i] + wordle_string.append(add_colour(Fore.LIGHTBLACK_EX, letter)) # Grey if it doesn't exist - Goudham + raw_letters.append(letter) + + # On the second pass, if there are any letters out of position using the knowledge from the first pass - Goudham + for index, letter in enumerate(raw_letters): + if letter in word_to_be_guessed and yellow_letter_map[letter] < green_letter_map[letter]: + yellow_letter_map[letter] = yellow_letter_map[letter] + 1 + wordle_string[index] = add_colour(Fore.YELLOW, letter) # Yellow if it's in the wrong position - Goudham + + return " ".join(wordle_string) # Return the list as string since no need to keep track of other lists - Goudham # Main game flow. @@ -97,12 +121,9 @@ def game_logic(): counter = 1 while counter < 7: users_guess_input = get_guess(counter) - # You can check if the game is over right after the user enters the word - Goudham + print("Result:", get_wordle_string(users_guess_input, word_to_be_guessed)) # No need to pass in lists if check_word_is_right(word_to_be_guessed, users_guess_input): game_over(True, word_to_be_guessed) - users_guess_results = ["_"] * 5 - check_letter_in_word(users_guess_input, word_to_be_guessed, users_guess_results) - print("Result: " + " ".join(users_guess_results)) counter += 1 game_over(False, word_to_be_guessed) @@ -113,6 +134,8 @@ def main(): if __name__ == '__main__': - # I promise that this just started out with wanting to add colours... turned into a rework sorry :( - Goudham + # READ THIS -> I promise that this just started out with wanting to add colours... + # And then it just turned into a rework sorry :( - Goudham + init() # Read -> https://pypi.org/project/colorama/ - Goudham main()