Bull an cow code so far

All this is the .cpp file

// 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
{

    // if the game is over do ClearScreen() and SetupGame() the game
    
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else // checking players guess
    {
        ProcessGuess(Input);
    }

}

void UBullCowCartridge::SetupGame()
{
    //welcoming the player
    PrintLine (TEXT("Hi there, welcome to the Bull&Cow game!"));

    HiddenWord = TEXT("Cakes");
    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine (TEXT("You have %i lives to"), Lives);
    PrintLine (TEXT("Guess the %i letter word!"), HiddenWord.Len()); 
    PrintLine (TEXT("Enter your guess \n press enter to continue..."));

    // const TCHAR HW[] = TEXT("cakes");
    // PrintLine(TEXT("Character 1 of the hiddenword is: %c"), HW[0]);
}

void UBullCowCartridge::EndGame()

{
    bGameOver = true;
    PrintLine(TEXT("\nPress enter to restart..."));

    // message at screen saying if he wants to play again
    // check user input
    // if yes TryAgain [A] Quit the game
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    
    
    if(Guess == HiddenWord)
    {
        ClearScreen();

        PrintLine(TEXT("GOOD JOB!"));
        EndGame();
        return;
    }
    
    // remove a life
    --Lives;

    // Check if the number of lives ( x > 0 ) GuessAgain
    if(Lives == 0) 
    {
        PrintLine(TEXT("You have no lives left"));
        PrintLine(TEXT("The hiddenword was %s"), *HiddenWord);
        EndGame();
        return;
    }

    //check the right number of characters
    if(Guess.Len() != HiddenWord.Len()) // if yes guess again
    {
        ClearScreen();
        PrintLine(TEXT("Oops, the word is not %i characters long"),Guess.Len());
        PrintLine(TEXT("Try guessing again, %i lifes left"), Lives);
        return;
    }
    //check IsIsogram(reapting letters)
    if (!IsIsogram(Guess))
    {


        PrintLine(TEXT("No reapeting letters!, guess again"));
        return;
    }
    
    ClearScreen();
    PrintLine(TEXT("Oops, just lost a life..."));   
    PrintLine(TEXT("Try guessing again, %i lifes left"), Lives);
}

bool UBullCowCartridge::IsIsogram(FString Word)
{
    // For each letter 
    // start at element [0].
    // comapre against the next letter.
    // until we reach [Word.Len() -1].
    // if any are the same return false.

    return true;
}

And this is the header file

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	void SetupGame();
	void EndGame();
	void ProcessGuess(FString Guess);
	bool IsIsogram(FString Word);

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

it works perfectly fine for me, but i really want to know if my code is in good “shape”. Thanks :grinning:
i am curently in lecture 64

2 Likes

It looks like it is in good shape for me.

Privacy & Terms