The code of chaos! Not really, I like to think I'm organized

I DID IT! Although there are spoilers for future lessons that I’m sure other’s haven’t seen at this point,
I just know about iterators from previous coding experience.

Also yes, I do have just an open else if statement. I know it will be used at some point.

// 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(TEXT("The HiddenWord is %s"), *HiddenWord);           //Debug Line
    // PrintLine(TEXT("It is %i characters Long"), HiddenWord.Len()); //Debug Line

    WelcomePlayer();

}

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

    // If Game is over, ClearScreen() and SetupGame()
    if (bGameOver) {
        EndGame();
        return; // Using a return here to exit early instead of using an else
    }

    /* 
        Normal Game Loop
        Check if Valid
        Check If Character Count is correct
    */
    if (HiddenWord.Len() != Input.Len())
    {
        // Prompt to GuessAgain
        PrintLine(TEXT("The Hidden Word is %i characters long, try again!"), HiddenWord.Len());
    }
    // Check if Isogram
    else if (false)
    {

    }
    else
    {
        // This code is so chaotic, there should be a simpler way to do this tree
        // I'll put everything in functions later I think as part of the refactoring process
        if (Input == HiddenWord)
        {
            PrintLine(TEXT("The Answer is %s"), *HiddenWord);
            EndGame();
        }
        else
        {
            if (Lives == 0) {
                // If no lives, inform player they have perished
                EndGame();
            }
            else if (Lives == 1)
            {
                // Warn them about how close they are to dying
                PrintLine(TEXT("YOU HAVE ONE YOUR FINAL LIFE!"));
                PrintLine(TEXT("YOU WILL PERISH IF YOU FAIL AGAIN"));
            }
            else
            {   // Are lives > 0
                // If yes, Show Lives left
                // Prompt user for another guess
                PrintLine(TEXT("YOU MUST TRY AGAIN OR PERISH!"));
                // Remove Life
                PrintLine(TEXT("You have %i lives left"), Lives--);
            }
        }
    }
    // Prompt to QuessAgain

    // How Many Bulls
    // How Many Cows

    // Press Enter to PlayAgain and ESC to QUit
    // If so, set lives and new Hidden Word
}

void UBullCowCartridge::SetUpGame()
{
    HiddenWord = TEXT("Prodigen");
    Lives = 7;
    bGameOver = false;
}

void UBullCowCartridge::WelcomePlayer()
{
    // Separating these may make it easier to simplify dialogue later
    PrintLine(TEXT("Welcome to Bull Cows!"));
    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len()); // REMOVE MAGIC NUMBER!!!
    PrintLine(TEXT("Please enter your guess \nPress enter to continue"));
}

void UBullCowCartridge::EndGame()
{

    // If They've confirmed that they want to replay the game
    // Then let re-initialize the game
    // This goes first so I can put a return statement here

    if (bGameOver) {
        // This is here because I feel like the EndGame function,
        // Should handle the events that occur EndGame()
        SetUpGame();
        WelcomePlayer();
        return; // Also using a return here to exit early to keep the tree shorter
    } else {
        // This should prevent an infinite loop
        bGameOver = true;
    }

    // Extra shame to those that have failed
    // Also show user the hidden word for some closure
    if(Lives == 0) {
        PrintLine(TEXT("THEN PERISH"));
        PrintLine(TEXT("The hidden word was %s\n"), *HiddenWord);
    }

    // Play Nice now
    PrintLine(TEXT("Press ente to play again..."));
}
1 Like

Privacy & Terms