Personal Show Off Thread

Hi there!
Been using the Questions Section over at Udemy to show off the work I’ve done along the course and will be using this thread in order to keep a record on the stuff I do. Feedback will be gladly appreciated, and thank you for stopping by :raised_hands:

Currently on Section 3: Coding the basic Game Loop

This is what my code looks like:

BullCowCartridge.h
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
    PrintLine(TEXT("Welcome to the Bulls & Cows guessing game!"));
    PrintLine(TEXT("Press TAB to access the terminal."));
    PrintLine(TEXT("Guess the word and press ENTER to see if you were right!"));

}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    ClearScreen();
    FString HiddenWord = TEXT("Tongue");
    PrintLine(Input);
    if (Input == HiddenWord)
    {
        PrintLine(TEXT("You got the word!"));
    }
    else
    {
        PrintLine(TEXT("Wrong word!"));
    }
}

As said in the course video, we are initializing the code each time we press enter, so we need to move it to BeginPlay() or store it somewhere else so we can change it after playing the game, and also needs to be readable inside the OnInput() function.

Full Game Loop Flowchart

Summary

Pseudo Code:

Summary
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    HiddenWord = TEXT("Tongue");
    //intialize Lives = 5;


    PrintLine(TEXT("Welcome to the Bulls & Cows guessing game!"));
    PrintLine(TEXT("Press TAB to access the terminal."));
    PrintLine(TEXT("Guess the word and press ENTER to see if you were right!"));

}

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

    //Get Length of Input
    //Check if isogram

    if (Input == HiddenWord)
    {
        PrintLine(TEXT("You got the word!"));
    }
    else
    {
        PrintLine(TEXT("Wrong word!"));
        // Check if Lives > 0
        // 
        // If true, decrease lives
        // Print remaining Lives
        // Ask for next guess
        // 
        // else
        // ask to play again
        // 
        // if YES
        // Print Welcome Message
        // 
        // if no
        //  HiddenWord was PrintLine (HiddenWord);
        //  Print Game Over Message
}

First Function:
.h

Summary
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	void InitGame();

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

.cpp

Summary
void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();


    InitGame();

    PrintLine(TEXT("Welcome to the Bulls & Cows guessing game!"));
    PrintLine(TEXT("Press TAB to access the terminal."));
    PrintLine(TEXT("Guess the word and press ENTER to see if you were right!"));

}

void UBullCowCartridge::InitGame()
{
    HiddenWord = TEXT("Tongue");
    Lives = 5;
}

Nested if:

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

    //Get Length of Input
    //Check if isogram

    if (Input == HiddenWord)
    {
        PrintLine(TEXT("You got the word!"));
    }
    else if (Input.Len() != HiddenWord.Len())
    {
     PrintLine(TEXT("The Hidden Word is 5 characters long, but don't tell anyone..."));
     
     PrintLine(TEXT("Wrong word!"));
     // Check if Lives > 0
     // 
     // If true, decrease lives
     // Print remaining Lives
     // Ask for next guess
     // 
     // else
     // ask to play again
     // 
     // if YES
     // Print Welcome Message
     // 
     // if no
     //  HiddenWord was PrintLine (HiddenWord);
     //  Print Game Over Message
    }
}

Full .cpp with format specifiers for strings and ints.

Summary
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    SetupGame();

    PrintLine(FString::Printf(TEXT("The HiddenWord is: %s."), *HiddenWord));

    PrintLine(TEXT("Welcome to the Bulls & Cows guessing game!"));
    PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());
    PrintLine(TEXT("\nPress TAB to access the terminal."));
    PrintLine(TEXT("Guess the word and press ENTER to see if you were right!"));

}

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

    //Get Length of Input
    //Check if isogram

    if (Input == HiddenWord)
    {
        PrintLine(TEXT("You got the word!"));
    }

    else if (Input.Len() != HiddenWord.Len())
    {
     PrintLine(TEXT("The Hidden Word is %i characters long, but don't tell anyone..."), HiddenWord.Len());
     
     PrintLine(TEXT("Wrong word!"));

     // Check if Lives > 0
     // 
     // If true, decrease lives
     // Print remaining Lives
     // Ask for next guess
     // 
     // else
     // ask to play again
     // 
     // if YES
     // Print Welcome Message
     // 
     // if no
     //  HiddenWord was PrintLine (HiddenWord);
     //  Print Game Over Message
    }
}

void UBullCowCartridge::SetupGame()
{
    HiddenWord = TEXT("Tongue");
    Lives = 5;
}

Early Returns Code Refactor Assignment:
Only change I would make is I’d move the Lenght check before the isogram one. So we only punish when they get an isogram but it’s not the one we are looking for (which the code was already doing, but I prefer checking if the length is wrong first so we can stop the flow of the code sooner).

Summary
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    SetupGame();

    PrintLine(FString::Printf(TEXT("The HiddenWord is: %s."), *HiddenWord));
}

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

void UBullCowCartridge::SetupGame()
{
    HiddenWord = TEXT("Tongue");
    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine(TEXT("Welcome to the Bulls & Cows guessing game!"));
    PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());
    PrintLine(TEXT("You have %i lives."), Lives);
    PrintLine(TEXT("Guess the word and press ENTER \nto see if you were right!"));
}
void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("\nPress Enter to play again."));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You got the word!"));
        EndGame();
        return;
    }

    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("Remember, the hidden word is %i letters long."), HiddenWord.Len());
        PrintLine(TEXT("Wrong Guess! \nYou have %i more guesses"), Lives);
        return;
    }

    //if (!IsIsogram)
    //{
    //    PrintLine(TEXT("There must not be repeating letters."));
    //}

    PrintLine(TEXT("You lose one life"));
    --Lives;

    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have run out of guesses."));
        PrintLine(TEXT(" The Hidden Word was: %s"), *HiddenWord);
        EndGame();
        return;
    }
    PrintLine(TEXT("Guess again!"));
}

Took a break!
Back to it with “for loops”.

Summary
bool UBullCowCartridge::IsIsogram(FString Word) const
{
    
    for (int32 i=0; i<=Word.Len();i++)
    {
        PrintLine(TEXT("%c"), Word[i]);
    }

    for (int32 i = 0; i <= HiddenWord.Len(); i++)
    {
        PrintLine(TEXT("%c"), HiddenWord[i]);
    }
    //get first char of HiddenWord
    //check first char against the other ones until the length of word
    //if char is equal to other one
    // return false;
    //
    //else return true;
    return true;
}

Privacy & Terms