Challenge Code

I got a little wild and added levels and lives into the mix, which complicated things a bit. Not sure my conditions nested inside of nested conditions situation is really goals, but it does technically work. I made a function called LevelClue() because I envision the prompt to guess changing with the HiddenWord as we progress to the end level eventually.

If at max level (5), game over prints a winning message.
If at 0 lives, game over prints a losing message with the correct answer.
Pressing Enter at game over clears the screen, resets the game, and prints the welcome message again.

// 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 Hidden Word is: %s"), *HiddenWord); // Debug Line
}

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

    if (bGameOver)
    {
        ClearScreen();
        SetUpGame();  
    }
    else // Checking player input
    {
        if (Input == HiddenWord && Level < 5) 
        {
            PrintLine(TEXT("Yup!"));
            ++Level;
            Lives = HiddenWord.Len();
            LevelClue();
        }
        else if (Input == HiddenWord && Level >= 5)
        {
            EndGame();
        }
        else if (Input == "Yup" && Level == 0)
        {
            ClearScreen(); 
            ++Level; 
            bGameOver = false;
            LevelClue();
        }
        else if (Input == "Nope")
        {
            PrintLine(TEXT("How to play:\n  Guess the correct isogram to win.\n"
            "  An isogram is a word in which the\n"
            "    letters occur an equal number\n"
            "    of times.\n"
            "  To play, simply type your guess\n"
            "    and press ENTER.\n\n"
            "  Ready to play?\n"
            "  Type \"Yup\" to begin."));
        }
        else
        {
            PrintLine(TEXT("Nope."));
            --Lives;
            if (Lives <= 0)
            {
                bGameOver = true;
            }

            if (Input.Len() == HiddenWord.Len())
            {
                PrintLine(TEXT("Correct Word Length.\n"));
            }
            else 
            {
                PrintLine(TEXT("Incorrect word Length.\n"));
            }     

            LevelClue();  
        }
        // check if Input is isogram
        // if not isogram inform player of error
    }
}

void UBullCowCartridge::SetUpGame()
{   
    PrintLine(TEXT("Yowdy Baws!\nYou ever played this here Bull Cows game?"));
    PrintLine(TEXT("\nLeft Click mouse\nPress TAB\nType \"Yup\" or \"Nope\"\nPress ENTER"));
    HiddenWord = TEXT("Plastic"); 
    Lives = HiddenWord.Len();
    Level = 0;
}

void UBullCowCartridge::LevelClue()
{
    if (bGameOver == false)
    {
        PrintLine(TEXT("\nLEVEL: %i"), Level);
        PrintLine(TEXT("Let's play!\nCLUE: %i letter word."), HiddenWord.Len()); 
        PrintLine(TEXT("LIVES: %i"), Lives);
    }
    else
    {
        EndGame();
    }
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    ClearScreen();
    if (Lives == 0)
    {
        PrintLine(TEXT("Yuh lose.\nThe word was: %s\n\nLasso up the ENTER\nkey to play again."), *HiddenWord);
    }
    else if (Level >= 5)
    {
        PrintLine(TEXT("WINNER!\n\nLasso up the ENTER key\n to play again."));
    }
    else
    {
        SetUpGame();
    }
}
1 Like

I love this code! Great job getting wild with your code :joy:

It caused a bit of a headache to implement the randomized hidden word in lecture 77 LOL. Debugging was… a journey.

This is what I could come up with. It’s a bit different but not really

Privacy & Terms