Formatting strings and a debugging mode

I’ve formatted the string as part of the challenge and also added a bool for a debugging mode.

Header file:

// 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;

	private:
	void DisplayDebugging();
	void DisplayStats();
	void InitGame();
	void ProcessInput(const FString& Input);
	void Welcome();

	// Your declarations go below!
	private:
	bool DebugMode;
	FString HiddenWord;
	int32 Lives;	
};

CPP file:

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

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    InitGame();
    if (DebugMode) { DisplayDebugging(); }
    DisplayStats();
    Welcome();    
}

void UBullCowCartridge::DisplayDebugging()
{
    PrintLine(TEXT("Hidden word: %s"), *HiddenWord);
}

void UBullCowCartridge::DisplayStats()
{
    PrintLine(TEXT("Lives: %i"), Lives);
    PrintLine(TEXT("Hidden word length: %i"), HiddenWord.Len());
}

void UBullCowCartridge::InitGame()
{
    DebugMode = true;
    HiddenWord = TEXT("keyboard");
    Lives = HiddenWord.Len();
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    ClearScreen();
    if (DebugMode) { DisplayDebugging(); }
    DisplayStats();
    ProcessInput(Input);    
}

void UBullCowCartridge::ProcessInput(const FString& Input)
{
    if (Input == HiddenWord)
    {
        PrintLine(TEXT("Congratulations you found the\nhidden word!"));
    }
    else
    {
        if (Input.Len() != HiddenWord.Len())
        {
            PrintLine(TEXT("That's not the same length as the hidden word."));
        }
        PrintLine(TEXT("That's not the hidden word, guess again."));
    }
}

void UBullCowCartridge::Welcome()
{
    PrintLine(TEXT("Welcome to Bulls & Cows"));
    PrintLine(TEXT("Enter input and press enter"));
}


1 Like

Phenomenal job :star:

1 Like

Privacy & Terms