I'm working on a project to copycat a game called 'TERMO', a portuguese version of 'WORDLE'. The biggest problem I've encountered so far is outputting yellow letters(right letter but in a wrong position). Keeping it simple, I know this problem can be solved with another for, however it´s really bugging me if there is other options without overlaping for's.
If there aren't any solutions, I'll be glad to know.
selectedWord = "MOLDO" word = "OMODO"
What I was expecting my output to be:
O(yellow) M(yellow) O(normal) D(green) O(green)
What I'm getting as output:
O(yellow) M(yellow) O(yellow) D(green) O(green)
string checkWord(string word, string selectedWord){
// 0 for green
// 1 for yellow
// 2 for normal
// string assuming "normal" for all letters
string result = {'2','2','2','2','2'};
for(int i = 0; i < 5; ++i){
char letter = word[i];
//check if it's green
if(letter == selectedWord[i]){
result[i] = '0';
continue;
}
//check if it's yellow
bool willBeGreen = false;
for(int k = 0; k < 5; ++k){
//Check if will be green
if(word[i] == selectedWord[k] && word[k] == letter){
willBeGreen = true;
break;
}
if(letter == selectedWord[k] && k != i && result[k] != '0'){
result[i] = '1';
// break;
}
}
if(willBeGreen){
result[i] = '2';
}
}
return result;
}
EDIT: Actually my previous version of this was wrong...
You can't determine if a guessed letter is yellow without knowing which letters are going to be green; that is, you do need two passes. In the following I do the first pass via a range pipeline.
In the second pass, you need to keep track of the hidden letters that will not ultimately be green in the output and that have not been "used" yet by a yellow letter. In the following I use a multiset of letters to keep track. Output is like
YY-GGfor yellow, yellow, gray, green, green.(this is C++23 because of the use of
std::ranges::to<T>andzipbut illustrates the idea)