BullCowGame implementing Early Returns

I was getting a bit confused using psuedo code and commenting lines, so I ended up making functions that print what they should be doing to fill the gaps.

BullCowCartridge.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	void SetupGame();
	void EndGame();
	void MinusLife();
	void ProcessGuess(FString Guess);
	bool CheckIsogram(FString Word2Check);
	void BullCowculator(FString Word3Check);

	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives, AttemptWarning;
	bool bGameOver;
	
};

BullCowCartridge.cpp

// 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(FString::Printf(TEXT("Hidden word is: %s. "), *HiddenWord)); // Debug Line
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{  
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else
    {
        ProcessGuess(Input);
    }
}

void UBullCowCartridge::SetupGame()
{
    PrintLine(TEXT("***Bull Cows Game*** "));

    HiddenWord = TEXT("aspired");
    Lives = HiddenWord.Len();
    AttemptWarning = 0;
    bGameOver = false;

    PrintLine(TEXT("Guess the %i letter isogram: "), HiddenWord.Len()); //Prompt player for guess
    PrintLine(TEXT("You have %i lives. "), Lives);
    PrintLine(TEXT("Type your guess \nPress enter to continue... ")); 
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("\n*** Game Over ***. "));
    PrintLine(TEXT("Press enter to play again. "));
    // PrintLine(TEXT("Press something to quit. "));
}

void UBullCowCartridge::MinusLife()
{
    if (Lives > 0)
    {
        PrintLine(TEXT("You have %i lives remaining. "), --Lives);
        if (Lives == 0)
        {
            PrintLine(TEXT("Last try! "));
        }
        AttemptWarning = 0;
    }
    else
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives remaining. "));
        AttemptWarning = 0;
        PrintLine(TEXT("\nYou Lose!"));
        EndGame(); // Losing EndGame
    }

}

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        ClearScreen();
        PrintLine(TEXT("You win!"));
        EndGame(); // Winning EndGame
        return;
    }
      
    if (Guess.Len() != HiddenWord.Len()) //Checking if guess is not valid length
    { 
        if (AttemptWarning++ < 2) // Tracking and advising player's invalid attempts
        {
            PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
            PrintLine(TEXT("The hidden word is %i characters long. "), HiddenWord.Len());
            if (Guess.Len() < HiddenWord.Len())
            {
                PrintLine(TEXT("Your guess needs to be longer. "));
                return;
            }
            else
            {
                PrintLine(TEXT("Your guess needs to be shorter. "));
                return;
            }
        }

        // Invalid attempt punishment
        if (Lives > 0)
        {
            PrintLine(TEXT("Guessing carelessly lost you a life. "));
            MinusLife();
            return;
        }
        else
        {
            PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
            MinusLife();
            return;
        } 
    }

    if (!CheckIsogram(Guess)) //Checking if guess is not an isogram
    {
        if (AttemptWarning++ < 2) // Tracking and advising player's invalid attempts
        {
            PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
            PrintLine(TEXT("Your guess was not an isogram."));
            return;
            
        } 
        if (Lives > 0)
        {
            PrintLine(TEXT("*Note* An isogram is a word with no repeating letters."));
            MinusLife();
            return;
        }
        else
        {
            PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
            MinusLife();
            return;
        }  

        BullCowculator(Guess);        
    }                
    
}

bool UBullCowCartridge::CheckIsogram(FString Word2Check)
{
    PrintLine(TEXT("checking if %s is an isogram. "), *Word2Check); //Debug ling
    return false; // Implement isogram checking code and return appropriate true / false
}

void UBullCowCartridge::BullCowculator(FString Word3Check)
{
    PrintLine(TEXT("The bulls and cows for %s are: "), *Word3Check); // Debug line
    // implement bull cow checking based on argument
}

Until I implement a way to actually check if the player’s guess is an Isogram, you can manually change return value for CheckIsogram(); to see how the code would run if the check were to either return true or false.

1 Like

How are you feeling about C++?

As the first coding language I’m really learning to use, it seems pretty good. Everything I’ve learned so far seems to make sense and it feels like each new thing opens up what you can actually make the code do by a lot.

Privacy & Terms