Edit: also, I intentionally don’t use an else because that code will run on every iteration and it’s intended to do nothing. So I omit it for cleanliness. The “else if” essentially IS the else, but it’s required to be have the if expression so that any old entry doesn’t fall through! Otherwise, the Cows++ could trigger on a correctly-sized word with no matches!
My code is quite different, but here it is –
I checked for a win at the top of all guess checking and validity to simply see if the words matched. If they did, it skips all other checking logic.
And I did the same when I added a Quit ability.
Actually, nevermind. I can’t upload my code, it’s way too long, across 6 files (I added a utilities file as well).
Here’s the win condition snippet though.
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
MyCurrentGuessTotal++;
FBullCowCount BullCowCount;
if (Guess == MyHiddenWord)
{
bPlayerHasWon = true;
}
else
{
for (size_t iGChar = 0; iGChar < Guess.length(); iGChar++)
for (size_t iHWChar = 0; iHWChar < MyHiddenWord.length(); iHWChar++)
if ((MyHiddenWord[iHWChar] == Guess[iGChar]) && (iHWChar == iGChar))
BullCowCount.Bulls++; // Letter match AND position match.
else if ((MyHiddenWord[iHWChar] == Guess[iGChar]))
BullCowCount.Cows++; // ONLY letter match.
/* else -- absolutely no else here! */
std::cout << "Bulls = " << BullCowCount.Bulls << ". ";
std::cout << "Cows = " << BullCowCount.Cows << std::endl;
PrintDblEndl();
}
return BullCowCount;
}
BullCowCount returns just junk values that are never used if the player guesses correctly.