I am writing a program supposed to simulate the once-popular Wordle game. I have written most of the code, and it all appears to work correctly except for the function that checks for the letters in the right or wrong position or not in the word at all. No error message is returned, but the code seems to acknowledge letters that are in the guesses but not in the actual word, and I can’t figure out why. Any assistance will be much appreciated.
Here is the code for the function:
int check_word(string guess, int wordsize, int status[], string choice)
{
int score = 0;
for (int i = 0; guess[i] != '\0'; i++)
{
for (int j = 0; j < wordsize; j++)
{
if (guess[i] == choice[i])
{
status[i] = EXACT;
score += EXACT;
break;
}
else if (guess[i] == choice[j] && status[i] != EXACT)
{
status[i] = CLOSE;
score += CLOSE;
}
else if (guess[i] != choice[j] && status[i] != EXACT && status[i] != CLOSE)
{
status[i] = WRONG;
score += WRONG;
}
}
}
return score;
}
I tried creating a loop to iterate over the characters in guess, and inside that loop, I made another loop to iterate over the actual word to be guessed (choice). It is supposed to check if the current character is in the right spot, and if it is, assign a particular score to the corresponding location in an array I created, if not then it’s supposed to compare the current character to every letter in the actual word, and assign another score if found, if not, then a final score is allocated. It somewhat works, but I noticed that it sometimes acknowledges letters that are in guess but not in the actual word, I.e., choice, and I do not know why.