Home > Software engineering >  How to account for multiple of the same characters in Wordle?
How to account for multiple of the same characters in Wordle?

Time:08-04

I was just wondering if you guys could give me some advice as to what I should do to find whether a letter is in the answer but is in the wrong position, allowing it to take multiple of the same character, as it starts to work incorrectly.

I DO UNDERSTAND THAT THERE HAVE BEEN OTHERS WITH SIMILAR QUESTIONS, BUT I HAVE USED TERMCOLOR MEANING AND I AM NOT SURE HOW TO TRANSLATE FROM THAT SOLUTION INTO THIS CONTEXT.

Here are a few test cases for you guys:

The answer is apple And guess props

Both p's should be yellow.

The answer is apple And guess happy

The 'a' should be yellow, the first 'p' should be green, and the second one should be yellow

Here is the code where I attempt to make Wordle a complete replication of normal Wordle. It is really inefficient code, and I apologize for anyone reading this code prematurely, but this was the closest I got to it working, as maybe one test case works, but then when you check the other one, it turns out the other one is messed up now.

An absolute abomination for a function:

def letter_analyzation(guess, word, x, yellow, green):
    green.clear()
    yellow.clear()
    guess_output = ''
    for i in range(0, 5, 1): # Making a loop to shorten code
        if guess[i] in word:
            if guess[i] != word[i]:
                if guess[i] in yellow or guess[i] in green:
                    if word.count(word[i]) > 1:
                        if word.count(word[i]) == green.count(word[i]) or word.count(word[i]) == yellow.count(word[i]):
                            x = colored(str(guess[i]), 'grey')
                            guess_output  = x                       
                        else:
                            x = colored(str(guess[i]), 'yellow')
                            guess_output  = x
                            yellow.append(guess[i])
                    else:
                        x = colored(str(guess[i]), 'grey')
                        guess_output  = x   
                else:
                    x = colored(str(guess[i]), 'yellow')
                    guess_output  = x
                    yellow.append(guess[i])
            if guess[i] == word[i]:
                x = colored(str(guess[i]), 'green')
                guess_output  = x
                green.append(guess[i])
        else:
            x = colored(str(guess[i]), 'grey')
            guess_output  = x
    return guess_output

Here is the code in completeness:

import random
from termcolor import colored

# Giving values to some variables:
word_list = ['apple']
guess_output = ''
x = ''
green = []
yellow = []

def word_picker(): # Generates a random number as the index for the word list.
    random_num = random.randint(0, len(word_list) - 1)
    word = word_list[random_num]
    return word

def input_word(): # Using recursion to take a guess
    guess = input('Guess: ')
    if len(guess) == 5:
        return guess.lower()
    else:
        return input_word()

def letter_analyzation(guess, word, x, yellow, green):
    green.clear()
    yellow.clear()
    guess_output = ''
    for i in range(0, 5, 1): # Making a loop to shorten code
        if guess[i] in word:
            if guess[i] != word[i]:
                if guess[i] in yellow or guess[i] in green:
                    if word.count(word[i]) > 1:
                        if word.count(word[i]) == green.count(word[i]) or word.count(word[i]) == yellow.count(word[i]):
                            x = colored(str(guess[i]), 'grey')
                            guess_output  = x                       
                        else:
                            x = colored(str(guess[i]), 'yellow')
                            guess_output  = x
                            yellow.append(guess[i])
                    else:
                        x = colored(str(guess[i]), 'grey')
                        guess_output  = x   
                else:
                    x = colored(str(guess[i]), 'yellow')
                    guess_output  = x
                    yellow.append(guess[i])
            if guess[i] == word[i]:
                x = colored(str(guess[i]), 'green')
                guess_output  = x
                green.append(guess[i])
        else:
            x = colored(str(guess[i]), 'grey')
            guess_output  = x
    return guess_output

def check_win(guess, word):
    if guess == word:
        win = True
        return win
    else:
        win = False
        return win

def Wordle():
    # Making global variables to avoid any errors:
    global word_list
    global guess_output
    global green
    global yellow

    # Defining some variables to allow the 
    win = False
    counter = 0
    
    word = word_picker()
    print(word) # <-------------- DELETE LATER
    while counter <= 4 and win == False:
        counter  = 1 # Counts to make the while loop better

        guess = input_word() # Takes an input

        # Coloring the letters:
        guess_output = letter_analyzation(guess, word, x, green, yellow)
        print(guess_output)

        win = check_win(guess, word) # Checks if a win is present.

        # Win case:
        if win == True:
            print('Congrats, you have won in ', counter, 'moves!')
    
    # Losing case:
    if counter >= 5:
        print('You have lost, the word was ' , word , '.')

Wordle()

# Reset case:
reset = input('Would you like to try again? y/n: ')
if reset == 'y':
    Wordle()
 

Thank you so much, and I am sorry for any pain I have caused you.

CodePudding user response:

It's not a problem with termcolor, just a problem with the logic in your function letter_analyzation().

The easiest way would be to make a copy of the answer word and subtract individual letters from it every time there's a matching letter.

There are only three different cases for a letter's location: proper location, improper location, and not in the word.

Here's the updated letter_analyzation function.

def letter_analyzation(guess, word, x, yellow, green):
    green.clear()
    yellow.clear()
    guess_output = ''
    not_found = str(word)
    for i in range(0, 5, 1): # Making a loop to shorten code
        if guess[i] in word:
            if guess[i] == word[i]:
                x = colored(str(guess[i]), 'green')
                not_found = not_found.replace(str(guess[i]), '', 1)
            else:
                if guess[i] in not_found:
                    x = colored(str(guess[i]), 'yellow')
                    not_found = not_found.replace(str(guess[i]), '', 1)
                else:
                    x = colored(str(guess[i]), 'grey')
        else:
            x = colored(str(guess[i]), 'grey')
        guess_output  = x
    return guess_output
  • Related