I am stuck on BullCows game... (Life issue)

my game seems to work fine until I type a word less than the “hidden word” or more than the
“hidden word”, and I can’t see why it only wants to take a life when the word is as long as the
“hidden word” only…

#include "BullCowCartridge.h" 


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

    PrintLine(TEXT("The HiddenWord is: %s."), *HiddenWord); //DebugLine

}

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

    }
}

void UBullCowCartridge::SetupGame()

{
 //welcome the player
    PrintLine(TEXT("Welcome to Bull Cows!"));

    HiddenWord = TEXT("cake");
    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine(TEXT("\nGuess the %i letter word!"), HiddenWord.Len());
    PrintLine(TEXT("You have %i lives.\n"), Lives);
    PrintLine(TEXT("Type in your guess. \nPress enter to continue..."));
}

void UBullCowCartridge::EndGame()

{
    bGameOver = true;
    PrintLine(TEXT("\nPress 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("Sorry, try guessing again! \nYou have %i lives remaining"), Lives);
        PrintLine(TEXT("The hidden word is %i letter long"), HiddenWord.Len());
        return;
    }




    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;
    }

    // show the player bulls and cows
    PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}

hope someone here can see the problem why this doesn’t work properly because I can’t see it at all even tho I try to follow the tutorial I can’t seem to get it working.

whoever helps me out, I thank you a lot in advance! :slight_smile:

Because of the return statements, return statements exit the function. Remember that Mike didn’t want to decrement a life on invalid guesses.

2 Likes

Thanks bud, helped alot! :slight_smile:

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

Privacy & Terms