Number of lives not decreasing

But I am decrementing lives after it checks if input is equal to the hiddenword.
Now, for any situation where input ! = hiddenword, lives should be decremented.

Moreover, when lives==0, i am calling the endgame function. That should reset the game. But for some reason

the compiler does not reach the lives==0 point.

But you return from the function if the length is not equal.

Oh yes! why am I such an idiot. Okay so I have modified the code a bit. I have overcome the problem of infinite loop of lives getting decremented. but a new issue has turned up. the condition for lives==0 is not getting executed.

void UBullCowCartridge::ProcessGuess(FString Input)

{

    if(Input== Hiddenword)

        {

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

            EndGame();

            return;

         } 

    /*if(!Isogram())

    {

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

    }*/

    

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

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

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

        {

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

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

            lives--;

        } 

           

    if(lives==0)

    {

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

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

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

        EndGame();

    }

    return;

            

}

Please help.

EDIT: Also I just now realised this code won’t work if the input is wrong but the word length is same as hiddenword.

Okay. So:

  1. Your "You lost a life and your guess again will happen every time, regardless if it’s correct or not. It needs to be within the if (Hiddenworld.Len() != Input.Len()) statement, or it will execute even if it’s correct. You could also get away with using an else statement instead of that .len() call:
if (input == Hiddenworld)
{
    // what you have
}else{
//it's wrong
}

That else statement should work. The first part of the statement you are comparing a string to a string, to see if they’re equal. The second part of the statement you are comparing length of strings, which is comparing int to int, which means if the two strings are not the same the code is only getting executed if they’re not the same length.

  1. A function with return type void does not require a return statement. There is nothing to return. You can do with just ending after the else.

Privacy & Terms