Early returns

why I think we have to order the checks

example: if the player should not lose a life on guessing a word that is an isogram so if we put the statement that says to if we don’t get the hidden word right then decrement a life he will lose a life before the check for isogram starts so he loses a life for no reason

I finally (after a long time) understood what the early return is for. (I guess)

I have some insecurities still about writing code, I’m not 100% confident on if it’s properly wrote or if it’s garbage.

I wanted to post my work this far and maybe get some feedback to know if it’s on the right track or is there something I could improve.

Thanks a lot!

// 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();// Setup the game
    
    // Debug Line
    //PrintLine(TEXT("The hidden word is: %s. \nIt is %i character long"), *HiddenWord, HiddenWord.Len());
    

    
    // Prompt player for guess 
}

void UBullCowCartridge::SetupGame()
{   
    //Set Variables
    HiddenWord = TEXT("Cake");
    Lives = HiddenWord.Len();
    bGameOver = false;
    
    // Welcome player
    PrintLine(TEXT("Welcome to Bulls and Cows!"));
    PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());
    PrintLine(TEXT("Type in your guess and press enter \nto continue..."));
}

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

    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else // Checking player guess
    {
        ProcessGuess(Input);
    }
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{

    //Check if no letters are typed
    if (Guess.Len() == 0)
    {
        PrintLine(TEXT("Please insert an Isogram"));
        PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len()); 
        return;
    }       
    
    // Check if Length and Word are the same
    if (HiddenWord.Len() != Guess.Len())
    {
        ClearScreen();
        PrintLine(TEXT("The Hidden Word has %i letter."), HiddenWord.Len());
        Damage();
        return;
    }
    
    if (HiddenWord != Guess)
    {
        ClearScreen();
        PrintLine(TEXT("%s doesn't match with the Hidden Word"), *Guess);
        Damage();
        return;
    }
    
    //Check is if Is Isogram
    /* if (IsIsogram)
    {
        PrintLine(TEXT("No repeating letters, guess again"));
    } */
    
    // Check is HiddenWord == Guess **** WIN ******
    if (HiddenWord == Guess)
    {
        Win();
        return;
    } 
}

void UBullCowCartridge::Damage()
{
    --Lives;
    PrintLine(TEXT("Current Lives: %i. \nTry Again"), Lives);
    
    if (Lives <= 0)
    {
        Lose();
        return;
    }
    // Show player Bulls and Cows
    PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
    
}

void UBullCowCartridge::Lose()
{
    bGameOver = true;
    ClearScreen();
    PrintLine(TEXT("You are out of lives!"));
    PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);    
    PrintLine(TEXT("\nPress Enter to Play again"));
}

void UBullCowCartridge::Win()
{
    ClearScreen();
    PrintLine(TEXT("You won!!!"));
    PrintLine(TEXT("\nPress Enter to Play again"));
    bGameOver = true;
}

Privacy & Terms