After implementing the Lambda I’m getting an Array out of bounds…Maybe I have been staring at a screen too long today but I don’t see it.
#include "BullCowCartridge.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
void UBullCowCartridge::BeginPlay()
{
Super::BeginPlay();
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("/WordLists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArrayWithPredicate(IsoGrams, *WordListPath, [](const FString& Word)
{
return Word.Len() >= 4 && Word.Len() <= 8 && IsIsogram(Word);
});
IsoGrams = GetValidWords(AllWords);
InitGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput)
{
if (bGameOver)
{
ClearScreen();
InitGame();
}
else
{
GuessEval(PlayerInput);
}
}
void UBullCowCartridge::InitGame()
{
PrintLine(TEXT("This is a game called Bull/Cows.\nIt's about guessing a word."));
HiddenWord = IsoGrams [FMath::RandRange(0, IsoGrams.Num() -1)];
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("The hidden word is: %s"), *HiddenWord);
PrintLine(TEXT("You have %i chances\nYour word has %i letters"),Lives ,HiddenWord.Len());
// Prompt for PLayerGuess
PrintLine(TEXT("Make your guess:"));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\n\n Press Enter to play again."));
}
void UBullCowCartridge::GuessEval(const FString& Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("YEP! Thats right and with %i chances left!"), Lives);
EndGame();
return;
}
else
{
if (!IsIsogram(Guess))
{
PrintLine(TEXT("Nope, repeating letters not allowed."));
return;
}
--Lives;
if (Lives > 1)
{
PrintLine(TEXT("Nope, nice try though\nChance Lost\nRemaining chances: %i"), Lives);
}
if (Lives == 1)
{
PrintLine(TEXT("Ok, settle down\nThis is your LAST CHANCE"));
}
if (Lives == 0)
{
ClearScreen();
PrintLine(TEXT("I see you are all out of chances.\nGAME OVER!\nThe word you were looking for was: %s."), *HiddenWord);
EndGame();
return;
}
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Related - didn't I say it is %i characters?"), HiddenWord.Len());
}
}
bool UBullCowCartridge::IsIsogram(const FString& Word)
{
for (int32 Index = 0; Index < Word.Len() -1; Index++)
{
for (int32 Compare = Index +1; Compare < Word.Len(); Compare++)
{
if (Word[Index] == Word[Compare])
return false;
}
}
return true;
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
{
TArray<FString> ValidWords;
for (FString TempWord : WordList)
{
if (TempWord.Len() >= 4 && TempWord.Len() <= 8 && IsIsogram(TempWord))
{
ValidWords.Emplace(TempWord);
}
}
return ValidWords;
}