To show my progress for the full CPP file
#include "BullCowCartridge.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); //Debug Line
}
void UBullCowCartridge::OnInput(const FString &Input) // When the player hits enter
{
ClearScreen();
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
CheckGuess(Input);
if (Lives == 0)
{
PrintLine(TEXT("You have lost, the hidden word was %s"), *HiddenWord);
EndGame();
return;
}
if (!bGameWon)
{
PrintLine(TEXT("Your guess was wrong"));
PrintLine(TEXT("Guess again, %i guess(es) remaining"), Lives);
}
}
}
void UBullCowCartridge::SetupGame()
{
HiddenWord = TEXT("market");
Lives = HiddenWord.Len();
bGameOver = false;
bGameWon = false;
bMulligan = false;
PrintLine(TEXT("Welcome to the Bull Cow Game!"));
PrintLine(TEXT("You have %i guesses"), HiddenWord.Len());
PrintLine(TEXT("Guess a %i letter isogram and press enter:"), HiddenWord.Len());
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
if (bGameWon)
{
PrintLine(TEXT("You guessed correctly!!!"));
}
PrintLine(TEXT("\nPress enter to play again"));
}
void UBullCowCartridge::CheckGuess(FString Guess)
{
if (Guess == HiddenWord)
{
bGameWon = true;
EndGame();
return;
}
if (Guess == TEXT(""))
{
PrintLine(TEXT("Please type a guess"));
return;
}
if (bMulligan)
{
--Lives;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Guess was not %i characters long"), HiddenWord.Len());
Mulligan();
return;
}
//Check Isogram
if (!IsIsogram(Guess))
{
PrintLine(TEXT("Guess has repeating characters"));
Mulligan();
}
}
void UBullCowCartridge::Mulligan()
{
if (!bMulligan)
{
PrintLine(TEXT("This is your one mulligan"));
}
bMulligan = true;
}
bool UBullCowCartridge::IsIsogram(FString Word)
{
int32 Checker = Word.Len() - 1;
int32 Counter = Checker - 1;
const TCHAR* IsoCheck = *Word;
while (Checker >= 0)
{
while (Counter >= 0)
{
if (IsoCheck[Checker] == IsoCheck[Counter])
{
return false;
}
--Counter;
}
--Checker;
Counter = Checker - 1;
}
return true;
}