Skip to content

Commit

Permalink
main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai-Denzel-Jane committed Sep 6, 2023
1 parent b51a918 commit 7e9811b
Showing 1 changed file with 40 additions and 58 deletions.
98 changes: 40 additions & 58 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,76 +102,58 @@ def select_word():

# Main logic, this is where the scoring is implemented
def algorithm(user_word, selected_word, tries, config, cheat):
"""Main logic, this is where the scoring is implemented
Parameters:
user_word (str): The word the user is guessing
selected_word (str): The word that the user is guessing
tries (int): The amount of tries the user has
config (dict): The configuration file
cheat (bool): Whether or not to load the game in cheat mode.
Returns:
None
"""
user_word = list(map(str.upper, user_word))
selected_word = list(map(str.upper, selected_word))

position = 0 # Could be removed
output = [" "] * len(selected_word) # Output list to show hints

selected_word_counts = {} # Dictionary to store the counts of each letter in the selected word
for letter in selected_word:
if letter in selected_word_counts:
selected_word_counts[letter] += 1
user_word = user_word.upper()
selected_word = selected_word.upper()
output = [" "] * len(selected_word)
matched_indices = []

for i, letter in enumerate(user_word):
if letter == selected_word[i]:
output[i] = "X"
matched_indices.append(i)

for i, letter in enumerate(user_word):
if output[i] != "X" and letter in selected_word:
if selected_word.count(letter) > user_word.count(letter):
output[i] = "-"
elif selected_word.count(letter) == 1 and letter not in output:
output[i] = "-"
else:
output[i] = "*"
elif output[i] != "X":
output[i] = "-"

for i, letter in enumerate(user_word):
if output[i] == "X":
print(Fore.WHITE + letter, Fore.GREEN, output[i])
elif output[i] == "*":
print(Fore.WHITE + letter, Fore.YELLOW, output[i])
else:
selected_word_counts[letter] = 1

for position in range(len(selected_word)):
if user_word[position] == selected_word[position]:
output[position] = "X" # Updates the output list at the position where the letter was both in the word and correct position
selected_word_counts[user_word[position]] -= 1 # Decrease the count for correctly guessed letters
elif user_word[position] in selected_word_counts and selected_word_counts[user_word[position]] > 0:
output[position] = "*" # Updates the output list at the position where the letter is in the word but not at that position
selected_word_counts[user_word[position]] -= 1 # Decrease the count for partially correct letters
else:
output[position] = "-" # Updates the output list at the position where the letter is not in the word

print(Fore.WHITE + letter, Fore.RED, output[i])

# Some nice colors and printing of the output
for position in range(len(output)):
if output[position] == f"X":
print(Fore.WHITE + user_word[position], Fore.GREEN, output[position])
elif output[position] == f"*":
print(Fore.WHITE + user_word[position], Fore.YELLOW, output[position])
else:
print(Fore.WHITE + user_word[position], Fore.RED, output[position])

# Determines if the user won or lost
if user_word == selected_word:
if tries == 1:
print(f"You won with one try remaining. That was close!")
print("You won with one try remaining. That was close!")
tries = 1

if cheat:
print(Fore.GREEN + f"You win, but you cheated so you don't deserve it", Style.RESET_ALL)
tries = 0 # Makes score 0 because of cheating
print(Fore.GREEN + "You win, but you cheated so you don't deserve it", Style.RESET_ALL)
tries = 0
else:
print(Fore.GREEN + f"You win", Style.RESET_ALL)
print(Fore.GREEN + "You win", Style.RESET_ALL)

if config.get(f"upload_score", False) and tries != 0:
client.info_input(tries_remaining=tries) # If configuration is set to true, attempt to upload scores
if config.get("upload_score", False) and tries != 0:
client.info_input(tries_remaining=tries)
else:
tries -= 1 # Decrease the tries counter
tries -= 1
if tries > 0:
print(Fore.MAGENTA + f"Try again")
print(tries, f"Tries remaining")
get_user_input(selected_word, cheat, config, tries) # Indicate the need for another input
print(Fore.MAGENTA + "Try again")
print(tries, "Tries remaining")
get_user_input(selected_word, cheat, config, tries)
else:
print(Fore.RED + "fYou lose")
if config.get(f"show_word_after_loss", False):
print(f"The word was:", " ".join(selected_word)) # If configuration set to true, show what the correct word was
print(Fore.RED + "You lose")
if config.get("show_word_after_loss", False):
print("The word was:", " ".join(selected_word))


# User Input
Expand Down

0 comments on commit 7e9811b

Please sign in to comment.