My code to remove lives and end game

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

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

PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); //This is a debug line that we can turn on and off in game. 




SetupGame(); //Setting up game. 



//Set amount of Lives.

}

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

ClearScreen();

    
if (Lives < 2)
{

    EndGame();

}

else
{

    if (Input == HiddenWord)
    {

        PrintLine(TEXT("You Win!")); //Win Message.

        
    }


    else
    {

        PrintLine(TEXT("That was an incorrect answer,\nplease try again.")); //Try again message.
        --Lives;
        PrintLine(TEXT("You have %i attempts left."), Lives);
        

        if (Input.Len() != HiddenWord.Len())
        {

            PrintLine(TEXT("Hint: The word contains %i letters.\n"), HiddenWord.Len()); //Random 4 letter word mentioned again.

        }

       
    }

}     

    

    if (bGameOver = true && Input == RestartType)
    {
        ClearScreen();
        SetupGame();

    }

}

void UBullCowCartridge::SetupGame()
{

//Welcome Message To Player.
HiddenWord = TEXT("Money"); // Set the HiddenWord
RestartType = TEXT("Restart");
Lives = HiddenWord.Len(); //Set Lives
bGameOver = false;

PrintLine(TEXT("Welcome to the Letter Game."));
PrintLine(TEXT("Guess the %i letter word, "), HiddenWord.Len()); //Create a random 4 letter word.
PrintLine(TEXT("and press enter."));
PrintLine(TEXT("You have %i lives remaining."), Lives);

}

void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT(“Game Over, please try again”));
PrintLine(TEXT(“Please type %s to replay the game.”), *RestartType);

}

1 Like

Great job with your code!

Thanks! :slight_smile:

Privacy & Terms