My Isogram code so far

Following the pseudocode, I tried to code the Isogram.
And this is the result as far as I could achieve:

Isogram Function:

bool UBullCowCartridge::IsIsogram(FString word)
{
    for (int i = 0; i < word.Len() - 1; i++)
    {
        if (word.IsValidIndex(i + 1)) // Make sure that next index is not out of the array
        {
            if (word[i] == word[i + 1])
            {
                return true;                    
            }
        }       
    }

    return false;
}

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

        if (IsIsogram(Guess))
        {
            PrintLine(TEXT("This word has repeating letters!"));                
        }

        // 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::IsIsogram(FString word)
{
    for (int i = 0; i < word.Len() - 1; i++)
    {
        if (word.IsValidIndex(i + 1)) // Make sure that next index is not out of the array!
        {
            if (word[i] == word[i + 1])
            {
                return true;                    
            }
        }       
    }

    return false;
}

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 word repeating letters?
	bool IsIsogram(FString word);

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

3 Likes

Looking good! Awesome job :clap:

1 Like

Thank you very much, Kevin! :grinning_face_with_smiling_eyes:

Privacy & Terms