While I do understand the importance of early returns for the efficiency of the program I prefered to leave my ProcessGuess() function with only one early return. So my code so far is like this:
#include "BullCowCartridge.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
// Setting up the game
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if(bGameOver) // is game over?
{
// Clear terminal
ClearScreen();
// Setup the game again
SetupGame();
}
else // check player guess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
// Set game variables
HiddenWord = TEXT("report");
Lives = HiddenWord.Len();
bGameOver = false;
// Welcoming the player with messages
PrintLine(TEXT("Welcome to BullCows game!"));
PrintLine(TEXT("Try to guess the hidden word!"));
PrintLine(TEXT("The hidden word has %i letters"), HiddenWord.Len());
PrintLine(TEXT("You have %i lives"), Lives);
PrintLine(TEXT("Please press the TAB key, write your guess word and press enter to continue"));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press enter to play again."));
}
void UBullCowCartridge::ProcessGuess(FString PlayerGuess)
{
// Check if the PlayerGuess is the HiddenWord
if(PlayerGuess.Equals(HiddenWord, ESearchCase::IgnoreCase))
{
// The Player wins the game
ClearScreen();
PrintLine(TEXT("You guessed the word!"));
PrintLine(TEXT("The hidden word is: %s."), *HiddenWord);
PrintLine(TEXT("YOU WIN!"));
EndGame();
return;
}
else
{
// Clear terminal
ClearScreen();
// Print messages for the player telling about the mistake in the guess
PrintLine(TEXT("Ooh sorry your guess is not correct!"));
// Remove a life since the player missed in the guess
PrintLine(TEXT("You lost a life. Now you have %i lives."), --Lives);
// Check if the PlayerGuess is an isogram
// Check if the PlayerGuess has the same amount of letters as the HiddenWord
if(PlayerGuess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Please remember that the hidden word has %i letters."), HiddenWord.Len());
}
// Tell the player the number of bulls and cows on the guess
// Check if there are no more lives
if(Lives == 0)
{
// The Player is out of lives then loses the game
PrintLine(TEXT(" You are out of lives.\nYOU LOST!"));
EndGame();
}
}
}