Bulls & Cows

UE4 In-Game Screenshot

Bulls and Cows is a Terminal Game built in Unreal Engine 4 as part of the Udemy Unreal Engine C++ Developer Course by Gamedev.tv. This course taught me the basics of C++ in Unreal Engine.

The game’s objective is to guess the Hidden Word. The player wins if you guess the exact Hidden Word. If the player guesses incorrectly, the player loses a life. Once the number of lives run out, the player loses.

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

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

    else //check PlayerGuess
    {
        ProcessGuess(PlayerInput);
    }       
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
 
    PrintLine (TEXT("\nPress Enter to Play Again"));
}

Once the player wins or loses, the game ends and the game is restarted.

The Bulls are characters, ‘E’ and ‘A’. The Cows are characters ‘M’ and ‘T’.

The number of bulls is determined by the number of matching characters seated at the exact spot of the hidden word.

The number of cows is determined by the number of remaining matching characters regardless of where the character is seated in the word.

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
 if (Guess == HiddenWord)
        {
            PrintLine (TEXT("You Win!"));
            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))
    {
        PrintLine(TEXT("No repeating letters, guess again!"));
        return;
    }
    
    // Remove Life 
     PrintLine(TEXT("You lost a life!"));
     --Lives;  
     if (Lives <= 0)
             {
                ClearScreen();
                PrintLine(TEXT("You have no lives left!"));
                PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
                EndGame();
                return;
             }
    
    //Show players 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);
}

Every Hidden Word is an isogram. The number of lives is determined by the number of characters in the Hidden Word.

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

The Hidden Word is selected by random off an array in a header file I had set up.

void UBullCowCartridge::SetupGame()
{
   //Welcoming the player
    PrintLine (TEXT("Hello, Welcome to BullCows!"));
       
    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() -1)];
    Lives = HiddenWord.Len();
    bGameOver = false;
   
    PrintLine (TEXT("Guess the %i letter word!"), HiddenWord.Len());
    PrintLine(TEXT("The Hidden Word is %s.") , *HiddenWord); //Debug Line
    PrintLine (TEXT("You have %i lives.") , Lives);
    PrintLine (TEXT("Type in your guess and\nPress Enter to Continue..."));//Prompt Player for Guess
}

The Player’s input is compared character by character with the Hidden Word and validated. The number of bulls and cows are checked and calculated.

TArray<FString> UBullCowCartridge :: GetValidWords(const TArray<FString>& WordList)const
{
TArray<FString> ValidWords ;

for (FString Word : WordList)
{
    if (Word.Len() >= 4 && Word.Len() <= 8)
    {
        if (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 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
            {
               Count.Cows++;
               break;
            }
        } 
    } 
   return Count;   
}

Leave a comment

Design a site like this with WordPress.com
Get started