My code so far

Changed the code a little bit… Life won’t get deducted when we put wrong number of letters. If all lives are lost then hidden word will be revealed.

// 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 Hidden Word is: %s"), *HiddenWord);   
}

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

{   if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else
    {
        if (Input == HiddenWord) 
        {          
        PrintLine(TEXT("You Win")); //checking input guess
        EndGame();
        }
        else 
        {
            if (Input.Len() != HiddenWord.Len()) 
            {
                PrintLine(TEXT("Please, Enter %i letter word"), HiddenWord.Len()); //check for number of characters
            }
            else
            {
                --Lives;
                PrintLine(TEXT("You have lost a life"));            

                if (Lives <= 0)  //check for lives > 0
                {
                    PrintLine(TEXT("Hidden Word was: %s"), *HiddenWord);
                    EndGame();
                }
                else
                {
                    PrintLine(TEXT("Guess Again. \nLives Left: %i"), Lives);
                } 
            }             
        }
    }  
}

void UBullCowCartridge::SetupGame()
{   
    PrintLine(TEXT("Hello There, Welcome to the BULL COW game"));
    HiddenWord = TEXT("Bake"); //Hidden Word
    Lives = HiddenWord.Len();
    bGameOver = false;    

    PrintLine(TEXT("Guess the %i letter word and then \npress ENTER to continue..."), HiddenWord.Len());  //prompt for guess
    PrintLine(TEXT("Lives Left: %i"), Lives);
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("Press ENTER to Play Again."));
}
1 Like

Amazing looking code!

Privacy & Terms