I managed to get the loop printing the first 5 words without issue. I remembered to use a pointer.
BullCowCartritge.h
// 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& Input) override;
void SetupGame();
void EndGame();
void MinusLife();
void ProcessGuess(FString Guess);
bool CheckIsogram(FString Word2Check);
void BullCowculator(FString Word3Check);
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives, AttemptWarning;
bool bGameOver;
// TArray <FString> Words;
};
HiddenWordList.h (only a few of the words to save on space)
#pragma once
#include "CoreMinimal.h"
const TArray <FString> Words =
{
TEXT("a"),
TEXT("ability"),
TEXT("able"),
TEXT("about"),
TEXT("above"),
TEXT("accept"),
TEXT("according"),
TEXT("account"),
TEXT("across"),
TEXT("act"),
TEXT("action"),
TEXT("activity"),
TEXT("actually"),
TEXT("add"),
};
BullCowCartridge.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
// const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
// FFileHelper::LoadFileToStringArray(Words, *WordListPath);
SetupGame();
PrintLine(TEXT("The number of possible words is %i"), Words.Num());
PrintLine(TEXT("Hidden word is: %s. "), *HiddenWord); // Debug Line
for (int32 Index = 0; Index < 5; Index++)
{
PrintLine(TEXT("%s"), *Words[Index]);
}
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
PrintLine(TEXT("***Bull Cows Game*** "));
HiddenWord = TEXT("aspired");
Lives = HiddenWord.Len();
AttemptWarning = 0;
bGameOver = false;
PrintLine(TEXT("Guess the %i letter isogram: "), HiddenWord.Len()); //Prompt player for guess
PrintLine(TEXT("You have %i lives. "), Lives);
PrintLine(TEXT("Type your guess \nPress enter to continue... "));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\n*** Game Over ***. "));
PrintLine(TEXT("Press enter to play again. "));
// PrintLine(TEXT("Press something to quit. "));
}
void UBullCowCartridge::MinusLife()
{
if (Lives > 0)
{
PrintLine(TEXT("You have %i lives remaining. "), --Lives);
if (Lives == 0)
{
PrintLine(TEXT("Last try! "));
}
AttemptWarning = 0;
}
else
{
ClearScreen();
PrintLine(TEXT("You have no lives remaining. "));
AttemptWarning = 0;
PrintLine(TEXT("\nYou Lose!"));
EndGame(); // Losing EndGame
}
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("You win!"));
EndGame(); // Winning EndGame
return;
}
if (Guess.Len() != HiddenWord.Len()) //Checking if guess is not valid length
{
if (AttemptWarning++ < 2) // Tracking and advising player's invalid attempts
{
PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
PrintLine(TEXT("The hidden word is %i characters long. "), HiddenWord.Len());
if (Guess.Len() < HiddenWord.Len())
{
PrintLine(TEXT("Your guess needs to be longer. "));
return;
}
else
{
PrintLine(TEXT("Your guess needs to be shorter. "));
return;
}
}
// Invalid attempt punishment
if (Lives > 0)
{
PrintLine(TEXT("Guessing carelessly lost you a life. "));
MinusLife();
return;
}
else
{
PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
MinusLife();
return;
}
}
if (!CheckIsogram(Guess)) //Checking if guess is not an isogram
{
if (AttemptWarning++ < 2) // Tracking and advising player's invalid attempts
{
PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
PrintLine(TEXT("Your guess was not an isogram."));
return;
}
if (Lives > 0)
{
PrintLine(TEXT("*Note* An isogram is a word with no repeating letters."));
MinusLife();
return;
}
else
{
PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
MinusLife();
return;
}
BullCowculator(Guess);
}
}
bool UBullCowCartridge::CheckIsogram(FString Word2Check)
{
PrintLine(TEXT("checking if %s is an isogram. "), *Word2Check); //Debug ling
for (int32 Index = 0; Index < Word2Check.Len(); Index++)
{
for (int32 Comparison = Index + 1; Comparison < Word2Check.Len(); Comparison++)
{
PrintLine(TEXT("cisog check 1: %c to %c"), Word2Check[Index], Word2Check[Comparison]); // Debug line
if (Word2Check[Index] == Word2Check[Comparison])
{
return false;
}
}
}
return true;
}
void UBullCowCartridge::BullCowculator(FString Word3Check)
{
PrintLine(TEXT("The bulls and cows for %s are: "), *Word3Check); // Debug line
// implement bull cow checking based on argument
}