I modified the game with a hint to a hidden word but it does not work
(1.cpp file
// 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 = GetValidWord(Words);
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
if (bGameOver) //If the game is over then ClearScreen() and Setupgame().
{
ClearScreen();
SetupGame();
}
else//If the game isn't over then check the player guess
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetupGame()
{
//Welcoming the player.
PrintLine(TEXT("Welcome to Bulls and Cows!"));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Set the hidden word.
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("You have only %i lives."), Lives);
PrintLine(TEXT("Type in your guess and\npress enter to continue....."));
Random = FMath::RandRange(MixingLettersone, MixingLetterstwo, MixingLettersthree, MixingLettersfour);
PrintLine(TEXT("This is a hint for the hidden word(Letters have been mixed.):%s"), *Random);
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press enter to play again."));
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (HiddenWord == Guess)//Cheking the player guess.
{
PrintLine(TEXT("Your answer is correct!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Sorry,the word has %i letters."), HiddenWord.Len());
PrintLine(TEXT("You have %i lives remaining!"), Lives);
EndGame();
return;
}
//Cheking if it is a isogrm.
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters,Guess again!"));
return;
}
PrintLine(TEXT("You lost a live!"));
--Lives;
if(Lives <= 0)
{
EndGame();
PrintLine(TEXT("You have no lives!"));
PrintLine(TEXT("The hidden word was '%s'!"), *HiddenWord);
ClearScreen();
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 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison])
{
return false;
}
}
}
return true;
}
TArray UBullCowCartridge::GetValidWord(const TArray& WordList)
{
TArray<FString> ValidWords;
for (FString Word : WordList)
{
if (Word.Len() = 5 && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}
FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
FBullCowCount Count;
//For every index Guess and the index hidden word are same.then Bullcount ++.
//If not a bull is it a 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;
}
TArray UBullCowCartridge::MixingLettersone(TArray& LetterMixerone) const
{
LetterMixerone = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.
TCHAR HW[] = TEXT("%S"), *Random;
PrintLine(TEXT("%C%C%C%C%C"), Random[4], Random[2], Random[0], Random[1], Random[3]);
}
TArray UBullCowCartridge::MixingLetterstwo(TArray& LetterMixertwo) const
{
LetterMixertwo = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.
TCHAR HW[] = TEXT("%S"), *Random;
PrintLine(TEXT("%C%C%C%C%C"), Random[3], Random[2], Random[0], Random[4], Random[1]);
}
TArray UBullCowCartridge::MixingLettersthree(TArray& LetterMixerthree) const
{
LetterMixerthree = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.
TCHAR HW[] = TEXT("%S"), *Random;
PrintLine(TEXT("%C%C%C%C%C"), Random[3], Random[0], Random[4], Random[2], Random[1]);
}
TArray UBullCowCartridge::MixingLettersfour(TArray& LetterMixerfour) const
{
LetterMixerfour = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.
TCHAR HW[] = TEXT("%S"), *Random;
PrintLine(TEXT("%C%C%C%C%C"), Random[2], Random[0], Random[4], Random[3], Random[1]);
}
(2.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”
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> GetValidWord(const TArray<FString>& WordList);
FBullCowCount GetBullCows(const FString& Guess) const;
TArray<FString> MixingLettersone(TArray<FString>& LetterMixerone, TArray<FString>& Random);
TArray<FString> MixingLetterstwo(TArray<FString>& LetterMixertwo, TArray<FString>& Random);
TArray<FString> MixingLettersthree(TArray<FString>& LetterMixerthree, TArray<FString>& Random);
TArray<FString> MixingLettersfour(TArray<FString>& LetterMixerfour, TArray<FString>& Random);
// Your declarations go below!
private:
FString HiddenWord;
FString Random;
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
};