I am trying to make a wordle clone in C, but I cannot deal with duplicate yellow letters. For example, if the master word is "apple"
and the user guess is "aplle"
(not an actual word, just an example), my code returns "APlLE"
instead of "AP*LE"
. Note that an uppercase character denotes a green character, lowercase denotes a yellow one, and a *
denotes a gray one.
This is the logic for handling the user guess. I'm really unsure as to how I can get a solution for this, if it's even possible with this logic. Any help is much appreciated. I've tried looking at other similar questions, but the code looks fairly different and this is my first time coding in C. Thanks in advance!
do {
scanf("%5s", userGuess);
correctGuess = strcmp(userGuess,chosenWord);
if (strlen(userGuess) != 5) { // checks if the user input is not a 5 letter word
printf("Please enter a five letter word.\n");
}
else if (correctGuess == 0) { // 0 means that the two strings are the same
printf("You are correct!\n");
guessNumber++;
printf("%i", guessNumber);
}
else {
guessNumber++;
for (int i = 0; i <= 4; i++) { // green letter implementation
if (userGuess[i] == chosenWord[i]) {
userGuess2[i] = toupper(userGuess[i]);
}
else {
int yellowChar = 0; // handles yellow letters - doesn't know how to deal with duplicates
for (int j = 0; j < 5; j++) {
if (i != j && userGuess[i] == chosenWord[j]) {
userGuess2[i] = tolower(userGuess[i]);
yellowChar = 1;
break;
}
}
if (!yellowChar) { // handles gray letters
userGuess2[i] = '*';
}
}
}
printf("%s\n", userGuess2);
}
}
while(guessNumber <= 5 && correctGuess != 0);