Bull Cow Extra Feature (level up with difficulty increase)

Added a level up feature with added difficulty up to level 5. Took a bit to figure out how it should work, but this seemed to turn out alright after a few changes.

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
// #include "Math/UnrealMathUtility.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    Isograms = GetValidWords(Words);
    Level = 0;
    SetupGame();  
}

void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{ 
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else // Checking PlayerGuess
    {
        ProcessGuess(PlayerInput);
    }
}

void UBullCowCartridge::SetupGame()
{
    // Welcoming The Player

    PrintLine(TEXT("Welcome to Bull Cows!"));
    
    
    HiddenWord = GetValidWords(Words)[FMath::RandRange(0, GetValidWords(Words).Num() - 1)];
    Lives = HiddenWord.Len()*2;
    bGameOver = false;

    PrintLine(TEXT("Level: %i"), Level);
    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
    PrintLine(TEXT("You have %i lives."), Lives);
    PrintLine(TEXT("Type in your guess and \npress enter to continue...")); // Prompt Player For Guess
    // PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); //Debug Line  
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("\nPress enter to play again."));
}

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You have Won!"));
        LevelUp();
        EndGame();
        return;
    }

    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());
        PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining."), Lives);
        return;
    }

    // Check If Isogram
    if (!IsIsogram(Guess))
    {
        /* code */
        PrintLine(TEXT("No repeating letters, guess again!"));
        return;
    }

    // Remove Life
    PrintLine(TEXT("Lost a life!"));
    --Lives;

    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left!"));
        PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
        Level = 0;
        EndGame();
        return;
    }

    // Show the player Bulls and Cows
    FBullCowCount Score = GetBullCows(Guess);

    PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);

    PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{

    for (int32 Index = 0; Index < Word.Len(); Index++)
    {
        for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
        {
            if (Word[Index] == Word[Comparison])
            {
                return false;
            }
        }   
    }

    return true;
}

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
{
    TArray<FString> ValidWords;
    
    for (FString Word : WordList)
    {
        if (Word.Len() == 4 + Level && IsIsogram(Word))
        {
                ValidWords.Emplace(Word);      
        }
    }
    return ValidWords;
}

FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
    FBullCowCount Count;

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

        for (int32 HiddenWordIndex = 0; HiddenWordIndex < HiddenWord.Len(); HiddenWordIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenWordIndex])
            {
                Count.Cows++;
                break;
            }
            
        }   
    }
    return Count;
}

void UBullCowCartridge::LevelUp()
{
    ClearScreen();
    Level++;

    if (Level >= 6)
    {
        PrintLine(TEXT("FINAL LEVEL COMPLETE! \nYou have beaten the game! \nAll the bulls and cows are yours now!"));
        Level = 0;
        return;
    }

    PrintLine(TEXT("Level Up! \nLevel: %i"),Level);
    
    if (Level == 5)
    {
    PrintLine(TEXT("Final Level!"));
    }
}
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#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 SetupGame();
	void EndGame();
	void ProcessGuess(const FString& Guess);
	bool IsIsogram(const FString& Word) const;
	TArray<FString> GetValidWords(const TArray<FString>& WordList) const;
	FBullCowCount GetBullCows(const FString& Guess) const;
	void LevelUp();

	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives;
	bool bGameOver;
	TArray<FString> Isograms;
	int32 Level;
};
2 Likes

How did you figure it out?

1 Like

The main thing I needed to do was add a level variable and replace the GetValidWords() functions second if statement to use 4 + Level for word length in place of the original between 4 and 8 characters check when deciding what words to .emplace. Then I just added a LevelUp() function to handle when the level should increase and to display any extra text I wanted the player to see, along with ending the game and resetting the level if they get above level 5 (For anyone trying to do this you need to have a maximum level or the game with crash when it tries to use a word with more characters than any posses). – The LevelUp() function runs during the beginning of ProccessGuess() during if (Guess == HiddenWord) check.

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
{
    TArray<FString> ValidWords;
    
    for (FString Word : WordList)
    {
        if (Word.Len() == 4 + Level && IsIsogram(Word))
        {
                ValidWords.Emplace(Word);      
        }
    }
    return ValidWords;
}

void UBullCowCartridge::LevelUp()
{
ClearScreen();
Level++;

if (Level >= 6)
{
    PrintLine(TEXT("FINAL LEVEL COMPLETE! \nYou have beaten the game! \nAll the bulls and cows are yours now!"));
    Level = 0;
    return;
}

PrintLine(TEXT("Level Up! \nLevel: %i"),Level);

if (Level == 5)
{
PrintLine(TEXT("Final Level!"));
}

}

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You have Won!"));
        LevelUp();
        EndGame();
        return;
    }

1 Like

Privacy & Terms