Logical Operators - Not creating list

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

for (int32 Index = 0; Index != 10; Index++)
{
    if (Words[Index].Len() >=4 && Words[Index].Len() <=8)
    {
        PrintLine(TEXT("%s"), *Words[Index]);
    }        
}

}
There are no compilation errors, but when I press play, there is no list of words. I’m extremely confused. Everything I ran before adding the last for statements worked fine.

Do you mean that it prints “The number of possible words is 0”?

right after printing possible words, he uses an array to print the first 7 words in the array from the list. But it doesn’t do that.

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

TArray<FString> ValidWords;

for (int32 Index = 0; Index != 10; Index++)
{
    if (Words[Index].Len() >= 4 && Words[Index].Len() <= 8)
    {
        PrintLine(TEXT("%s"), *Words[Index]);
        ValidWords.Emplace(Words[Index]);
    }
}

for (int32 Index = 0; Index < ValidWords.Num(); Index++)
{
    PrintLine(TEXT("%s."), *ValidWords[Index]);
}

}

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

if (bGameOver)
{
    ClearScreen();
    SetupGame();
}
else  // Checking PlayerGuess
{
    ProcessGuess(Input);
}

}

void UBullCowCartridge::SetupGame()
{
// Welcome the player
PrintLine(TEXT(“Welcome to Bulls & Cows!”));

HiddenWord = TEXT("cakes");
Lives = HiddenWord.Len();

bGameOver = false;

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 continue."));

}

void UBullCowCartridge::EndGame()

{
bGameOver = true;
PrintLine(TEXT(“Press enter to play again.”));
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT(“You Have Won!”));
EndGame();
return;
}

if (Guess.Len() != HiddenWord.Len())
{
    PrintLine(TEXT("The hidden word is %i letters long"), HiddenWord.Len());
    PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining"), Lives);
    return;
}

//check if isogram
if (!IsIsogram(Guess))
{       
    PrintLine(TEXT("No repeating letters, guess again"));
    return;
}

// remove a life
PrintLine(TEXT("Lost a life!"));
--Lives;

if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left!"));
        PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
        EndGame();
        return;
    }
      

    PrintLine(TEXT("Guess again, 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;

And you’re sure the first 10 words fit the criteria (>= 4 && <= 8)? Because you’re only checking the first 10 and not all of them.

I believe you intended for the conditions for the two loops to be the other way round i.e.

for (int32 Index = 0; Index < Words.Num(); Index++)
{
    if (Words[Index].Len() >= 4 && Words[Index].Len() <= 8)
    {
        PrintLine(TEXT("%s"), *Words[Index]);
        ValidWords.Emplace(Words[Index]);
    }
}

for (int32 Index = 0; Index < 10; Index++)
{
    PrintLine(TEXT("%s."), *ValidWords[Index]);
}

Yes, it is actually a list of isograms. So there are no repeating letters in any of the words and I’ve removed words from the list that are over 8 characters and under 4 characters. I checked my code against the professors and I honestly can’t figure it out.

Could you provide the contents of your bull cow cartridge .h and .cpp and a snippet of your words variable?

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

Privacy & Terms