Final Version of Bull & Cow

Hello there. This is my final codes for the Bull and Cow game. Quite a bit in loss in the section since I don’t have any prior coding knowledge, but thankfully I managed to make it into the end of the section. I enjoyed this course so far, and raring to see forward the next section. :slightly_smiling_face:

.cpp

// 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);
    
    SetupGame(); // Setting Up Game
}

void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{   
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else // 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; // Set Lifes
    bGameOver = false;

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

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("Press ENTER to play again."));
}

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

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

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

    // Remove Life
    PrintLine(TEXT("You have 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 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 Comparision = Index + 1; Comparision < Word.Len(); Comparision++)
        {
            if (Word[Index] == Word[Comparision])
            {
                return false;
            }  
        }
    }
    
    return true;
}

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

        for (FString Word : WordList)
        {
            if (Word.Len() >= 4 && Word.Len() <= 8 && 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;
}

HiddenList.h (I only included 5 Hidden Words since 1000 words are way to hard for some people…haha)

#pragma once
#include "CoreMinimal.h"

const TArray<FString> Words = 
{
    TEXT("dairy"), 
    TEXT("swine"),
    TEXT("calves"),
    TEXT("farms"),
    TEXT("meats"),
};

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>& WordList) const;
	FBullCowCount GetBullCows(const FString& Guess) const;

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

Privacy & Terms