Bull cow with difficulty and rules

I added to features

  1. If the user wants to the rules of the game then he can Press “r”.
  2. Setting difficulty as
    Novice, Amateur, veteran.

One thing that I thought was to Optimize the word list file by not adding the non -isogram word as by this we have to remove them from the list by calling the ISISOGRAM function for 1000 words which will increase the compile time.

Code is below
CPP file -
#include “BullCowCartridge.h”
#include “WordList.h”

void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
PrintLine(TEXT(“Welcome to Bulls and Cows game”));
PrintLine(TEXT(“Enter you choice:\n\n1.Press ‘R’ to see the rules of the game.”));
PrintLine(TEXT("\n2.Or set the difficulty to enter the game."));
PrintLine(TEXT(“Press ‘A’ for Novice”));
PrintLine(TEXT(“Press ‘B’ for Amateur”));
PrintLine(TEXT(“Press ‘C’ for Veteran”));
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (Input == “R”)
{
PrintLine(TEXT(“1.You need to press ‘Tab’ to access the terminal.”));
PrintLine(TEXT("\n2.If the aplhabet you entered is correct and is at right position then it is BULL."));
PrintLine(TEXT("\n3.If the aplhabet you entered is correct but is at wrong position then it is COW."));
bRules = true;
if (!bSetDiff)
{
PrintLine(TEXT("\nNow please set the difficulty."));
}
}
else if (bGameOver || bRules && Input == “A” || Input == “B” || Input == “C”)
{
bSetDiff = true;
Difficulty = Input;
ClearScreen();
SetupGame(Difficulty);
}
else
{
ProcessGuess(Input);
}
}

void UBullCowCartridge::SetupGame(const FString& Difficulty) //beginning of the game
{
ClearScreen();

Isograms = GetValidWords(HiddenWordList);

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

if (Difficulty == "A")
{
	Lives = HiddenWord.Len();
}
else if (Difficulty == "B")
{
	Lives = HiddenWord.Len() /2;
}
else 
{
	Lives = 3;
}

bGameOver = false;

//PrintLine(TEXT("diff is %s"), *Difficulty);
//PrintLine(TEXT("%s"), *HiddenWord);//debug line
//PrintLine(TEXT("%i"), Lives);//debug line
PrintLine(TEXT("OK! So let's begin the game."));
PrintLine(TEXT("Try to guess the word."));
PrintLine(TEXT("The length of the word to be guessed is %i "), HiddenWord.Len());
PrintLine(TEXT("Lives left: %i"), Lives);

}

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

void UBullCowCartridge::ProcessGuess(const FString& Input) // Def of the game input
{

if (Input == HiddenWord)
{
	PrintLine(TEXT("The entered word is correct"));
	EndGame();
	return; // early return
}

//Check for correct length
if (HiddenWord.Len() != Input.Len())
{
	PrintLine(TEXT("The Length of Hidden Word is %i"), HiddenWord.Len()); //Magic Word
	PrintLine(TEXT("Lives left:%i"), --Lives);
	return;
}


if (IsIsogram(Input)== false)
{
	PrintLine(TEXT("No repeating alphabets in the words"));
	PrintLine(TEXT("Lives left:%i"), --Lives);
	return;
}

PrintLine(TEXT("Lives left:%i"), --Lives);


if (Lives <= 0)
{
	PrintLine(TEXT("You have entered a wrong word!!"));
	PrintLine(TEXT("\nThe Hidden word was %s"), *HiddenWord);
	EndGame();
	return;
}

int32 Bulls, Cows;
GetBullsCows(Input, Bulls, Cows);
PrintLine(TEXT("Bull Count %i and Cow Count %i"), Bulls, Cows);

}

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

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

for (FString Word : WordList)
{
	if (Word.Len() >= 2 && Word.Len() <= 10)  // add the logic for isogram
	{
		ValidWords.Emplace(Word);
	}
}
return ValidWords;

}

void UBullCowCartridge:: GetBullsCows(const FString& Guess, int32& Bulls, int32& Cows)
{
Bulls = 0;
Cows = 0;
for (int32 i = 0; i<Guess.Len(); i++)
{
if (Guess[i] == HiddenWord[i])
{
Bulls++;
continue;
}
for (int32 j = 0; j < HiddenWord.Len(); j++)
{
if (Guess[i] == HiddenWord[j])
{
Cows++;
}
}
}
}

Header File-
// 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”

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(const FString&);
void EndGame();
void ProcessGuess(const FString&);
bool IsIsogram(const FString&) const;
void GetBullsCows(const FString&, int32&, int32&); // out parameter as no const in them int32&, int32& parameter which is a reference but not a const para is Out parameter
TArray GetValidWords(const TArray&) const;
// Your declarations go below!
private:
FString HiddenWord;
FString ValidWords;
int32 Lives;
TArray Isograms;
bool bGameOver = true;
bool bRules = false;
bool bSetDiff = false;
FString Difficulty;
};

1 Like

Awesome job for adding all these incredible features!

Privacy & Terms