Search code examples
pythonlistwordle-game

Wordle copy having trouble with duplicate letters. (Python)


I’m trying to make a copy of the game Wordle but I’m having trouble dealing with duplicate letters. Here’s my code and I explain my logic a bit below.

# a portion of the code, the logical bit #

def minigame(): # wordle
    global wordGuess
    delay_print([f'You have 6 chances to guess the 5 letter word. Heres the coloring guide: \n', colored('W', on_color='on_red') + ': The letter is wrong\n', colored('W', on_color='on_yellow') + ': The letter is right but in the wrong spot\n', colored('W', on_color='on_green') + ': The letter is right and in the correct spot\n'], 0.03)
    with open('valid-wordle-words.txt', 'r') as f:
        listWords = [line.strip() for line in f]
    with open('validWords.txt', 'r') as f:
        listValid = [line.strip() for line in f]
    wordGuess = random.choice(listWords)
    counter = 0
    
    while counter <= 5:
        guess = input('\n> ')
        if len(guess) != 5:
            delay_print("Your guess should be 5 letters long.")
        else:
            if guess in listValid or guess in listWords:
                counter += 1
                userGuess(guess, wordGuess)
            else:
                delay_print("Your guess is not valid.")
    if guess != wordGuess:
        delay_print('You lost :(. The word was {}\n'.format(wordGuess))
        retry(minigame)

def userGuess(guess: str, correct: str):
    if guess == correct:
        for x in correct:
            print(colored(x, on_color='on_green'), end='')
        delay_print("\nYou win!")
        exit()
    else:
        for index, (guess_letter, correct_letter) in enumerate(zip(guess, correct)):
            if guess.count(guess_letter) >= 2:
                numCor = 0
                if guess_letter == correct_letter:
                    print(colored(guess_letter, on_color='on_green'), end='')
                    correct = correct.replace(guess_letter, '-', 1)
                    numCor += 1
                elif guess_letter in correct and numCor != guess.count(guess_letter):
                    print(colored(guess_letter, on_color='on_yellow'), end='')
                    correct = correct.replace(guess_letter, '-', 1)
                else:
                    print(colored(guess_letter, on_color='on_red'), end='')
                
            else:
                if guess_letter == correct_letter:
                    print(colored(guess_letter, on_color='on_green'), end='')
                    correct = correct.replace(guess_letter, '-', 1)
                elif guess_letter in correct:
                    print(colored(guess_letter, on_color='on_yellow'), end='')
                    correct = correct.replace(guess_letter, '-', 1)
                elif guess_letter not in correct:
                    print(colored(guess_letter, on_color='on_red'), end='')

I explain the rules of Wordle (kinda) in the code, so go there if you aren’t aware of what the rules are. Anyways, here’s my logic. Let’s assume that the word I’m trying to guess is STOLE. Basically for each letter, I first check to see if there are two or more instances of the same letter in the guess (this is the problem of duplicates that I’ve been trying to solve, my logic is probably flawed though but this is what I came up with). I thought this would be a good idea since if there are two of the same letters in the guess like the T in LATTE, I would check the first T, giving it the green highlight, and then preventing the second T from getting a yellow highlight. Here’s the code in action: working code

Here’s an instance of where it doesn’t seem to go as planned: ignore my other guesses lol<- the problem is with my guess trees. The code is saying that there are two E’s in the final word, but if you look at the end, OFTEN only has one E.

Any help would be appreciated. Thank you!

This code is my attempt at trying to account for duplicates. I tried integrating a counter but that doesn’t seem to work. I think the error lies in the fundamentals of my logic but I can’t pinpoint where exactly.


Solution

  • You dont handle the duplicate letters properly, the same letter can be counted multiple times as a correct letter in the wrong position. we can create a separate function to compare the guess and correct word and return the colored output for each letter.

    def userGuess(guess: str, correct: str):
        if guess == correct:
            for x in correct:
                print(colored(x, on_color='on_green'), end='')
            delay_print("\nYou win!")
            exit()
        else:
            output = compare_guess_and_correct(guess, correct)
            for letter_color in output:
                print(letter_color, end='')
            print()
    
    def compare_guess_and_correct(guess, correct):
        output = []
        correct_matched_positions = []
        yellow_count = {}
    
        for i, g in enumerate(guess):
            if g == correct[i]:
                output.append(colored(g, on_color='on_green'))
                correct_matched_positions.append(i)
            else:
                yellow_count[g] = yellow_count.get(g, 0) + 1
    
        for i, g in enumerate(guess):
            if i not in correct_matched_positions and g in correct:
                if yellow_count[g] > 0:
                    output.append(colored(g, on_color='on_yellow'))
                    correct = correct.replace(g, '', 1)
                    yellow_count[g] -= 1
                else:
                    output.append(colored(g, on_color='on_red'))
            elif i not in correct_matched_positions:
                output.append(colored(g, on_color='on_red'))
    
        return output