I tried implementing the code from this lecture and did not get an error, but UE keeps crashing when I try launching the game.
Here is the .cpp file
#include "BullCowCartridge.h"
#include "Math/UnrealMathUtility.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
GetValidWords(Words);
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(Words, *WordListPath);
//Words.RemoveAll([this](auto& word) {return IsIsogram(word); });
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // Checking PlayerGuess
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetupGame()
{
//Welcoming the Player
PrintLine(TEXT("Welcome to the Bull Cows!"));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num()-1)];
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("You start off with %i lives."), Lives);
PrintLine(TEXT("The Hidden Word is: %s."), *HiddenWord);//Debug Line
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
ClearScreen();
PrintLine(TEXT("\nWould you like to play again?\nIf so press 'Enter' to play again!"));
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT("Congrats, you guessed the right word!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The word is %i letters long \nyou have %i lives left..."), HiddenWord.Len(), Lives);
return;
}
// Check for isogram
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letter \nguess again..."));
return;
}
--Lives;
if (Lives <= 0)
{
PrintLine(TEXT("It seems you have run out of lives"));
PrintLine(TEXT("The Hidden word was %s."), *HiddenWord);
EndGame();
}
if (Lives > 0)
{
int32 Bulls, Cows;
GetBullCows(Guess, Bulls, Cows);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Bulls, Cows);
PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
return;
}
}
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;
}
//Limiting the words in the wordlist to just the valid ones
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
{
TArray<FString> ValidWords;
for (FString Word : WordList)
{
if (Word.Len() >=4 && Word.Len() <=8 && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}
void UBullCowCartridge::GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const
{
BullCount = 0;
CowCount = 0;
//for every index guess is same as index of hiddenword, BullCount++
//If not a bull was it a cow? if yes CowCount++
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
{
if (Guess[GuessIndex])
{
BullCount ++;
continue;
}
for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
{
if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
{
CowCount++;
}
}
}
}
And here is the .h file
#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();
void EndGame();
void ProcessGuess(const FString& Guess);
bool IsIsogram(const FString& Word) const;
TArray<FString> Words;
TArray<FString> GetValidWords(const TArray<FString>& WordList) const;
void GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const;
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
};