I understand how “Words” works. When you open HiddenWordList.h the variable name is given “Words”.
Originally the instructor leaves in “Words” on accident, and it works properly. At the end he notices the mistake and replaces all of the occurrences of “Words” with “WordList”.
I can’t find anywhere in the code where we took the “Words” array and associated it with the new variable “WordList”.
When we created the new GetValidWords function. Simply because we said TArray is that enough for it to know we are pulling from the TArray Words source?
We have done this type of thing in the past as well and I never really understood why a new variable name can magically pull the same data without having some line of code where the two are associated. I’m a serious green horn. Sorry if I missed something. But in this case especially with WordList, I don’t see how the game knows what we mean by WordList??
This is the function in question:::
TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> WordList) const
{
TArray<FString> ValidWords;
for (int32 Index = 0; Index < WordList.Num(); Index++)
{
if (WordList[Index].Len() >= 4 && WordList[Index].Len() <= 8 && IsIsogram(WordList[Index]))
{
ValidWords.Emplace(WordList[Index]);
}
}
return ValidWords;
}
HiddenWordList.h
(The top of it)
#pragma once
#include "CoreMinimal.h"
const TArray<FString> Words =
{
TEXT("a"),
TEXT("ability"),
TEXT("able"),
TEXT("about"),
TEXT("above"),
BullCowCartridge.h
#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(FString Guess);
bool IsIsogram(FString Word) const;
TArray<FString> GetValidWords(TArray<FString>) const;
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
};
And just in case you need it here is the entire
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();
SetupGame();
PrintLine(TEXT("The number of possible words is %i"), Words.Num());
PrintLine(TEXT("The number of valid words is: %i."), GetValidWords(Words).Num());
PrintLine(FString::Printf(TEXT("The HiddenWord is %s"), *HiddenWord)); // Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // checking the player guess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
// Welcoming the player
PrintLine(TEXT("Welcome back to the BullCow Game!\n"));
HiddenWord = TEXT("cakes");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("You have %i lives!"), Lives);
PrintLine(TEXT("Guess the %i letter word!\n"
"Type in your guess!\n"
"Hit enter to continue!"), HiddenWord.Len()); // Prompt player for guess
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("The Hidden Word was %s"), *HiddenWord); // if no show GameOver message and show HiddenWord
PrintLine(TEXT("Press enter to play again!"));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT("Congrats, you guessed correctly! \nYou Win!!!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len()) // Check number of characters
{
PrintLine(TEXT("Remember the Hidden Word has %i characters!"), HiddenWord.Len());
PrintLine(TEXT("You lose 1 extra life for being stupid!")), --Lives;
}
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters, guess again!"));
return;
}
else
{
PrintLine(TEXT("You have guessed incorrectly!"));
PrintLine(TEXT("You have lost a life!"));
--Lives; // Remove Life
if (Lives > 0) // Check if lives are > zero
{
PrintLine(TEXT("Guess Again!")); // Prompt to guess again if lives are greater than 0
PrintLine(TEXT("You have %i lives left!"), Lives);
return;
}
else
{
PrintLine(TEXT("You have no lives left! \nGame Over!!!"));
EndGame();
}
}
}
bool UBullCowCartridge::IsIsogram(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(TArray<FString> WordList) const
{
TArray<FString> ValidWords;
for (int32 Index = 0; Index < WordList.Num(); Index++)
{
if (WordList[Index].Len() >= 4 && WordList[Index].Len() <= 8 && IsIsogram(WordList[Index]))
{
ValidWords.Emplace(WordList[Index]);
}
}
return ValidWords;
}