1

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.

Wild Zyzop
  • 580
  • 1
  • 3
  • 13
  • You need some code somewhere that (1) counts how many times the letter appears in the wordle and (2) keeps track of how many times it's appeared in the user input so far. – Alex Hall Jun 11 '23 at 10:42
  • What should be the output if the user enters "sssss"? – Wild Zyzop Jun 11 '23 at 10:52
  • For that it should be ----X if the wordle is "woods". –  Jun 11 '23 at 10:55
  • Your output example for "brass" does not match the example for input "sssss" from the previous comment. Perhaps "brass" should be "- O - X -"? If one of the duplicate letters is in its place, what should be the result for the other occurrences of that letter? – Wild Zyzop Jun 11 '23 at 14:06
  • The result depends on how many of the same letters exist in the wordle. For example, brass (the wordle) has 2 "s" and sassy (user input) has 3 "s", meaning that one s does not exist in the wordle so the symbol for that should be a "-". Since the 4th letter of "brass" matches the 4th letter of the wordle "sassy", the symbol is a "X". For the last "s", it exists in the wordle, but does not match the position of the user's input, so it is an "O". This is also true for the letter "a". So the result is "O O - X -", it could also be "- O O X -" as it is the same. I hope I understood your question –  Jun 11 '23 at 14:32

1 Answers1

0

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.

Wild Zyzop
  • 580
  • 1
  • 3
  • 13
  • misread this, but definitely works after testing it! thank you for your answer! –  Jun 12 '23 at 02:47