Added levels and get Extra chance on losing

Added some new features to the game:

1. Created 5 Level game just like the TripleX game in last section

As you go through the levels the number of letters will also increase, thus increasing difficulty.

2. Added feature to show the bulls after the guess to make the game easier

3. Added Extra Chances for the same level and Word would not change for that particular level
When you loose all your lives the game would check if you have extra chances. If you have the chances then game will ask you to use the extra chance upon loosing the level… This will replay the same level and Guess WHAT- The word would also remain the same

Here it will ask for the input if you type “yes” then it would restart from the same level again …else typing “No” will prompt to end the game and will ask to play the game again or quit the game

While I adding new features to this game… At first I messed up my whole of the code. I was way too confused on how to create new functions or use bools to my advantage and many more things… But then I took my time and kept on trying different things to create these features…
Also My doubts were cleared at many points by the instructors and the community.

Will be tiding up the look of the game if I get some time.

Here is the code:
(its very messed up though…might also arrange this sooner or later)

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"


void UBullCowCartridge::BeginPlay() // When the game starts
{ 
    Super::BeginPlay();

    const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
    FFileHelper::LoadFileToStringArray(Words, *WordListPath);
    //...
    GameIntro();
    Level = 1;
    SecondChance = 2;
    PrintLine(TEXT("\nLevel %i"), Level);
    
    // // GetValidWords(Words);
    // PrintLine(TEXT("Number of Words are: %i"), Words.Num());
    // PrintLine(TEXT("Number of Valid Words are: %i"), GetValidWords(Words).Num());
    SetupGame();
}


void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter

{      

    if (bGameOver)
    {
        ClearScreen();
        PrintLine(TEXT("\nLevel %i"), Level);        
        SetupGame();     
    }

    else if (bAnotherChance)
    {
        AnotherChance(PlayerInput);

        if (bChanceYes)
        {
            SecondChance -= 1;
            SameWordSetup();
            return;
        }
        
        if (bChanceNo)
        {
            SecondChance = 2;
            Level = 1;
            EndGame();
            return;
        }
        
    }    
    
    else
    {
        ProcessGuess(PlayerInput);
    }    
}

void UBullCowCartridge::GameIntro()
{
    PrintLine(TEXT("Hello There, Welcome to the BULL COW game"));
}

void UBullCowCartridge::SetupGame()
{   
    Isograms = GetValidWords(Words);
    

    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num()-1)]; //Hidden Word
    Lives = 10; //HiddenWord.Len() + 3;
    bGameOver = false;
    bAnotherChance = false;
    
    PrintLine(TEXT("The Hidden Word is: %s"), *HiddenWord);     
    PrintLine(TEXT("Guess the %i letter word and then \npress ENTER to continue..."), HiddenWord.Len());  //prompt for guess
    PrintLine(TEXT("Lives Left: %i"), Lives);
}


void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("Press ENTER to Play Again.\nPress ESCAPE to Quit the Game"));
}


void UBullCowCartridge::ProcessGuess(const FString& Guess)
{   
    int32 MaxLevel=5;        

        if (Guess == HiddenWord) 
        {        

            if(Level >= MaxLevel)
            {
                PrintLine(TEXT("You Win The Game"));
                Level = 1;
                EndGame();                
                return;
            }

            else
            {
                PrintLine(TEXT("Level Cleared")); //checking input                
                
                Level += 1; //declared in header file
                // PrintLine(TEXT("2: Level == %i"), Level);
                PrintLine(TEXT("\nLevel %i"), Level);
                SetupGame();             
                return;
            }
            
        }
        else
        {
            CheckGuess(Guess);
            return;
        }               
}


bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
    for (int32 i=0; i<=Word.Len()-1; i++)
    {
        TCHAR CheckChar = Word[i];

        for (int32 f= i+1; f<=Word.Len()-1;f++) 
        {
            if (CheckChar == Word[f])
            {
                return false;               
            }        
        }    
    } 
return true;   
}


TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList)
{   
    
    TArray<FString> ValidWords; 
    // PrintLine(TEXT("Number of words is: %i"), Words.Num());
    // PrintLine(TEXT("GetValidWords level is: %i"), Level);

    for (FString Word : WordList)
    {
        if (Word.Len() == Level + 2 && IsIsogram(Word))
        {
            ValidWords.Emplace(Word);
        }
    } 
    return ValidWords;   

    // for (int32 i = 0; i < ValidWords.Num(); i++)
    // {
    //     PrintLine(TEXT("%i. %s"), i+1,*ValidWords[i]);
    // }
}

