All like in video
File “HiddenWordsList.h”
#include "CoreMinimal.h"
const TArray<FString> Words =
{
TEXT("cakes"),
TEXT("water"),
TEXT("plums"),
TEXT("kings"),
TEXT("knife")
};
And “BullCowCartridge.cpp”:
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
PrintLine(FString::Printf(TEXT("The HiddenWord is: %s. It is %i character long."), *HiddenWord, HiddenWord.Len())); //Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
HiddenWord = TEXT("Cake"); // Set the HiddenWord
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Hello and welcome to my terminal game \nabout bulls and cows!"));
PrintLine(TEXT("For start, pls input Enter"));
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("You have %i Lives"), Lives);
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press Enter to play again"));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT("You win!"));
EndGame();
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("HiddenWord have only %i letter"), HiddenWord.Len());
Lives = --Lives;
PrintLine(TEXT("Lives = %i"), Lives);
}
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeting characters"));
}
--Lives;
PrintLine(TEXT("Lost your live"));
if (Lives <= 0)
{
PrintLine(TEXT("You Lives is over. \nPlay Enter for play again"));
}
else {
PrintLine(TEXT("You lose!"));
EndGame();
}
}
bool UBullCowCartridge::IsIsogram(FString Word) const
{
int32 Index = 0;
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;
return true;
}