Revert "Merge pull request #3 from sgoudham/rework"

This reverts commit 754e6c34d9, reversing
changes made to 1c9ef1c2ee.
main
Conor Walker 3 years ago
parent cd4138624c
commit 1dde14d435

Binary file not shown.

@ -3,12 +3,6 @@ import random
import sys import sys
from datetime import date 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 # Gets the input from a user - number param is used to display the guess number the user is on
def get_guess(number): def get_guess(number):
@ -33,6 +27,7 @@ def resource_path(relative_path):
# If file is not present, uses a small fallback list of words. # 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. # 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(): def get_random_word():
lines = []
try: try:
lines = open(resource_path('words.txt')).read().splitlines() lines = open(resource_path('words.txt')).read().splitlines()
except IOError: except IOError:
@ -44,8 +39,11 @@ def get_random_word():
# Compares the users input with the word to be guessed. Returns true if the guess is correct, false otherwise # 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_to_guess, word_guessed): def check_word_is_right(word_guessed, word_to_guess):
return "".join(word_to_guess) == str(word_guessed).upper() # The 'word_to_guess' will now be a list - Goudham if str(word_guessed).upper() == str(word_to_guess).upper():
return True
else:
return False
# Makes a cool header. # Makes a cool header.
@ -60,70 +58,50 @@ def header():
# Prints welcome messages # Prints welcome messages
def intro(): def intro():
print("Welcome to...") print("Welcome to...")
print(add_colour(Fore.MAGENTA, header())) # You could make this multicoloured - Goudham print(header())
print("A totally original guessing game!") print("A totally original guessing game!")
# Prints the results, a thank-you message and gracefully exits # Prints the results, a thank-you message and gracefully exits
def game_over(user_was_right, correct_word): def game_over(user_was_right, correct_word):
if user_was_right: if user_was_right:
print("Congratulations! You figured it out!") # No need to display the word again? - Goudham print("Congratulations! The word was", ''.join(correct_word).capitalize())
else: else:
print("Ran out of guesses! The word was:", add_colour(Fore.GREEN, ''.join(correct_word).capitalize())) print("Ran out of guesses! The word was: ", "".join(correct_word))
input("Thanks for playing! Press ENTER to exit :)") input("Thanks for playing! Press ENTER to exit :)")
sys.exit(0) sys.exit(0)
# Store how many times each letter appears in the word - Goudham # Takes in the users guess and the myriad lists involved in the game logic, and compares the letters in the guess
def count_letter_frequency(word): # with the word to be guessed.
letter_frequency = {} # Inserts the letter into the appropriate list, based on its presence & position (or lack thereof)
for letter in word: def check_letter_in_word(users_guess_input, word_to_be_guessed, users_guess_results, present_in_word, not_in_word):
letter_frequency[letter] = letter_frequency[letter] + 1 if letter in letter_frequency else 1 for i in range(len(users_guess_input)):
return letter_frequency 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:
# Initialise the letter frequency map to count up the way as a letter is encountered out of position - Goudham if users_guess_input[i] in users_guess_results and users_guess_input[i] not in word_to_be_guessed[i:]:
def initialise_letter_frequency(word): break
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 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: else:
wordle_string.append(add_colour(Fore.LIGHTBLACK_EX, letter)) # Grey if it doesn't exist - Goudham present_in_word.add(users_guess_input[i])
raw_letters.append(letter) else:
not_in_word.add(users_guess_input[i])
# 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. # Main game flow.
def game_logic(): def game_logic():
not_in_word, present_in_word = set(), set()
word_to_be_guessed = list(get_random_word().upper()) word_to_be_guessed = list(get_random_word().upper())
counter = 1 counter = 1
while counter < 7: while counter < 7:
users_guess_input = get_guess(counter) users_guess_input = get_guess(counter)
print("Result:", get_wordle_string(users_guess_input, word_to_be_guessed)) # No need to pass in lists users_guess_results = ["_"] * 5
if check_word_is_right(word_to_be_guessed, users_guess_input): 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):
game_over(True, word_to_be_guessed) 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))
print("Result: " + " ".join(users_guess_results))
counter += 1 counter += 1
game_over(False, word_to_be_guessed) game_over(False, word_to_be_guessed)
@ -134,8 +112,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
# 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() main()

Loading…
Cancel
Save