Cash...Code Flow: Early Returns

Here is my code thus far:

// 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();// makes sense to set up game first: Lives and Word ready to go
  
    PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line

}


void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    // Simply testing what the pre and post increments/decrements are doing
    // int32 a = 1;
    // int32 b = ++a; // 2
    // int32 c = ++ ++a;
    // int32 d = a += 2;
    // int32 e = a ++;
    

    // PrintLine(TEXT("%i , %i, %i, %i, %i"), a, b, c, d, e);
    
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
        return;   
    } 
    else // Check Player guess
    {
        ProcessGuess(Input);
        return;
    }
}  


void UBullCowCartridge::SetupGame()
{

    // Welcomeing The Player
    PrintLine(TEXT("Welcome to Bull Cow!"));

    HiddenWord = TEXT("words"); 
    Lives = HiddenWord.Len();
    bGameOver = false;
    
    PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line

    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len()); 
    PrintLine(TEXT("you have %i lives."), Lives);
    PrintLine(TEXT("Type in your guess and \npress enter to moove on..."));
 
}


void UBullCowCartridge::EndGame()
{
    bGameOver = true; 
        PrintLine(TEXT("\nPress Enter to play again..."));
}


void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
        {
            ClearScreen();
            PrintLine(TEXT("You Win!"));
            EndGame(); 
            return;
            // More Words to Complete?
            // If Yes, Print: You Completed this Word --> Ask if Want to continue playing?-- return
                             
            //if No,Print below--return
            
            //Prompt Play again?
            // If Yes SetUpGame--return
            // If No: EndGame--return
        }
    // Isogram?
    // if (IsNotIsogram)
    // {
    //    PrintLine(TEXT("No repeatingletters, guess again"));
    // }
    
    // Prompt to GuessAgain
    // Repeated Letters?
    // Prompt to GuessAgain 
 
    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The hidden word is %i letters long"), HiddenWord.Len());
        PrintLine(TEXT("Sorry! Guess again, you have %i \nlives left"), Lives);
        
        return;
        
    }
    
    PrintLine(TEXT("Lost a Life!"));
    --Lives;

    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left!"));
        PrintLine(TEXT("The hidden world was: %s"), *HiddenWord);
        EndGame();
        return;
    }
    // Show the player BUlls and Cows
    PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
     
    // Check User Input
    // Play Again or Quit?  
   
        // Only one if right now, but if more than one check, then will place more if statements
    //if Character Guess and return
    //if isogram-- return
          
        
      
     
}

The instructor was asking whether our order of checks were proper, and why the order might be important. Taking the latter question first, it would seem appropriate to go from the most universal check and proceed to the more specific ones. If the check fails on broadest of levels, there will be no reason for the code to continue checking. If there were a ton of tiny checks prior to the main one it could slow down the program. What if they all passed, but the last, which should have been first, fail?

So perhaps an order like this will suffice:

  1. Is the word inputted the Hidden word? No? Then wrong…
  2. Does the word match the specified amount of letters? No? Then wrong…
  3. Does the word have any repeating letters? No? then wrong…
    etc.

As you can see, my example answers the first question posed by the instructor, rightly or wrongly.

1 Like

Great thinking process1 Keep this up and you will be a great programmer!