pythonduplicateswordle-game

How to handle duplicate letters in wordle


I am wondering how to handle duplicate characters in my wordle game.

Here is an example of the code:

wordle = "woods"

user_input = input("Enter guess: ")

feedback = []


for i in range(len(wordle)):
    if user_input[i] == wordle [i]:
        feedback.append("X")
    elif user_input[i] != wordle[i] and user_input[i] in wordle:
        feedback.append("O")
    else:
        feedback.append("-")

print()

for let in user_input:
    print(let, end=" ")

print()

for char in feedback:
    print(char, end=" ")

When the wordle is "woods" and the user input is "sassy", the output should be:

s a s s y
O - - - -

but instead it is:

s a s s y 
O - O O -

Another example:

If the wordle is "brass" and the user input is "sassy", the output should be:

s a s s y
O O - X -

but instead it is:

s a s s y 
O O O X -

For clarification, "X" is when the letter in the guessed word is also in the target word and is at the same spot. "O" means the letter in the guessed word is also in the target word but isn't at the same spot. "-" means that it's the wrong letter.


Solution

  • It is necessary to count the viewed letters that are in the wordle and take into account which of them are in their places. You can try like this:

    from collections import Counter
    
    def wordle_game(wordle, user_input):
        feedback = ["-"] * len(wordle)
        seen_counter = Counter()
    
        # finding the right letters in their places
        for i in range(len(wordle)):
            if user_input[i] == wordle[i]:
                feedback[i] = "X"
                seen_counter[user_input[i]] += 1
    
        # search for existing letters that are out of place
        for i in range(len(wordle)):
            if feedback[i] == "X":
                continue
            elif user_input[i] in wordle and seen_counter[user_input[i]] < wordle.count(user_input[i]):
                feedback[i] = "O"
                seen_counter[user_input[i]] += 1
    
        return feedback
    
    samples = [
        {"wordle":"brass", "input":"sassy"},
        {"wordle":"brass", "input":"ssarb"},
        {"wordle":"brass", "input":"Absss"},
    ]
    
    for sample in samples:
        wordle = sample["wordle"]
        user_input = sample["input"]
        feedback = wordle_game(wordle, user_input)
        print(" ".join(wordle))
        print("-" * (len(wordle) * 2 - 1))
        print(" ".join(user_input))
        print(" ".join(feedback))
        print()
    
    

    Sample output:

    b r a s s
    ---------
    s a s s y
    O O - X -
    
    b r a s s
    ---------
    s s a r b
    O O X O O
    
    b r a s s
    ---------
    A b s s s
    - O - X X
    

    To count the viewed letters, I used the Counter class from the standard collections package. It is a subclass of Dictionary, but returns 0 for missing keys. See this article or library reference for more details.

    wordle.count(user_input[i]) returns the number of occurrences of a current letter in a word.