Bull Cow Game WordList.num() not outputting to terminal

As far as I can tell, I have all the code right across the correct files, but the number of possible words keeps printing as 0 instead of over 100. If anyone knows how to fix this, I’d appreciate it~

BullCowCartridge.cpp

#include "BullCowCartridge.h"

// #include "Misc/FileHelper.h"

// #include "Misc/Paths.h"

#include "HiddenWordList.h"

void UBullCowCartridge::BeginPlay() // When the game starts

{

    Super::BeginPlay();

    

    // const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordList/HiddenWordList.txt");

    // FFileHelper::LoadFileToStringArray(WordList, *WordListPath);

    InitGame(); 

    PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); //Debug Line

    PrintLine(TEXT("The number of possible words is %i"), WordList.Num());

}

void UBullCowCartridge::OnInput(const FString& Input)  // When the player hits enter

{

    ClearScreen();

    PrintLine(TEXT("Guess the %i letter word\nYou have %i lives"), HiddenWord.Len(), Lives);

    PrintLine(TEXT("Type in your guess\npress Enter to continue"));

    //Checking PlayerGuess

    if (bGameOver)

    {

        ClearScreen();

        InitGame();

    }

    else

    {

        PrintLine(Input);

        ProcessGuess(Input);

    }

        //Check if Isogram

        //Display Error, Prompt GuessAgain

        //Check right number of characters

        //Display Error, Prompt GuessAgain

        //Remove Life

        //Check if lives > 0

        //If True GuessAgain

        //Else Display GameOver and HiddenWord

        //Prompt Restart, Press Enter to Restart

        //Press Esc to Quit

        //Check User Input

    

}

    

void UBullCowCartridge::InitGame()

{

    PrintLine(TEXT("Welcome to Bull Cow Game "));

    HiddenWord = TEXT("fake");

    Lives = HiddenWord.Len();

    bGameOver = false;

    

    PrintLine(TEXT("Guess the %i letter word\nYou have %i lives"), HiddenWord.Len(), Lives); 

    PrintLine(TEXT("Type in your guess\npress Enter to continue"));

    //prompt player for guess

    const TCHAR HW[] = TEXT("plum");

    IsIsogram(HiddenWord);

}

void UBullCowCartridge::EndGame()

{

    bGameOver = true;

    if (Lives > 0)

    {

        PrintLine(TEXT("You Win!"));

    }

    else 

    {

        PrintLine(TEXT("You Lose!"));

        PrintLine(TEXT("The secret word was %s"), *HiddenWord);

    }

    PrintLine(TEXT("Press Enter to play again"));

}

void UBullCowCartridge::ProcessGuess(FString Guess)

{

    if (Guess == HiddenWord)

        {

            ClearScreen();

            // PrintLine(TEXT("You have won!"));

            EndGame();

            return;

        }

   

    if (Guess.Len() != HiddenWord.Len())

        {

            PrintLine(TEXT("Wrong number of letters! Guess a %i letter word"), HiddenWord.Len());

            PrintLine(TEXT("You have %i lives left"), Lives);

            return;

        }

     if (!IsIsogram(Guess))

    {

        PrintLine(TEXT("No repeating letters, guess again"));

        return;

    }

        ClearScreen();

        --Lives;

        // PrintLine(TEXT("You Lose"));

        // EndGame();

    

        if (Lives == 0)

        {

            EndGame();

            return;

        }

        ClearScreen();

        PrintLine(TEXT("Incorrect, guess again"));

        PrintLine(TEXT("You have %i lives left"), Lives);

}

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;

}

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 InitGame();

    void EndGame();

    void ProcessGuess(FString Guess);

    bool IsIsogram(FString Word) const;

    // Your declarations go below!

    private:

    FString HiddenWord;

    int32 Lives;

    bool bGameOver;

    TArray<FString> WordList;

};

HiddenWordList.h

const TArray<FString> WordList = 

