My BullCowCartridge.cpp so far

I’m a little over 1/2 of the way through Bulls and Cows, and I have a slightly different approach to the logic.

I’m pretty good at programming now that I’ve been doing it at school for a while, so this is more of a review of C++ for me and an intro to Unreal, but still very valuable to go through these earlier lessons. I’m gonna master C++ soon!

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

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    PrintLine(TEXT("Hello! and welcome to Bulls and Cows!"));

    InitGame();
}

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

    if (!bWon && Lives > 0)
    {
        if (Input == HiddenWord)
        {
            bWon = true;
            PrintLine(TEXT("You win!!!"));
        }
        else if (CheckIsogram(Input) && CheckLength(Input))
        {
            Lives--;
            if (Lives > 0)
            {
                GiveBullCowInfo(Input);
                PrintLine(TEXT("You have %i Lives remaining."), Lives);
                PrintLine(TEXT("Guess again."));
            }
            else
            {
                PrintLine(TEXT("You lose!!! Boo!"));
            }
        }
        else
        {
            PrintLine(TEXT("Don't worry, you haven't lost a life...\nthis time."));
        }
    }
    else
    {
        EndGame();
    }
}

void UBullCowCartridge::InitGame()
{
    HiddenWord = TEXT("wordy");
    Lives = 4;
    bWon = false;

    /* Using Printf is the standard way to format FStrings, as shown here. In other functions,
     * it is omitted because PrintLine is overridden in Cartridge.h to include Printf functionality. */
    PrintLine(FString::Printf(TEXT("Please enter a %i-letter word as your first guess, and press enter."), HiddenWord.Len()));
    PrintLine(FString::Printf(TEXT("(The word is '%s'.)"), *HiddenWord));  // Debug line
}

void UBullCowCartridge::EndGame()
{
    PrintLine(TEXT("Restart the game to play again, ya fool!"));
    // Maybe restart the game with InitGame() again.
}

bool UBullCowCartridge::CheckIsogram(const FString& InputString)
{
    bool isIsogram = true;
    if (isIsogram)
    {
        return true;
    }
    else
    {
        PrintLine(TEXT("That is not an isogram."));
        return false;
    }
}

bool UBullCowCartridge::CheckLength(const FString& InputString)
{
    if (InputString.Len() == HiddenWord.Len())
    {
        return true;
    }
    else
    {
        PrintLine(TEXT("Please enter a word of length %i."), HiddenWord.Len());
        return false;
    }
}

void UBullCowCartridge::GiveBullCowInfo(const FString& InputString)
{
    PrintLine(TEXT("...Dummy Bull Info..."));
}

1 Like

I agree you will master C++ ! I also want to master C++ and Unreal! Let’s do it together man!

Privacy & Terms