This is what my ProcessGuess functions looks like at the point of finishing this lecture:
void UBullCowCartridge::ProcessGuess(FString Guess)
{
//Check PlayerGuess
if(Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Sorry, the hidden word should be \n%i characters long."), HiddenWord.Len());
}
else if(!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters are allowed."));
}
else //passed input criteria
{
if(Guess != HiddenWord) //guess is wrong
{
PrintLine(TEXT("Lives: %i"), --Lives); //Decriment and Display Lives
//Check number of bulls and cows (function)
//Display number of bulls and cows
PrintLine(TEXT("Some number of bulls"));
PrintLine(TEXT("Some number of cows"));
}
}
PrintLine(TEXT("Guess Again."));
if(Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("You have won!"));
EndGame();
}
else if(Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have lost!"));
PrintLine(TEXT("The hidden word was %s"), *HiddenWord);
EndGame();
}
}
I realize that I didn’t actually use the return to break out of some of the if statements, but since the beginning of making the game, I structured them differently. I could use returns and it might technically be more optimized, but this would force me to add the “Guess Again” text many times individually.
I structured my code in this way for a couple of reasons. First off, I wanted to do it in the order that makes sense. The user entered a guess so we first have to make sure it’s a valid guess while the lecture checks if the user won first before validating the guess. Again, I could use returns here but it would only stop the last if statement checking if the guess is right which doesn’t take much time. I suppose if I checked if the guess was right, it wouldn’t go through the whole bull cows calculation which is probably the most “demanding” part of the code, but I’m coding it in a more readable way rather than efficient since this game is so simple. If the guess is valid, it calculates the bulls and cows (this part would not help with any returns and would break the code). And I say guess again.
The last bit does the checks for EndGame all at once - both the win and lose conditions so it’s readable and if either happens, it will clear the screen and stop the other text from appearing such as the lives, bulls and cows, and “guess again” because that is all irrelevant once the game is over.