My code thus far

Here’s my code for the Bull Cow game thus far. Hope this helps

// 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();
    StartNewGame();
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    ClearScreen();
    if (!bGameOver)
    {
        //Check input validity. Is it an isogram? Does it have the correct number of characters?
        if (CorrectWordLength(Input, HiddenWord))
        {
            if (IsCorrectAnswer(Input, HiddenWord))
            {
                EndGame(true);
            }
            else
            {
                //Check if player has run out of lives
                //Subtract life
                OnWrongGuess();
            }
        }
        else
        {
            PrintLine(TEXT("Incorrect word length! Please, try again"));
        }
    }
    else
    {
        PlayAgain(Input);
    }
    
}

void UBullCowCartridge::SetupGame()
{
    PrintLine(TEXT("Hello there! Welcome to the BullCow game"));
    PrintLine(TEXT("Please, type \"Start\" to start playing"));
}

void UBullCowCartridge::PrintAvailableLives(int32 _Lives)
{
    UBullCowCartridge::PrintLine(TEXT("You have " + FString::FromInt(_Lives) + " lives"));
}

void UBullCowCartridge::OnWrongGuess()
{
    Lives--;
    if (Lives > 0)
    {
        PrintLine(TEXT("Sorry, but no. Try again"));
        PrintAvailableLives(Lives);
    }
    else
    {
        Lives = 0;
        EndGame(false);
    }
}

void UBullCowCartridge::StartNewGame()
{
    bGameOver = false;
    Lives = 3;
    WordLength = 5; //Set hidden word length
    HiddenWord = TEXT("skate"); //Set HiddenWord
    PrintAvailableLives(Lives);
    PrintLine(FString::Printf(TEXT("The hidden word is %s.\nIt is a %i letter word"), *HiddenWord, HiddenWord.Len()));
    PrintLine(TEXT("Try to guess the " + FString::FromInt(WordLength) + " letter word"));
}

bool UBullCowCartridge::IsCorrectAnswer(FString _Input, FString _Reference)
{
    if (_Input == _Reference)
    {
        return true;
    }
    return false;
}

bool UBullCowCartridge::CorrectWordLength(FString _Input, FString _Reference)
{
    if (_Input.Len() == _Reference.Len())
    {
        return true;
    }
    return false;
}

void UBullCowCartridge::EndGame(bool _bPlayerWon)
{
    if (_bPlayerWon)
    {
        PrintLine(TEXT("You won!"));
    }
    else
    {
        PrintLine(TEXT("You lost!"));
    }
    bGameOver = true;
    PrintPlayAgain();
}

void UBullCowCartridge::PrintPlayAgain()
{
    PrintLine(TEXT("Play again? Y/N\n"));
}

void UBullCowCartridge::PlayAgain(FString _Input)
{   
    if (_Input == "Y")
    {
        StartNewGame();
    }
    else if (_Input == "N")
    {
        //Exit game
    }
    else
    {
        PrintLine(TEXT("Invalid response\n"));
        PrintPlayAgain();
    }
}
3 Likes

You did a great job!

:fire: :fire: :fire:

Privacy & Terms