BullCowCartridge.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
virtual void OnInput(const FString& Input) override;
void SetupGame();
void EndGame();
void MinusLife();
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives, AttemptWarning;
bool bGameOver;
};
BullCowCartridge.cpp
// 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(FString::Printf(TEXT("Hidden word is: %s. "), *HiddenWord)); // Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
if (Input == HiddenWord)
{
PrintLine(TEXT("You win!"));
EndGame();
}
else
{
if (Input.Len() != HiddenWord.Len())
{
if (AttemptWarning++ < 2)
{
PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
PrintLine(TEXT("The hidden word is %i characters long. "), HiddenWord.Len());
if (Input.Len() < HiddenWord.Len())
{
PrintLine(TEXT("Your guess needs to be longer. "));
}
else
{
PrintLine(TEXT("Your guess needs to be shorter. "));
}
}
else
{
PrintLine(TEXT("Guessing carelessly lost you a life. "));
MinusLife();
}
}
else
{
if (Lives > 0)
{
PrintLine(TEXT("Wrong answer. "));
MinusLife();
}
else
{
PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
MinusLife();
}
}
}
}
//Check if isogram
//prompt to play again
//check user input
//play again or quit
}
void UBullCowCartridge::SetupGame()
{
PrintLine(TEXT("***Bull Cows Game*** "));
HiddenWord = TEXT("aspired");
Lives = HiddenWord.Len();
AttemptWarning = 0;
bGameOver = false;
PrintLine(TEXT("Guess the %i letter isogram: "), HiddenWord.Len()); //Prompt player for guess
PrintLine(TEXT("You have %i lives. "), Lives);
PrintLine(TEXT("Type your guess \nPress enter to continue... "));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("*** Game Over ***. "));
PrintLine(TEXT("Press enter to play again. "));
}
void UBullCowCartridge::MinusLife()
{
if (Lives > 0)
{
PrintLine(TEXT("You have %i lives remaining. "), --Lives);
AttemptWarning = 0;
}
else
{
PrintLine(TEXT("You have no lives remaining. "));
AttemptWarning = 0;
PrintLine(TEXT("You Lose"));
EndGame();
}
}