FBullCowCount UBullCowCartridge::GetBullCow(const FString& Guess) const
{
    FBullCowCount Count;
    FString Display = FString::ChrN(HiddenWord.Len(), TEXT('_'));    

    for (int32 i = 0; i < Guess.Len(); i++)
    {
        if (Guess[i] == HiddenWord[i])
        {
            Count.Bulls++;
            Display[i] = HiddenWord[i];            
            continue;
        }

        for (int32 f = 0; f < Guess.Len(); f++)
        {
            if (Guess[i] == HiddenWord[f])
            {
                Count.Cows++;
                break;
            }            
        }    
    }
    if (Count.Bulls > 0)
    {
        PrintLine(TEXT(" %s"), *Display); 
    }       
    return Count;    
}

void UBullCowCartridge::CheckGuess(const FString& Guess)
{
    if (Guess.Len() != HiddenWord.Len()) 
        {
            PrintLine(TEXT("Please, Enter %i letter word"), HiddenWord.Len()); //check for number of characters
            return;
        }
            
        --Lives;
                

        if (!IsIsogram(Guess)) //check for isogram
        {
            PrintLine(TEXT("Oops, you have repeated letters in your guess"));
            PrintLine(TEXT("You need to write an Isogram")); 
        }

        if (Lives <= 0)  //check for lives > 0
        {
            
            if (SecondChance == 1 || SecondChance == 2)
            {
                PrintLine(TEXT("You have lost all lives"));
                PrintLine(TEXT("You still have %i chances left. Would you like to play this level again?\n Type \"yes\" to play again.\n Type \"no\" to end this game"), SecondChance);
                bAnotherChance = true;
                 
            }
            else
            {
                PrintLine(TEXT("You have lost all lives"));
                PrintLine(TEXT("Hidden Word was: %s"), *HiddenWord);
                Level = 1;
                SecondChance = 2;
                EndGame();
            }       
        }
        else
        {
            FBullCowCount Score = GetBullCow(Guess);

            PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);
            PrintLine(TEXT("You have lost a life"));
            PrintLine(TEXT("Guess Again. \nLives Left: %i"), Lives);
        }
}

void UBullCowCartridge:: AnotherChance(const FString& Input)
{
    if (Input == "yes")
    {
        AnotherChanceYes();
    }
    else if (Input == "no")
    {
        AnotherChanceNo();
    }
    else
    {
        PrintLine(TEXT("Please enter appropriate choice"));
        bAnotherChance = true;
    }   
}

void UBullCowCartridge:: AnotherChanceYes()
{
    bChanceYes = true;
    bChanceNo = false;
}

void UBullCowCartridge:: AnotherChanceNo()
{
    bChanceNo = true;
    bChanceYes = false;
}

void UBullCowCartridge:: SameWordSetup()
{
    HiddenWord; //Hidden Word
    Lives = 10; //HiddenWord.Len() + 3;
    bGameOver = false;
    bAnotherChance = false;
    
    PrintLine(TEXT("You got another chance"));
    PrintLine(TEXT("The Hidden Word is: %s"), *HiddenWord);     
    PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());  //prompt for guess
    PrintLine(TEXT("Lives Left: %i"), Lives);
    PrintLine(TEXT("Guess the same word Again"));

}

Here is the header file code:

// Fill out your copyright notice in the Description page of Project Settings.



#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

struct FBullCowCount 
{
	int32 Bulls = 0;
	int32 Cows = 0;
};

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 GameIntro();
	void SetupGame();
	void EndGame();
	void ProcessGuess(const FString& Guess);
	void CheckGuess(const FString& Guess);
	bool IsIsogram(const FString& Word) const;
	TArray<FString> GetValidWords(const TArray<FString>& WordList);
	FBullCowCount GetBullCow (const FString& Guess) const;
	void LevelDifficulty();
	void AnotherChance(const FString& Input);
	void AnotherChanceYes();
	void AnotherChanceNo();
	void SameWordSetup();
	// void Lives(Levels);
	

	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Level;
	int32 Lives;
	bool bGameOver;
	bool bAnotherChance;
	bool bChanceYes;
	bool bChanceNo;
	TArray<FString> Isograms;
	TArray<FString> Words;
	int32 SecondChance;
};

Feel free to correct me at any point and also point out if you find any mistake in this code
Thanks

2 Likes

Code looks good! Great idea adding levels! :clap:

1 Like

Nicely done! :+1:

1 Like

Glad you made it far

1 Like

Privacy & Terms