I finally got my code to work thanks to DanM in the post: Isogram Trouble.
I kept getting a LNK2005 error, which meant that my header files were being used between two .cpp files. A big No No in C++. The issue was that one of my backup files had been saved in my projects’ folder rather than my backup folder. Thus both were using the same code.
I was taken out of the frying-pan, tossed into the oven, and then burned.
Here is my working code:
#include "BullCowCartridge.h"
//include "HiddenWordList.h"
#include "LatinWordList.h"
//#include "Math/UnrealMathUtility.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
if (bGameOver == true)// do not need to add "==true", but it helps with self-documenting
{
ClearScreen();
SetupGame();
return;
// Quit Game?
}
else // Check Player guess
{
ProcessGuess(PlayerInput);
return;
}
}
void UBullCowCartridge::SetupGame()
{
// Welcomeing The Player
PrintLine(TEXT("Salve! Welcome to TaurusBos!"));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() -1)];
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line
// *= dereference
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("you have %i lives."), Lives);
PrintLine(TEXT("Type in your guess and \npress enter to moove on..."));
PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nPress Enter to play again..."));
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("Veni, Vidi, Vici!"));
EndGame();
return;
}
PrintLine(TEXT("You lost a Life!"));
--Lives;
if (Guess.Len() != HiddenWord.Len())//Right Length?
{
PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());
PrintLine(TEXT("Sorry! Guess again, you have %i \nlives left."), Lives);
return;
}
// Isogram?
if (!IsIsogram(Guess))
{
PrintLine(TEXT("You have repeating letters, guess again!"));
PrintLine(TEXT("Sorry! Guess again, you have %i \nlives left."), Lives);
return;
}
if (Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left!"));
PrintLine(TEXT("The hidden world was: %s."), *HiddenWord);
EndGame();
return;
}
// Show the player BUlls and Cows
PrintLine(TEXT("Guess again, you have %i lives left."), Lives);
// Check User PlayerInput
// Play Again or Quit?
}
bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
// Instructors Code
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; //Making the empty storage unit: the variable.
for (const FString& Word : WordList )
{
if (Word.Len() >= 3 && Word.Len() <= 8 && IsIsogram(Word) )//Checks specific word
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}
I changed the minimum word condition to three so that more Latin words could be included. This brought the maximum valid words up from three-hundred some to closer to 460.
I also placed my “Life” decrement up toward and below the “Ypu Win” if-statement. Now it decrements every time the player does not guess correctly. Now the player is told their error and then how many lives they have left.