{

    TEXT("Fake"),

    TEXT("cake"),

    TEXT("mars"),

    TEXT("flow"),

    TEXT("town"),

    TEXT("about"),

    TEXT("after"),

    TEXT("again"),

    TEXT("air"),

    TEXT("all"),

    TEXT("along"),

    TEXT("also"),

    TEXT("an"),

    TEXT("and"),

    TEXT("another"),

    TEXT("any"),

    TEXT("are"),

    TEXT("around"),

    TEXT("as"),

    TEXT("at"),

    TEXT("away"),

    TEXT("back"),

    TEXT("be"),

    TEXT("because"),

    TEXT("been"),

    TEXT("before"),

    TEXT("below"),

    TEXT("between"),

    TEXT("both"),

    TEXT("but"),

    TEXT("by"),

    TEXT("came"),

    TEXT("can"),

    TEXT("come"),

    TEXT("could"),

    TEXT("day"),

    TEXT("did"),

    TEXT("different"),

    TEXT("do"),

    TEXT("does"),

    TEXT("dont"),

    TEXT("down"),

    TEXT("each"),

    TEXT("end"),

    TEXT("even"),

    TEXT("every"),

    TEXT("few"),

    TEXT("find"),

    TEXT("first"),

    TEXT("for"),

    TEXT("found"),

    TEXT("from"),

    TEXT("get"),

    TEXT("give"),

    TEXT("go"),

    TEXT("good"),

    TEXT("great"),

    TEXT("had"),

    TEXT("has"),

    TEXT("have"),

    TEXT("he"),

    TEXT("help"),

    TEXT("her"),

    TEXT("here"),

    TEXT("him"),

    TEXT("his"),

    TEXT("home"),

    TEXT("house"),

    TEXT("how"),

    TEXT("I"),

    TEXT("if"),

    TEXT("in"),

    TEXT("into"),

    TEXT("is"),

    TEXT("it"),

    TEXT("its"),

    TEXT("just"),

    TEXT("know"),

    TEXT("large"),

    TEXT("last"),

    TEXT("left"),

    TEXT("like"),

    TEXT("line"),

    TEXT("little"),

    TEXT("long"),

    TEXT("look"),

    TEXT("made"),

    TEXT("make"),

    TEXT("man"),

    TEXT("many"),

    TEXT("may"),

    TEXT("me"),

    TEXT("men"),

    TEXT("might"),

    TEXT("more"),

    TEXT("most"),

    TEXT("Mr."),

    TEXT("must"),

    TEXT("my"),

    TEXT("name"),

    TEXT("never"),

    TEXT("new"),

    TEXT("next"),

    TEXT("no"),

    TEXT("not"),

    TEXT("now"),

    TEXT("number"),

    TEXT("of"),

    TEXT("off"),

    TEXT("old"),

    TEXT("on"),

    TEXT("one"),

    TEXT("only"),

    TEXT("or"),

    TEXT("other"),

    TEXT("our"),

    TEXT("out"),

    TEXT("over"),

    TEXT("own"),

    TEXT("part"),

    TEXT("people"),

    TEXT("place"),

    TEXT("put"),

    TEXT("read"),

    TEXT("right"),

    TEXT("said"),

    TEXT("same"),

    TEXT("saw"),

    TEXT("say"),

    TEXT("see"),

    TEXT("she"),

    TEXT("should"),

    TEXT("show"),

    TEXT("small"),

    TEXT("so"),

    TEXT("some"),

    TEXT("something"),

    TEXT("sound"),

    TEXT("still"),

    TEXT("such"),

    TEXT("take"),

    TEXT("tell"),

    TEXT("than"),

    TEXT("that"),

    TEXT("the"),

    TEXT("them"),

    TEXT("then"),

    TEXT("there"),

    TEXT("these"),

    TEXT("they"),

    TEXT("thing"),

    TEXT("think"),

    TEXT("this"),

    TEXT("those"),

    TEXT("thought"),

    TEXT("three"),

    TEXT("through"),

    TEXT("time"),

    TEXT("to"),

    TEXT("together"),

    TEXT("too"),

    TEXT("two"),

    TEXT("under"),

    TEXT("up"),

    TEXT("us"),

    TEXT("use"),

    TEXT("very"),

    TEXT("want"),

    TEXT("water"),

    TEXT("way"),

    TEXT("we"),

    TEXT("well"),

    TEXT("went"),

    TEXT("were"),

    TEXT("what"),

    TEXT("when"),

    TEXT("where"),

    TEXT("which"),

    TEXT("while"),

    TEXT("who"),

    TEXT("why"),

    TEXT("will"),

    TEXT("with"),

    TEXT("word"),

    TEXT("work"),

    TEXT("world"),

    TEXT("would"),

    TEXT("write"),

    TEXT("year"),

    TEXT("you"),

    TEXT("your"),

    TEXT("was")

};
PrintLine(TEXT("The number of possible words is %i"), WordList.Num());

This line is referring to your member variable you declared in your header file and not the one you declared in HiddenWord.h

This is known as variable shadowing. Rename either or comment out the member variable.

Thank you so much! that has stumped me for weeks now, but it is now working properly!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms