Final Cow and Bull agme and new features

Hi all

I have now finished my cow and bull agme and added a few more extra features.

My game has a main menu.

From that menu you can change the difficulty of the game (select how many letters the hidden word will have) and also increase and decease the number of gussess allowed.

Also I have aded a extra feature called Marker tips. This is a toggle on and off option. When on it will show the player the poistion of cows and bull;s in the game making ti alot easier to guess the word.

I have also added extra features during play like indicating to the player that this is there last chance to get the word right.

Also another hidden feature is that you can return to the main menu when trying to guess the word by just typing the word “end”.

I not sure how to actually send the code so others can try my game??

So hopefully I wil just paste the .h and cpp’s code below??

If theres a betetr way to share please lets me knows.

Thanks

BullCowCartridges.h

#pragma once

#include “CoreMinimal.h”

#include “Console/Cartridge.h”

#include “BullCowCartridge.generated.h”

struct FBullCowCount

{

int32 Bulls = 0;

int32 Cows = 0;

FString BullsCowsMarker = "";

};

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

void PrintLivesLeft();

void GameOver();

void MainMenu(const FString& Input);

void ProcessGuess(const FString& GuessedWord);

void PrintWelcomeMessage(const FString& Message);

TArray<FString> GetValidWords(const TArray<FString>& WordList) const;

bool IsIsoGram(const FString& CheckWord) const;

FBullCowCount NumberOfBullsCows(const FString& Checkword, const FString& HiddenW) const;

//int NumberOfCows(const FString& Checkword, const FString& HiddenW) const;

bool GuessedValid;

int32 GameState;

int32 HiddenWordCharactersLength = 4;

int32 NumberGuesses = 5;

TArray<FString> IsoGrams;

// Your declarations go below!

private:

FString HiddenWord;

FString GameMessage;

int32 Lives;

int32 HiddenWordLength;

int32 WordLength;

bool bGameOver;

bool bGameWon;

bool bMarkersOn;

};

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

SetUpStartGameVariables();

NumberGuesses = 15;

GameState = 0;

HiddenWordCharactersLength = 5;

bMarkersOn = true;

// FBullCowCount Score;

PrintLine(TEXT("Bull And Cow Word Guessing  Game!"));

PrintLine(TEXT("\nTo start click onto the billboard."));

PrintLine(TEXT("To activate the terminal press \"Tab Key\""));

PrintLine(TEXT("then press the \"Enter Key\""));

}

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

{

if(GameState == 0)

{

    MainMenu(Input);

}

if(GameState == 1)

{

    if(Input == "End") { GameState = 0; }

    FString TypedWord = Input.ToLower();

    if(Lives > 0 && !bGameWon && !bGameOver )

    {

        ProcessGuess(TypedWord);

    }

    else

    {

        if(TypedWord != "y" && bGameOver)

        {

            FString message1 = FString::Printf(TEXT("\nYou Lost the game\nword was \"%s\"\nPress \"enter\" to go to main menu"), *HiddenWord); 

            GameMessage = message1;

        }

        

        if(TypedWord != "y" && bGameWon)

        {

            GameMessage = TEXT("\nYou have guessed the right word!\nPress \"enter\" to go to main menu");

        }

    }

    ClearScreen();

    PrintWelcomeMessage(GameMessage);

    //PrintLine(FString::Printf(TEXT(" hiddenword %s"),*HiddenWord));

    if(GuessedValid && !bGameOver && !bGameWon && TypedWord.Len() >= HiddenWord.Len())

    {

        FBullCowCount Score = NumberOfBullsCows(TypedWord, HiddenWord);

            PrintLine(FString::Printf(TEXT("Word typed   \"%s\""), *Input)); 

            if(bMarkersOn)

            {

                PrintLine(FString::Printf(TEXT("Bulls* CoWs- \"%s\""), *Score.BullsCowsMarker));

            }

            PrintLine(FString::Printf(TEXT("\nBulls: %i CoWs: %i\n"), Score.Bulls, Score.Cows)); 

    }

    if(!bGameOver && !bGameWon) { PrintLivesLeft(); }

}

}

void UBullCowCartridge::SetUpStartGameVariables()

{

Lives = NumberGuesses; 

bGameOver = false;

bGameWon = false;

bool bPickedWord = false;

do

{

    int32 Index = FMath::RandRange(0, IsoGrams.Num()-1);

    HiddenWord = IsoGrams[Index];

        

    HiddenWordLength = HiddenWord.Len();

    if(HiddenWordLength == HiddenWordCharactersLength) { bPickedWord = true; }

} while(!bPickedWord);

}

void UBullCowCartridge::PrintLivesLeft()

{

if(Lives == 1)

{

    PrintLine("This is your last guess. so guess right!"); 

}

else

{

    FString Message = FString::Printf(TEXT("You have got %i Lives Left"), Lives);

    PrintLine(Message);    

}

}

void UBullCowCartridge::PrintWelcomeMessage(const FString& Message2)

{

PrintLine(TEXT("Welcome to the cow bull game!"));

FString Message1 = FString::Printf(TEXT("The word has %i characters."), HiddenWordLength); 

PrintLine(Message1);

PrintLine(Message2);

}

void UBullCowCartridge::ProcessGuess(const FString& GuessedWord)

