Here's my code, but it has a peculiar oddity

Hello! James here! I’ve been revisiting this course to refresh my Unreal creativity, and here’s what my code is looking like so far. However I seem to have a curious issue with part of the code working.

#include "BullCowCartridge.h"

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

    Super::BeginPlay();

    SetUpGame();    

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

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

    //If game is over, then do ClearScreen() and SetUpGame().
    if (bGameOver) {
        ClearScreen(); 
        SetUpGame();
    }
    else { //Checking the PlayerGuess while the game is not over.

        if (Input == HiddenWord) { 
            PrintLine(TEXT("Your guess was: ")+Input);
            PrintLine(TEXT("Correct! You got the hidden word and won!"));
            EndGame();
        }

    else {   
        PrintLine(TEXT("Your guess was: ")+Input);  
        PrintLine(TEXT("Incorrect! You have lost a life!"));
        PrintLine(TEXT("You have %i lives remaining."), --Lives);

        if (Lives > 0) {
            if (Input.Len() != HiddenWord.Len())
            {
                PrintLine(TEXT("Incorrect! Try guessing again! \nYou have %i lives remaining."), --Lives);         
            }
        }
        else {
            PrintLine(TEXT("You have no lives left! Game over!"));
            EndGame();
        }
      }
    }
}

void UBullCowCartridge::SetUpGame() {
    //Welcoming the Player
    PrintLine(TEXT("Hi there! Welcome to Bulls and Cows!"));  
    HiddenWord = TEXT("cakes"); 
    Lives = HiddenWord.Len();
    bGameOver = false;   
    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
    PrintLine(TEXT("To begin, please press Tab to start, \ntype in your guess and hit Enter!")); //Prompt the player for Guess
    PrintLine(TEXT("You have %i lives left"), Lives);  
}

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

While the game runs fine so far and is successful upon clearing and getting the correct HiddenWord, it seems that it deducts two lives if I don’t have the correct word AND/OR the length of the PlayerGuess’s word is not the same as the HiddenWord. So say if I typed, “cat”, the result would deduct two lives instead of one.
Is there a way to fix this?
Thanks in advance!
From James.

Well you’re deducting lives in two places.

else 
{   
    PrintLine(TEXT("Your guess was: ")+Input);  
    PrintLine(TEXT("Incorrect! You have lost a life!"));
    // 1
    PrintLine(TEXT("You have %i lives remaining."), --Lives);
    if (Lives > 0) 
    {
        if (Input.Len() != HiddenWord.Len())
        {
            // 2
            PrintLine(TEXT("Incorrect! Try guessing again! \nYou have %i lives remaining."), --Lives);
        }
    }
}

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

Privacy & Terms