Number of lives not decreasing

#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    SetupGame();
    PrintLine(FString::Printf(TEXT("The hidden word is %s"), *Hiddenword));//Debug line
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    if(bGameOver)
    {
         ClearScreen();
         SetupGame();
    }
    else//checking player guess
    {
        ProcessGuess(Input);
    }
}

void UBullCowCartridge::SetupGame()
{
    Hiddenword= TEXT("cabinet") ;
    lives=Hiddenword.Len();   
    bGameOver= false;

    PrintLine(TEXT("Hi There! Welcome to the game"));
    PrintLine(TEXT("Guess the %i lettered word"),Hiddenword.Len());  
    PrintLine(TEXT("Type in your guess. \nPress enter to continue"));
    PrintLine(TEXT("Number of lives left is %i"),lives);
    Hiddenword= TEXT("cabinet") ;
}

void UBullCowCartridge::EndGame()
{
    bGameOver=true;
    ClearScreen();
}

void UBullCowCartridge::ProcessGuess(FString Input)
{
    if(Input== Hiddenword)
    {
            PrintLine(TEXT("You Have Won !!"));
            EndGame();
            return;
     } 
    /*if(!Isogram())
    {
        PrintLine(TEXT("There are no repeating letters"));
    }*/

    lives--;
    PrintLine(TEXT("Lost a life"));
    PrintLine(TEXT("Guess again. You have %i lives remaining"),lives);

    if(Hiddenword.Len()!=Input.Len())
    {
         PrintLine(TEXT("Hiddenword has %i letters"),Hiddenword.Len());
         return;
    } 
    if(lives<=0)
    {
        PrintLine(TEXT("GAME OVER!!!"));
        PrintLine(TEXT("The hidden word was %s"),*Hiddenword);
        PrintLine(TEXT("\nPress Enter to continue playing"));
        EndGame();
        return;
    }
}

This is my code.
The output is attachedDOUBT1

I’ve edited your post to properly format the code, please do that in the future.

Your output isn’t matching the code so I would suggest you try recompiling.

okay. Thank you.

Recompiled. Yet getting wrong output.

Does the output in BeginPlay now match though? You have this in SetupGame which is called BeginPlay which is not present in your screenshot

PrintLine(TEXT("Hi There! Welcome to the game"));
PrintLine(TEXT("Guess the %i lettered word"),Hiddenword.Len());  
PrintLine(TEXT("Type in your guess. \nPress enter to continue"));
PrintLine(TEXT("Number of lives left is %i"),lives);

Wondering why you believe the lives aren’t being deducted? Looking at your code, you are setting lives to the hidden word length.
The word in your example being 7 characters long. You make two wrong guesses (cake and biscuit) and you end up at 5 lives remaining.

The only thing missing is the deduction of lives not reporting when you aren’t entering 7 character words but looking at your code, that is expected as this part of your code is being executed.

if(Hiddenword.Len()!=Input.Len())
    {
         PrintLine(TEXT("Hiddenword has %i letters"),Hiddenword.Len());
         return;
    }

Yes, the output in BeginPlay is supposed to print the hidden string. So I was getting that.

1 Like

I did some editing in the code, like i added SetupGame after ClearScreen() in EndGame(). But I am having a new error now. The number of lives are reaching negatives, that is the game is not stopped once lives<=0. The code in the lives<=0 section is not being executed I guess.

#include "BullCowCartridge.h"

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

{

    Super::BeginPlay();

    SetupGame();

    PrintLine(FString::Printf(TEXT("The hidden word is %s"), *Hiddenword));//Debug line

   

}

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

{

   

    if(bGameOver)

    {

         ClearScreen();

         SetupGame();

    }

    else//checking player guess

    {

        ProcessGuess(Input);

    }

        

}

void UBullCowCartridge::SetupGame()

{

    Hiddenword= TEXT("cabinet") ;

    lives=Hiddenword.Len();   

      bGameOver= false;

    PrintLine(TEXT("Hi There! Welcome to the game"));

    PrintLine(TEXT("Guess the %i lettered word"),Hiddenword.Len());  

    PrintLine(TEXT("Type in your guess. \nPress enter to continue"));

    PrintLine(TEXT("Number of lives left is %i"),lives);

    Hiddenword= TEXT("cabinet") ;

     

}

void UBullCowCartridge::EndGame()

{

    bGameOver=true;

    ClearScreen();

    SetupGame();

    

}

void UBullCowCartridge::ProcessGuess(FString Input)

{

    if(Input== Hiddenword)

        {

            PrintLine(TEXT("You Have Won !!"));

            EndGame();

            return;

         } 

    /*if(!Isogram())

    {

        PrintLine(TEXT("There are no repeating letters"));

    }*/

    lives--;

    PrintLine(TEXT("Lost a life"));

    PrintLine(TEXT("Guess again. You have %i lives remaining"),lives);

    if(Hiddenword.Len()!=Input.Len())

        {

            PrintLine(TEXT("Hiddenword has %i letters"),Hiddenword.Len());

            return;

        } 

           

    else if(lives==0)

    {

        PrintLine(TEXT("GAME OVER!!!"));

        PrintLine(TEXT("The hidden word was %s"),*Hiddenword);

        PrintLine(TEXT("\nPress Enter to continue playing"));

        EndGame();

        return;

    }

            

}

Number of lives left is not being reported if wrong word length is equal to hidden word length.

Could you package your project and upload it somewhere?

File > Package Project > Zip Up Project

Instead of

else if(lives==0)

Give the following a try

if(lives<=0)
1 Like

it was like this previously, but did not work.

1 Like

Can you please guide me more on how to package my project?

In Unreal

Then upload that somewhere.

can you refer some site where I can upload? :sweat_smile: :sweat_smile:

Google Drive, Dropbox, One Drive.

https://drive.google.com/file/d/1sqtsBrLNAJ5Dm5Oqml4MMpecu4DVnM-a/view?usp=sharing

is it fine?

Works as expected
image

Number of lives becoming negative.
The game should reset on reaching zero.

doubt.JPG

Ok I’m an idiot, your problem is that you’re decrementing lives before the checks.

lives--;
PrintLine(TEXT("Lost a life"));
PrintLine(TEXT("Guess again. You have %i lives remaining"), lives);
if (Hiddenword.Len() != Input.Len())
{
    //...
	return;
}
if (lives == 0)
{
    //....
	return;
}

Whereas if you have it just before the check it should work as expected.

Privacy & Terms