BullCow game with references

#include "BullCowCartridge.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "ctime"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
    FFileHelper::LoadFileToStringArray(WordList, *WordListPath);

    PopulateWords();

    SetupGame();

    //PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); //Debug Line
}

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

    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else
    {
        CheckGuess(PlayerInput);
        if (Lives == 0)
        {
            PrintLine(TEXT("You have lost, the hidden word was %s"), *HiddenWord);
            EndGame();
        }        
    }
}

void UBullCowCartridge::SetupGame()
{
    srand(time(NULL));
    // do 
    // {
    //     HiddenWord = Words[rand() % Words.Num()];
    // }
    // while (HiddenWord.Len() < 4 || HiddenWord.Len() > 8 || !IsIsogram(HiddenWord));
    
    HiddenWord = Isograms[rand() % Isograms.Num()];
    Lives = HiddenWord.Len();
    bGameOver = false;
    bGameWon = false;
    bMulligan = true;

    PrintLine(TEXT("Welcome to the Bull Cow Game!"));
    PrintLine(TEXT("You have %i guesses for the secret isogram"), HiddenWord.Len());
    PrintLine(TEXT("*An isogram has no repeating characters*"));
    PrintLine(TEXT("Guess the %i letter word and press enter:"), HiddenWord.Len());
   
    // Debug
    PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord);
    PrintLine(TEXT("%i"), Isograms.Num());
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    if (bGameWon)
    {
        PrintLine(TEXT("You guessed correctly!!!"));
    }
    PrintLine(TEXT("\nPress enter to play again"));

    Isograms.Remove(HiddenWord);
    if (Isograms.Num() <= 0)
    {
        PopulateWords();
    }
}

void UBullCowCartridge::PopulateWords()
{
    for (FString Word : WordList)
    {
        if (Word.Len() >= 4 && Word.Len() <= 8 && IsIsogram(Word))
        {
            Isograms.Emplace(Word);
        }
    }
}

void UBullCowCartridge::CheckGuess(const FString& Guess)
{
    if (Guess == HiddenWord)
    {
        bGameWon = true;
        EndGame();
        return;
    }

    if (Guess == TEXT(""))
    {
        PrintLine(TEXT("Please type a guess and press enter")); 
        return;
    }

    if (Guess.Len() != HiddenWord.Len() || !IsIsogram(Guess))
    {
        PrintLine(TEXT("ERROR!\nGuess was not %i characters long"), HiddenWord.Len());
        PrintLine(TEXT("or has repeating characters"));
        if (bMulligan)
        {
            PrintLine(TEXT("This is your one mulligan")); 
            bMulligan = false;
        }
        else
        {
            if (--Lives > 0)
            {
                PrintLine(TEXT("Your guess was wrong"));
                PrintLine(TEXT("Guess again, %i guess(es) remaining"), Lives);
            }
        }
        return;
    }

    if (--Lives > 0)
    {
        PrintLine(TEXT("Your guess was wrong"));
        TArray<int32> BullCow = IsBullOrCow(Guess, HiddenWord);
        PrintLine(TEXT("You have %i Bulls and %i Cows"), BullCow[0], BullCow[1]);
        PrintLine(TEXT("Guess again, %i guess(es) remaining"), Lives);
    }
}

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
    for (int32 Index = 0; Index < Word.Len() - 1; Index++)
    {
        for (int32 Compare = Index + 1; Compare < Word.Len(); Compare++)
        {
            if (Word[Index] == Word[Compare])
            {
                return false;
            }
        }
    }
    return true;
}

TArray<int32> UBullCowCartridge::IsBullOrCow(const FString& Guess, const FString& Word) const
{
    TArray<int32> Counter = {0, 0};
    for (int32 Index = 0; Index < Guess.Len(); Index++)
    {
        if (Guess[Index] == Word[Index])
        {
            Counter[0]++;
        }
        for (int32 Compare = Index + 1; Compare < Guess.Len(); Compare++)
        {
            if (Guess[Index] == Word[Compare])
            {
                Counter[1]++;
            }
        }
        for (int32 Compare = Index - 1; Compare >= 0; Compare--)
        {
            if (Guess[Index] == Word[Compare])
            {
                Counter[1]++;
            }
        }
    }
    return Counter;
}

Here is my code so far. My get valid words function I chose not to make it a TArray like in the videos since I’ve called the list in other places and I thought it would be better as a member variable. I also made the Wordlist a member variable so that I could call it without passing it.

1 Like

Look at you doing stuff on your own! Congrats :clap:

Privacy & Terms