Finished BullCowGame

Here is my finished code. I made a level system so that the game gives you longer words and less lives as you progress.

// 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();    

    Level = 0;

    SetUpGame();

}

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

    if (bGameOver){

        ClearScreen();
        SetUpGame();

    }
    else{

        ProcessGuess(Input);

    }

    

}



void UBullCowCartridge::SetUpGame()
{   
    PrintLine(TEXT("Welcome to the Bull Cow game!"));

    Level++;
    PrintLine(TEXT("Level: %i"), Level);

    Isograms = GetValidWords(Words);

    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num()-1)];
    bGameOver = false;
    Lives = HiddenWord.Len() + (8 - Level);
    

    IsIsogram(HiddenWord);

    
    //PrintLine((TEXT("The Hidden word is: %s"), *HiddenWord)); //Debug line
    //PrintLine(FString::Printf(TEXT("\nThe Hidden word is %i characters long."), HiddenWord.Len()));
    PrintLine(TEXT("Guess a %i letter word!"), HiddenWord.Len());
    PrintLine(TEXT("You have %i guesses!"), Lives);

    //PrintLine(FString::Printf(TEXT("The Hidden word is: %s"), *HiddenWord)); //debug

    // const TCHAR HW[] = TEXT("plums");
    // const char HW[] = "cake":
    // HW;






}

void UBullCowCartridge::EndGame()
{

    bGameOver = true;

    PrintLine(TEXT("Press Enter to play again."));

}

void UBullCowCartridge::ProcessGuess(const FString& Guess)
{

    if(HiddenWord == Guess){
        PrintLine(TEXT("You win!"));
        if(Level == 8){

            PrintLine("Congrats! You beat the game!");

            Level = 0;

            EndGame();

            return;

        }
        else{

            EndGame();

            return;

        }
    }

    if(Guess.Len() != HiddenWord.Len()){

        PrintLine(TEXT("The Hidden Word is %i letters long."), HiddenWord.Len());
        return;

    }

    if(!IsIsogram(Guess)){

        PrintLine(TEXT("That is not an isogram!"));
        return;

    }

    else{

        FBullCowCount Score = GetBullCows(Guess);

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

        PrintLine(TEXT("You have %i lives left!"), --Lives);

        if(Lives == 0){

            PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);

            Level = 0;

            EndGame();
        }

        return;
        

    }

    

}

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
    for(int32 Index = 0; Index < Word.Len()-1; Index++){

        for(int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++){

            if(Word[Comparison] == Word[Index]){

                return false;

            }

        }

    }

    return true;
}

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

    TArray<FString> ValidWords;

    if(Level <= 2){

        for(FString i : WordList){

            if(i.Len() >= 3 && i.Len() <= 4 && IsIsogram(i)){

                ValidWords.Emplace(i);

            }

        }

    }
    else if(Level <= 4){

        for(FString i : WordList){

            if(i.Len() >= 4 && i.Len() <= 6 && IsIsogram(i)){

                ValidWords.Emplace(i);

            }

        }

    }
    else if(Level <= 6){

        for(FString i : WordList){

            if(i.Len() >= 4 && i.Len() <= 8 && IsIsogram(i)){

                ValidWords.Emplace(i);

            }

        }

    }
    else{

        for(FString i : WordList){

            if(i.Len() >= 8 && IsIsogram(i)){

                ValidWords.Emplace(i);

            }

        }

    }

    

    // for (int32 i = 0; i < ValidWords.Num(); i++){

    //     PrintLine(TEXT("%s."), *ValidWords[i]);

    // }

    //PrintLine(TEXT("The number of possible words: %i"), Words.Num());

   

    return ValidWords;

}

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

    FBullCowCount Count;

    for (int32 i = 0; i < Guess.Len(); i++)
    {

        if (Guess[i] == HiddenWord[i])
        {
            Count.Bulls++;
            continue;
        }

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

    return Count;


    

}
2 Likes

Congrats! :partying_face:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms