My code with lives implemented!

Hi there!
I have this code that I’ve implemented with lives! :slight_smile:

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
        {
            SubstractLive();

            if (HasLives())
            {
                ClearScreen();

                PrintLine(TEXT("Wrong word"));

                // Asks if Input Len is not equal to Hiddenword Len.
                if (!IsInputLenEqualHiddenwordLen(Input))
                {
                    PrintLine(TEXT("The word has not %i characters!"), GetHiddenwordLen());
                }
                
                PrintLine(TEXT("Now you have %i lives."), Lives);
            }
            else
            {   
                ClearScreen();
                         
                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"); 

    // Set Lives equal to numbers of characters of hidden word. 
    SetLives(GetHiddenwordLen());

    // Welcomes the player
    PrintLine(TEXT("Welcome to Bull Cows!"));
    PrintLine(TEXT("The hidden word is %s"), *HiddenWord);
    PrintLine(TEXT("You have a total of %i lives."), Lives);
    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;
}

int32 UBullCowCartridge::GetHiddenwordLen()
{
	return HiddenWord.Len();
}

bool UBullCowCartridge::IsGameOver()
{
    return bGameOver;
}

bool UBullCowCartridge::HasLives()
{
    return Lives > 0;
}

bool UBullCowCartridge::IsInputMatchingHiddenWord(FString Input)
{
    return Input.Compare(GetHiddenWord()) == 0;
}

bool UBullCowCartridge::IsInputLenEqualHiddenwordLen(FString Input)
{
    return Input.Len() == GetHiddenwordLen();
}

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();

	// Check if lives are greater than zero.
	bool HasLives();

	// Has input guess the same characters of hidden word characters length?
	bool IsInputLenEqualHiddenwordLen(FString Input);

	bool IsInputMatchingHiddenWord(FString Input);	

	// Gets the current Lives amount
	int32 GetLives();

	// Gets the number of characters for Hiddenword
	int32 GetHiddenwordLen();		

	// Members
	private:
	FString HiddenWord;
	int32 Lives;
	bool bGameOver;
	
};

2 Likes

Nice job adding lives functionality :+1:

1 Like

Thank you very much! :grinning_face_with_smiling_eyes:

Privacy & Terms