Here’s how I’m coming along, a little variance from the course, I put printing the intro text into its own function called from within GameSetup. Refactoring this into a series of checks was a little challenge, because I had built all the logic in if/else/if/else blocks, but I got it after a bit!
#include "BullCowCartridge.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& input) // When the player hits enter
{
ClearScreen();
if (bGameOver)
{
SetupGame();
}
else
{
ProcessGuess(input);
}
}
void UBullCowCartridge::SetupGame()
{
HiddenWord = TEXT("zero");
Attempts = HiddenWord.Len();
bGameOver = false;
PrintIntro();
}
void UBullCowCartridge::PrintIntro()
{
PrintLine(TEXT("Welcome to the Bulls and Cows game!\n"));
PrintLine(FString::Printf(TEXT("Guess the %d letter word!"), HiddenWord.Len()));
PrintLine(FString::Printf(TEXT("You have %d attempts!"), Attempts));
PrintLine(TEXT("\nEnter your guess:"));
}
void UBullCowCartridge::ProcessGuess(const FString& word)
{
if (IsMatch(word))
{
PrintLine(TEXT("You won!"));
EndGame();
return;
}
PrintLine(TEXT("That was not the right word..."));
if (!IsLength(word))
{
PrintLine(FString::Printf(TEXT("\nYour guess must be %d characters long!"), HiddenWord.Len()));
PrintLine(TEXT("\nGuess again! (free)\n"));
return;
}
if (!IsIsogram(word))
{
PrintLine(TEXT("\nYour guess must be an isogram!"));
PrintLine(TEXT("\nGuess again! (free)\n"));
return;
}
if (HasLives())
{
--Attempts;
PrintLine(TEXT("\nGuess again!\n"));
PrintLine(FString::Printf(TEXT("You have %d more attempts left"), Attempts));
}
else
{
PrintLine(FString::Printf(TEXT("You have lost! The correct word was:\n%s"), *HiddenWord));
EndGame();
}
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nPress enter to play again!"));
}
bool UBullCowCartridge::IsIsogram(const FString& word)
{
return true;
}
bool UBullCowCartridge::IsLength(const FString& word)
{
return (word.Len() == HiddenWord.Len());
}
bool UBullCowCartridge::IsMatch(const FString& word)
{
return (word == HiddenWord);
}
bool UBullCowCartridge::HasLives()
{
return (Attempts > 0);
}