Hello again! Here’s how my code is looking so far as of the end of this lecture.
I have a fear that it’s looking a bit messy or unorganized, so I’d appreciate some advice on tidying it up if needs be!
// 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();
PrintLine(TEXT("The HiddenWord is: %s."), *HiddenWord); //Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) { // When the player hits enter
//If game is over, then do ClearScreen() and SetUpGame().
if (bGameOver) {
ClearScreen();
SetUpGame();
}
else { //Checking the PlayerGuess while the game is not over.
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetUpGame() {
//Welcoming the Player
PrintLine(TEXT("Hi there! Welcome to Bulls and Cows!"));
HiddenWord = TEXT("cakes");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("To begin, please press Tab to start, \ntype in your guess and hit Enter!")); //Prompt the player for Guess
PrintLine(TEXT("You have %i lives left"), Lives);
}
void UBullCowCartridge::EndGame() {
bGameOver = true;
PrintLine(TEXT("\nPress enter to play again."));
}
void UBullCowCartridge:: ProcessGuess(FString Guess) {
if (Guess == HiddenWord) { //Check if Guess is the HiddenWord and it is correct
PrintLine(TEXT("Your guess was: ")+Guess);
PrintLine(TEXT("Correct! You got the hidden word and won!"));
EndGame();
return;
}
if (Lives <= 0) { //Check if lives are greater than 0
ClearScreen();
PrintLine(TEXT("Game over! You have no more lives left!"));
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len()) { //Check if the guess is not the same length as the HiddenWord.
//The lives will NOT be decremented if you enter wrong number of characters.
PrintLine(TEXT("Incorrect! Try guessing again! \nYou have %i lives remaining."), Lives);
PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());
return;
}
/* if (Guess != IsIsogram) { //Check if Guess is an Isogram
}
*/
if (Guess != HiddenWord) {//Check if Guess is NOT the HiddenWord
PrintLine(TEXT("Your guess was: ")+Guess);
PrintLine(TEXT("Incorrect! You have lost a life!"));
PrintLine(TEXT("You have %i lives remaining."), --Lives);
return;
}
//Show the player Bulls and Cows
}
Thanks for checking it out and providing any advice!