{

    GuessedValid = true;

    int32 GuessedWordLength = GuessedWord.Len();

    if(GuessedWordLength == HiddenWordLength)

    {

            if(HiddenWord == GuessedWord)

            {

                //guess right

                GameMessage = TEXT("\nYou have guessed the right word!\n\nPress \"enter\" to go to main menu");

                bGameWon = true;  

                GameState = 0;

                return;         

            }

           if(!IsIsoGram(GuessedWord))

           {

                GameMessage = TEXT("\nYou did not type a Isogram");   

                GuessedValid = false;

                return;

            }

                //wrong guess

                GameMessage = TEXT("\nWrong Word Sorry");

                 --Lives;

                 

                 if(Lives == 0)

                 {

                    bGameOver = true;

                    FString message1 = FString::Printf(TEXT("\nYou Lost the game\nword was \"%s\"\nPress \"enter\" to go to main menu"), *HiddenWord); 

                    GameMessage = message1;

                    GameState = 0;

                    return;

                 }

    }

    else

    {

            FString Message = FString::Printf(TEXT("\nYou did not type %i characters"), HiddenWordLength);

            GuessedValid = false;

            GameMessage = Message;

            return;

    }

}

TArray UBullCowCartridge::GetValidWords(const TArray& WordList) const

{

TArray<FString> ValidWords;

for(FString Word : WordList)

{

    if(Word.Len() >= 4 && Word.Len() <= 8 && IsIsoGram(Word))

    {

        ValidWords.Emplace(Word);

    }

}

return ValidWords;

}

bool UBullCowCartridge::IsIsoGram(const FString& CheckWord) const

{

int32 WordLength = CheckWord.Len();

for(int32 Z = 0; Z < WordLength - 1; Z++)

{

     for(int32 B = Z+1; B < WordLength; B++)

     {

            if(CheckWord[Z] == CheckWord[B])

            {

                return false;

            }

     }

}

return true;

}

FBullCowCount UBullCowCartridge::NumberOfBullsCows(const FString& CheckWord, const FString& HiddenW) const

{

FBullCowCount Count;

bool bChanged;

int32 WordLength = CheckWord.Len();

for(int32 Z = 0; Z < WordLength; Z++)

{

    if(CheckWord[Z] == HiddenW[Z])

    {

     Count.Bulls++;   

     Count.BullsCowsMarker = Count.BullsCowsMarker + "*";

     continue;

    }

        

    for(int32 B = 0; B < WordLength; B++)

    {

        bChanged = false;

        if(CheckWord[Z] == HiddenW[B])

        {

        Count.Cows++; 

        Count.BullsCowsMarker = Count.BullsCowsMarker + "-";

        bChanged = true;

        break;   

        }                    

    }

    if(!bChanged)

    {

        Count.BullsCowsMarker = Count.BullsCowsMarker + " ";

    }

    

}

return Count;

}

/*

int UBullCowCartridge::NumberOfCows(const FString& CheckWord, const FString& HiddenW) const

{

int32 WordLength = CheckWord.Len();

int32 Counter = 0;

for(int32 Z = 0; Z < WordLength; Z++)

{

    for(int32 B = 0; B < WordLength; B++)

    {

            if(B != Z)

            {

                if(CheckWord[Z] == HiddenW[B])

                {

                 Counter++; 

                 break;   

                }                   

            }

    }

}

return Counter;

}

*/

void UBullCowCartridge::MainMenu(const FString& InputPressed)

{

ClearScreen();

PrintLine(TEXT("Main Menu!"));

PrintLine(TEXT("\"1\" To Start Game and press \"Enter\""));

PrintLine(TEXT("\"2\" To change difficulty and press \"Enter\""));



if(InputPressed == "1")

{

    SetUpStartGameVariables();

    GameState = 1;

}

if(InputPressed == "2")

{

    HiddenWordCharactersLength++;

    if(HiddenWordCharactersLength > 8)

    {

        HiddenWordCharactersLength = 4;

    }

    

    switch(HiddenWordCharactersLength)

    {

        case 4:

            NumberGuesses = 10;

        break;

        case 5:

            NumberGuesses = 15;

        break;

        case 6:

            NumberGuesses = 20;

        break;

        case 7:

            NumberGuesses = 25;

        break;    

        case 8:

            NumberGuesses = 30;

        break;                 

    }

}

if(InputPressed == "3")

{

    if(NumberGuesses <= 50)

    {

    NumberGuesses+=5;

    }

}

if(InputPressed == "4")

{

    if(NumberGuesses >= 5)

    {

        NumberGuesses -=5;

    }

}

if(InputPressed == "5")

{

    if(bMarkersOn)

    {

        bMarkersOn = false;

    }

    else

    {

        bMarkersOn = true;

    }

}

if(NumberGuesses < 50)

{

    PrintLine(TEXT("\"3\" to increase quesses and press \"Enter\""));

}



if(NumberGuesses >= 6)

{

    PrintLine(TEXT("\"4\" to decease quesses and press \"Enter\""));

}



PrintLine(TEXT("\"5\" to toggle Marker ON and OFF"));

PrintLine(FString::Printf(TEXT("\nHidden Word Length %i"), HiddenWordCharactersLength));

PrintLine(FString::Printf(TEXT("Number of guessess %i"), NumberGuesses));

if(bMarkersOn)

{

    PrintLine(TEXT("Tip marker is ON"));

}

else

{

    PrintLine(TEXT("Tip marker is OFF"));

}

}