After replacing GetValidWords with the new Isograms variable in this lesson, Unreal crashes upon hitting Play.
Attached is my cpp and header file. Any advice is appreciated, as this is quite frustrating the game is now broken and I’m unable to move on.
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
GetValidWords(Words);
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("Word List/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(Words, *WordListPath);
SetupGame();
// PrintLine(TEXT("The number of valid words is %i."), GetValidWords(Words).Num()); // DEBUG ONLY
// PrintLine(FString::Printf(TEXT("The secret word is: %s.\nIt is %i-characters long."), *HiddenWord, HiddenWord.Len())); // DEBUG ONLY
// PrintLine(TEXT("ValidWords - 1 is %i"), GetValidWords(Words).Num() - 1); // DEBUG ONLY
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetupGame()
{
PrintLine (TEXT("Welcome to Bull Cow Game!"));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("\nGuess 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(const FString& Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT("You have won!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("It's %i letters, dumbass."), HiddenWord.Len());
PrintLine(TEXT("You have %i lives remaining."), Lives);
return;
}
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters, moron."));
return;
}
--Lives;
PrintLine(TEXT("%i lives remaining, dimwit."), Lives);
if (Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left."));
PrintLine(TEXT("The hidden word was %s."), *HiddenWord);
EndGame();
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;
}
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;
}
// 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& PlayerInput) 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;
// Your declarations go below!
private:
FString HiddenWord; // Declares the HiddenWord
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
};