1
$\begingroup$

I am trying to reverse engineer the following image. So far, the way I would take into account of duplicate letter handling is the following My Code Output.

I am unsure how I could code this such that the duplicates are not colored incorrectly. For example, in my code output in the first row, the first "N" should not be yellow, but rather dark grey since the other "N" at the center is correctly placed and shaded in green. As there is only 1 N in the secret word (BANAL), then only 1 N must be shaded.

My current approach consists of the following:

public static char[] duplicates(char[] MysteryWordArr, char[] guessArr){
    int dup = 0; // adds up dup values
        for (int i=0; i<WORD_LENGTH; i++){
            if (MysteryWordArr[i] != guessArr[i]){
                for(int j=0; j<WORD_LENGTH; j++){
                    if ((MysteryWordArr[i] == guessArr[j])) {               
                        MysteryWordArr[i] = ' ';
                    }
                else{
                    continue;
                }
                }
            }
        }
        return MysteryWordArr;
    }

My reasoning is that provided the char at same index of secret and guess are not the same, if the index of secret is equal to any other index of guess, then change that index of secret to a random string (in this empty). This I presumed, would mean that the duplicates would be grey as they cannot compare to their identical value in secret.

What do you think I should do.

Wordle Duplicate Letter Handling

$\endgroup$
3
  • 2
    $\begingroup$ Coding questions and asking for feedback on your code is off-topic here. If you're asking for an algorithm, that could be suitable, but it sounds like you're hoping for code. Also, we are looking for a more specific question than "what do you think I should do"? $\endgroup$
    – D.W.
    Commented Feb 26, 2022 at 9:20
  • $\begingroup$ I was merely describing what I had done so far. I want to know what would be an effective algorithm that I should implement. $\endgroup$ Commented Feb 26, 2022 at 11:24
  • $\begingroup$ OK. An algorithm to do what? What are the requirements? I don't understand what you want the algorithm to do. A good way to specify an algorithmic problem is to identify what are the inputs to the algorithm, and what is the desired output from the algorithm. Don't force us to guess what you're trying to achieve. $\endgroup$
    – D.W.
    Commented Feb 27, 2022 at 0:03

1 Answer 1

1
$\begingroup$

I sat on that same issue for a couple weeks and wracked my brain over it. I finally found someone who posted the solution in Python. I was able to convert it to Visual Basic for my self and it works perfectly. Here is the code if you still need it.

Function Compare(Guess As String, Target as String) As String
Dim Output As String = "-----"

'Match all of the Exact Match Characters first
For i = 1 To 5
    If Guess.Substring(i - 1, 1) = Target.Substring(i - 1, 1) Then
        Output = Output.Remove(i - 1, 1).Insert(i - 1, "X")
        Target = Target.Remove(i - 1, 1).Insert(i - 1, "-")
    End If
Next

'Next Match Characters in the word but in the wrong spot
For i = 1 To 5
    For j = 1 To 5
        If (Guess.Substring(j - 1, 1) = Target.Substring(i - 1, 1)) And Output.Substring(j - 1, 1) = "-" Then
            Output = Output.Remove(j - 1, 1).Insert(j - 1, "O")
            Target = Target.Remove(i - 1, 1).Insert(i - 1, "-")
        End If
    Next
Next
Compare = Output

End Function

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.