From cd4bb9397ed6ee536e03f9351d7fbd41e4a04e1d Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 2 Feb 2022 05:47:04 +0000 Subject: [PATCH] Implement coloured output & rework logic to display like wordle --- src/pydle.py | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/pydle.py b/src/pydle.py index 3f62320..ee2ff70 100644 --- a/src/pydle.py +++ b/src/pydle.py @@ -3,6 +3,12 @@ import random import sys from datetime import date +from colorama import init, Fore + + +def add_colour(colour, uncoloured_string): # I COULD NOT RESIST NAMING IT THIS I'M SORRY - Goudham + return colour + uncoloured_string + Fore.RESET # Add the given colour and then resets it - Goudham + # Gets the input from a user - number param is used to display the guess number the user is on def get_guess(number): @@ -27,7 +33,6 @@ def resource_path(relative_path): # If file is not present, uses a small fallback list of words. # Shuffles based on a seed and selects a consistent member of the list to allow consistency between runs on same day. def get_random_word(): - lines = [] try: lines = open(resource_path('words.txt')).read().splitlines() except IOError: @@ -39,11 +44,8 @@ def get_random_word(): # Compares the users input with the word to be guessed. Returns true if the guess is correct, false otherwise -def check_word_is_right(word_guessed, word_to_guess): - if str(word_guessed).upper() == str(word_to_guess).upper(): - return True - else: - return False +def check_word_is_right(word_to_guess, word_guessed): + return "".join(word_to_guess) == str(word_guessed).upper() # The 'word_to_guess' will now be a list - Goudham # Makes a cool header. @@ -58,16 +60,17 @@ def header(): # Prints welcome messages def intro(): print("Welcome to...") - print(header()) + print(add_colour(Fore.MAGENTA, header())) # You could make this multicoloured - Goudham print("A totally original guessing game!") # 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", ''.join(correct_word).capitalize()) + print("Congratulations! The word was:", coloured_correct_word) else: - print("Ran out of guesses! The word was: ", "".join(correct_word)) + print("Ran out of guesses! The word was:", coloured_correct_word) input("Thanks for playing! Press ENTER to exit :)") sys.exit(0) @@ -75,32 +78,30 @@ def game_over(user_was_right, correct_word): # 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) -def check_letter_in_word(users_guess_input, word_to_be_guessed, users_guess_results, present_in_word, not_in_word): +# 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]: - users_guess_results[i] = users_guess_input[i] - elif users_guess_input[i] in word_to_be_guessed: - if users_guess_input[i] in users_guess_results and users_guess_input[i] not in word_to_be_guessed[i:]: - break - else: - present_in_word.add(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]) else: - not_in_word.add(users_guess_input[i]) + users_guess_results[i] = users_guess_input[i] # Main game flow. def game_logic(): - not_in_word, present_in_word = set(), set() word_to_be_guessed = list(get_random_word().upper()) counter = 1 while counter < 7: users_guess_input = get_guess(counter) - users_guess_results = ["_"] * 5 - check_letter_in_word(users_guess_input, word_to_be_guessed, users_guess_results, present_in_word, not_in_word) - if check_word_is_right(word_to_be_guessed, users_guess_results): + # You can check if the game is over right after the user enters the word - Goudham + if check_word_is_right(word_to_be_guessed, users_guess_input): game_over(True, word_to_be_guessed) - print("Incorrect letters: " + ', '.join(not_in_word)) - print("Correct letters in the wrong place: " + ', '.join(present_in_word)) + 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) @@ -112,4 +113,6 @@ def main(): if __name__ == '__main__': + # I promise that this just started out with wanting to add colours... turned into a rework sorry :( - Goudham + init() # Read -> https://pypi.org/project/colorama/ - Goudham main()