My ProcessGuess() and my code order so far

Hi!
I have these codes. Two are CPP/Header files, and the third one is the scope ordered in the way that I believe that it could be optimized :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
    {
        ProcessGuess(Input);
    }     
}

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::ProcessGuess(FString Guess)
{
    if (IsInputMatchingHiddenWord(Guess))
    {
        ClearScreen();

        PrintLine(TEXT("Correct! You Win!"));
        EndGame();

        return;
    }       

    SubstractLive(); 

    if (!HasLives())
    {
        ClearScreen();
                         
        PrintLine(TEXT("Game Over"));
        EndGame();

        return;
    }

    if (HasLives())
    {
        ClearScreen();

        PrintLine(TEXT("Wrong word"));

        // Asks if Input Len is not equal to Hiddenword Len.
        if (!IsInputLenEqualHiddenwordLen(Guess))
        {
            PrintLine(TEXT("The word has not %i characters!"), GetHiddenwordLen());
        }

        PrintLine(TEXT("Now you have %i lives."), Lives);

        return;
    } 
}

void UBullCowCartridge::EndGame()
{
    SetGameOver(true);
    PrintLine("\nPress 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();

	// Check the player guess.
	void ProcessGuess(FString Guess);

	// 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;
	
};

ProcessGuess() Function Ordered:

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (IsInputMatchingHiddenWord(Guess))
    {
        ClearScreen();

        PrintLine(TEXT("Correct! You Win!"));
        EndGame();

        return;
    }       

    SubstractLive(); 

    if (!HasLives())
    {
        ClearScreen();
                         
        PrintLine(TEXT("Game Over"));
        EndGame();

        return;
    }

    if (HasLives())
    {
        ClearScreen();

        PrintLine(TEXT("Wrong word"));

        // Asks if Input Len is not equal to Hiddenword Len.
        if (!IsInputLenEqualHiddenwordLen(Guess))
        {
            PrintLine(TEXT("The word has not %i characters!"), GetHiddenwordLen());
        }

        PrintLine(TEXT("Now you have %i lives."), Lives);

        return;
    } 
}

Very cool! this all compiles correctly? Thanks for sharing!

1 Like

Thank you! :slight_smile:
Yes, it compiles correctly!

1 Like

Privacy & Terms