Completed Bull Cow game with Extra additions

Notes: I added the Hint() and UI() functions.
Added int32 HintCount member variable to header file to keep track of hints.

HInt() allows user to ask for up to two hints (first and last letter of hidden word)
UI() keeps track of lives and hints and displays info to user

BullCowCartridge.cpp :

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

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

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

void UBullCowCartridge::SetupGame()
{
    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];

    Lives = HiddenWord.Len() * 2; 
    HintCount = 2;
    bGameOver = false;

    // Print Welcome Message
    UI();
    //PrintLine(TEXT("The isogram is: %s"), *HiddenWord); //Debug Line

    PrintLine(TEXT("Welcome to Bulls and Cows!"));
    PrintLine(TEXT("Try to guess the %i letter Isogram.\n"), HiddenWord.Len());
    PrintLine(TEXT("Rules:"));
    PrintLine(TEXT("Bull = Correct letter in correct place"));
    PrintLine(TEXT("Cow = Correct letter in incorrect place"));
    PrintLine(TEXT("Type 'Hint' to receive a hint."));
    PrintLine(TEXT("Type your guess and press enter"));  
}

void UBullCowCartridge::EndGame()
{
    // ClearScreen();
    bGameOver = true;
    
    PrintLine(TEXT("Press Enter to play again"));
}

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

    if (Guess == "Hint" ) //|| Guess == "hint"
    {
        UI();
        ClearScreen();
        Hint(HintCount);
        // PrintLine(TEXT("The count is %i"), HintCount); // Debug Line
        return;
    }
    
    if (HiddenWord == Guess)
    {    
        ClearScreen();    
        UI();
        PrintLine(TEXT("You are correct! You Win!"));
        EndGame();
        return;
    }    

    if (Guess.Len() != HiddenWord.Len())
    {   
        ClearScreen();
        UI();
        PrintLine(TEXT("The isogram is %i characters long"), HiddenWord.Len());
        return;
    }

    FBullCowCount Score = GetBullCows(Guess);

     //Check IF Isogram
    if (!IsIsogram(Guess))
    {
        ClearScreen();
        UI();
        PrintLine(TEXT("No repeating letters, try again"));
        PrintLine(TEXT("The isogram is %i characters long"), HiddenWord.Len());
        return;
    }  

    ClearScreen();
    --Lives;
    UI();
    PrintLine(TEXT("Wrong, lost a life"));
    
    if (Lives <= 0)
    {
        ClearScreen();
        UI();
        PrintLine(TEXT("Game Over"));
        PrintLine(TEXT("The isogram was %s."), *HiddenWord);
        EndGame(); 
        return;  
    }    
    //UI();
    PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);
}

 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>& ValidWordList) const
{
      TArray<FString> ValidWords;

        for (FString Word : WordList)
        {
            if (IsIsogram(Word))
            {
            ValidWords.Emplace(Word);
            }
        }
        return ValidWords;    
}

FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
    FBullCowCount Count;    
    
    // For every Index of Guess is same as Index of Hiddenword, BullCount ++
    // If not Bull, was it Cow, if yes, CowCount ++

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

void UBullCowCartridge::Hint(int32& Count) const
{
    // Ask for a Hint Sytem
    //If user types "Hint", give user 1st hint
    //If user types "Hint" again, give user 2nd hint
    //If user types "Hint" again, let user know they are out of hints

    if (Count == 2)
    {   
        Count--;
        UI();
        PrintLine(TEXT("1st letter of isogram is %c"), HiddenWord[0]);
        PrintLine(TEXT("The isogram is %i characters long"), HiddenWord.Len());
        
        
        return;
    } 
    if (Count == 1)
    {
        Count--;
        UI();
        PrintLine(TEXT("Last letter of isogram is %c"), HiddenWord[HiddenWord.Len() - 1]);
        PrintLine(TEXT("The isogram is %i characters long"), HiddenWord.Len());
        return;
    } 
    if (Count == 0)
    {
        UI();
        PrintLine(TEXT("No More Hints Left"));
        PrintLine(TEXT("The isogram is %i characters long"), HiddenWord.Len());
        return;
    } 
}

void UBullCowCartridge::UI() const
{
    PrintLine(TEXT("Hints: %i                         Lives: %i\n"),HintCount, Lives);
}

BullCowCartridge.h :

// 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>& ValidWordList) const;
	FBullCowCount GetBullCows(const FString& Guess) const;
	void Hint( int32& Count) const;
	void UI() const;

	// Your declarations go below!
	private:

	FString HiddenWord;
	int32 Lives;
	bool bGameOver;
	TArray<FString> Isograms;
	int32 HintCount;
	
};

Screen Shot for Reference:

2 Likes

Very cool with the add hints function

1 Like

Privacy & Terms