I cleaned up my code and got a little bit ahead of the lecture by implementing the lives system. I think this would be a very doable challenge to implement in this lecture because previous lessons in this course have already taught me the knowledge/syntax required (I had no previous C++ experience and didn’t have to reference any outside sources).
The comments I added are just placeholders for future functionality I’ll learn in the upcoming lectures, but I think they are at least in the appropriate location within the code.
// 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();
if(bGameOver)
{
SetupGame();
}
else
{
PrintLine(TEXT("You guessed: %s"), *Input);
}
//To Do: If Input is not an isogram, tell player and ask them to guess again
if(HiddenWord.Len() != Input.Len())
{
PrintLine(TEXT("Your guess wasn't %i letters long. \nTry again."), HiddenWord.Len());
}
else if(Input == HiddenWord)
{
PrintLine(TEXT("Correct! You win!"));
EndGame();
}
else
{
PrintLine(TEXT("Incorrect."));
Lives--;
if(Lives > 0)
{
PrintLine(TEXT("You have %i lives remaining."), Lives);
//To do: Calculate Bulls and Cows and display to player
PrintLine(TEXT("Try again! Guess the %i letter word."), HiddenWord.Len());
}
else
{
PrintLine(TEXT("You ran out of lives. You lose!"));
PrintLine(TEXT("The word was %s."), *HiddenWord);
EndGame();
}
}
}
void UBullCowCartridge::SetupGame()
{
HiddenWord = TEXT("isogram"); //To do: generate HiddenWord from a list of isograms
Lives = 5;
bGameOver = false;
PrintLine(TEXT("Welcome to the Bull Cow game!"));
PrintLine(TEXT("Press the Tab key to start."));
PrintLine(TEXT("Guess a %i letter word,"), HiddenWord.Len());
PrintLine(TEXT("then press enter to continue."));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press Enter to start over or Tab to exit."));
}