Need to press enter on 0 lives

I need to press enter when i have 0 lives left, but I can’t find out a way after going from 1 to 0 lives to go to my else statement:

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    SetupGame();

    PrintLine(TEXT("The HiddenWord is: %s. \nIt is %i Characters long"), *HiddenWord, HiddenWord.Len()); //debug Line
    }

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

{
/* 
    if game is over ClearScreen() and SetupGame()
    else check PlayerGuess  
 */

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

}

void UBullCowCartridge::SetupGame()
{
    HiddenWord = TEXT("bertja");
    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine(TEXT("This is the BullCow Game!"));
    PrintLine(TEXT("You have %i lives"), Lives);
    PrintLine(TEXT("Guess the %i letter isogram"), HiddenWord.Len());
    PrintLine(TEXT("Your input here, then press enter")); 

        // const TCHAR HW[] = TEXT("plums");
        //PrintLine(TEXT("Character 1 of the hidden word is: %c"), HiddenWord[0]); // print "b"
        //PrintLine(TEXT("Character 4 of the HW is: %c"), HW[3]); // print "m"
    IsIsogram(HiddenWord);

}

void UBullCowCartridge::EndGame()
    {
        bGameOver = true;
        PrintLine(TEXT("Press enter to restart"));
    }

     void UBullCowCartridge::ProcessGuess(FString Guess)
     {
            if (Lives > 0)
            { 
                if(Guess == HiddenWord)
                {  
                    PrintLine(TEXT("You are correct and the winner!"));
                    EndGame();
                    return;
                }        
                if(Guess.Len() != HiddenWord.Len())
                {
                    PrintLine(TEXT("Please input a %i letter word"), HiddenWord.Len());
                    PrintLine(TEXT("You have %i lives left"), Lives);
                    return;      
                }
                if(!IsIsogram(Guess))
                {
                PrintLine(TEXT("The word can't have duplicate letters\nGuess again"));
                return;
                }
                --Lives;
                PrintLine(TEXT("You Lost a life\nYou have %i Lives left"), Lives);
                return;
            }
            else
            {
            ClearScreen();
            PrintLine(TEXT("You lose, the hiddenword was %s"), *HiddenWord);
            EndGame();
            return;          
            }
        // PrintLine(TEXT("Wrong answer\nthe hiddenword is %i letters long"), HiddenWord.Len());
        // PrintLine(TEXT("You got x Bulls and x Cows"));
        // PrintLine(TEXT("You have %i lives left"), Lives);
     }

    bool UBullCowCartridge::IsIsogram(FString Word)
    {

        // if Guess[all] != Guess[Word.Len() -1]
        // compare the letters
        // if any letter is the same return false
        
        for (int32 Index = 0; Index < Word.Len(); Index++)
        {
            PrintLine(TEXT("%c"), Word[Index]);
            
        }
    
     
    
    return true;
    }

Hey I think this is probably to do with the logic flow here (assuming I’ve understood your question correctly!) I’ve commented on your code below but removed unnecessary parts to your question.

Let’s assume you have 1 life remaining…

void UBullCowCartridge::ProcessGuess(FString Guess)
     {
            if (Lives > 0) // We have one life so excecute this block. 
            { 
              
                --Lives; // We decrement the count now lives == 0;
                PrintLine(TEXT("You Lost a life\nYou have %i Lives left"), Lives);
                //We now display 0 lives to the user. Is this what we intended?
                //So now our lives are actually at 0, the next time we evaluate this if statement we run through into your else statement. 
                return; 
            }
            else
            {
            ClearScreen();
            PrintLine(TEXT("You lose, the hiddenword was %s"), *HiddenWord);
            EndGame();
            return;          
            }
     }

It pays to take a little time to think about the order in which you perform your tasks. What if you switch the decrement step to perform after the printout step for example?

Thanks for the comment, I thought I had already tried that, but maybe is just changed the location of the – to:
Lives–

The only thing now is that the first try is “free”, or atleast it shows the incorrect amount of lives, because the decrement comes after the message.

Yes, each change throws up new challenges. For me that’s very much part of the fun. You could always:

if (Lives > 1) 
{
}

:wink:

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

Privacy & Terms