How does WordList work?

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;
}

Right here specifically

GetValidWords(Words)

Parameter names have absolutely no connection to names of variables passed in to it. How can it? As a library writer, how would I know what you would end up calling your variables? It might not even have a name.

Functions are given values not names of things.

int square(int n)
{
    return n * n;
}

int main()
{
    int v1 = square(3);
    int v2 = square(v1);
    int v3 = square(square(v2));
}

At each call site it will initialise the parameter with the value that was passed in. For the first call no variable was given, just the value 3, that would initialise the function’s parameter and will multiply by itself and return that value (9) which will then be used to initialise v1.

On the next line the value of v1 (9) gets passed into the function and the same thing happens again so v2 will store the value 81.

O the last line square gets called by the value returned by square(v2) so first square(v2) gets called and 6,561 is returned (81 * 81) and that value is passed into square and 43,046,721 (6,561 * 6,561) is returned and stored in v3

Compiled on: Compiler Explorer

Privacy & Terms