A bit different cause I am doing it more to my personal taste, but is working well and quite similarly.
// Fill out your copyright notice in the Description page of Project Settings.
#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();
ProcessGuess(Input);
}
void UBullCowCartridge::SetupGame()
{
HiddenWord = TEXT("melon");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Welcome to Bulls and Cows game!"));
PrintLine(TEXT("Try and guess the %i letter isogram"), HiddenWord.Len());
PrintLine(TEXT("You got %i lives"), Lives);
}
void UBullCowCartridge::EndGame(bool bIsWon)
{
bGameOver = true;
switch (bIsWon)
{
case true:
PrintLine(TEXT("Congratulations, you have won the game!"));
PrintLine(TEXT("hit Enter to start a new one..."));
break;
case false:
PrintLine(TEXT("You ran out of lives."));
PrintLine(TEXT("hit Enter to start a new game..."));
break;
}
}
void UBullCowCartridge::CheckLives()
{
if (Lives <= 0)
{
EndGame(false);
}
else
{
PrintLine(TEXT("You got %i lives left..."), Lives);
PrintLine(TEXT("Try another guess"));
}
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (bGameOver)
{
SetupGame();
return;
}
if (Guess == HiddenWord)
{
EndGame(true);
return;
}
else
{
--Lives;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The hidden word has %i characters!"), HiddenWord.Len());
PrintLine(TEXT("Your guess had %i."), Guess.Len());
CheckLives();
return;
}
else
{
PrintLine(TEXT("Wrong answer!"));
CheckLives();
return;
}
}