Hi!
Here is my code!
I made it with some differences to get the code more organized.
CPP file:
#include "BullCowCartridge.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
InitGame(); // Play the Bull & Cows's Videogame!
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (IsGameOver())
{
ClearScreen();
InitGame();
}
else
{
if (IsInputMatchingHiddenWord(Input))
{
ClearScreen();
PrintLine(TEXT("Correct! You Win!"));
EndGame();
}
else
{
ClearScreen();
PrintLine(TEXT("Wrong word"));
if (Input.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The word has not %i characters!"), HiddenWord.Len());
}
PrintLine(TEXT("Game Over"));
EndGame();
}
}
// ----------------------------------------
// ----------- PSEUDOCODE -----------
// ----------------------------------------
//
// PrintWelcomeMessage()
//
// StartTheGame()
// {
// SetLifes(amount)
// SetHiddenWord(value)
//
// AsksForGuess()
//
// CHECK:
// If: Is not a Isogram && Chars's length match input's guess length.
//
// WinTheGame()
//
// Else:
//
// SubstractLife()
//
// CHECK:
// If: Are lifes > 0?
//
// AsksForGuess()
//
// Else:
//
// PrintGameLost()
// PromptTryAgain()
//
// CHECK:
// If: Play Again?
//
// AsksForGuess()
//
// Else:
//
// ExitTheGame()
//
// }
}
void UBullCowCartridge::InitGame()
{
SetGameOver(false);
SetHiddenWord("Cakes");
SetLives(4);
// Welcomes the player
PrintLine(TEXT("Welcome to Bull Cows!"));
PrintLine(TEXT("The hidden word is %s"), *HiddenWord);
PrintLine(TEXT("Type %i characters."), HiddenWord.Len());
PrintLine(TEXT("Press Enter to continue..."));
}
void UBullCowCartridge::EndGame()
{
SetGameOver(true);
PrintLine("Press enter to play again.");
}
void UBullCowCartridge::SetLives(int32 _lives)
{
Lives = _lives;
}
void UBullCowCartridge::SubstractLive()
{
--Lives;
}
void UBullCowCartridge::SetHiddenWord(FString word)
{
HiddenWord = word;
}
void UBullCowCartridge::SetGameOver(bool bValue)
{
bGameOver = bValue;
}
FString UBullCowCartridge::GetHiddenWord()
{
return HiddenWord;
}
int32 UBullCowCartridge::GetLives()
{
return Lives;
}
bool UBullCowCartridge::IsGameOver()
{
return bGameOver;
}
bool UBullCowCartridge::IsInputMatchingHiddenWord(FString Input)
{
return Input.Compare(GetHiddenWord()) == 0;
}
Header file:
#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;
// Executes the game.
void InitGame();
// Finalizes the game.
void EndGame();
// Set if videogame is over or not.
void SetGameOver(bool bValue);
// Sets the lives amount.
void SetLives(int32 amount);
// Substract 1 live.
void SubstractLive();
// Sets the hidden word.
void SetHiddenWord(FString word);
// Gets the hidden word.
FString GetHiddenWord();
// Has the videogame ended?
bool IsGameOver();
bool IsInputMatchingHiddenWord(FString Input);
// Gets the current Lives amount
int32 GetLives();
// Members
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
};