Need help understanding some code in bull cow game

Hey Guys,
I’m in the middle of the bull cow course and I’ve gotten a little lost, maybe you can help me understand.

So this is my code of BullCowCartridge.cpp:
#include “BullCowCartridge.h”

void UBullCowCartridge::BeginPlay() // When the game starts

{

Super::BeginPlay();

SetupGame();

}

void UBullCowCartridge::OnInput(const FString& Guess) // When the player hits enter

{

    if (bGameOver) {

        ClearScreen();

        SetupGame();

        PrintLine(TEXT("Trying again? good luck!"));

    }

    else { //check player guess

    ProcessGuess(Guess);

        }

    }

void UBullCowCartridge::SetupGame() {

HiddenWord = TEXT("Kitty");

Lives = HiddenWord.Len();

bGameOver = false;

PrintLine(TEXT("Hi There!"));

PrintLine(TEXT("Guess the hidden word which is %i letter \nlong.\n"), HiddenWord.Len());

PrintLine(TEXT("You have %i Lives."), Lives);

PrintLine(TEXT("Type it here and press enter!"));

/* const TCHAR HW[] = TEXT("kitty");

PrintLine(TEXT("Character number 1 of the hidden word is: %c"), HiddenWord[2]);

HW;    */



}

void UBullCowCartridge::EndGame() {

bGameOver = true;

PrintLine(TEXT("\nPress enter to play again"));

}

void UBullCowCartridge::ProcessGuess(FString Guess) {

 if (HiddenWord == Guess) {

        PrintLine(TEXT("You Win!"));

        EndGame();

        return;

        }

if (HiddenWord.Len() != Guess.Len()) 

{

    PrintLine(TEXT("Sorry, try guessing again, \nThe hidden word is %i Charachters long"), HiddenWord.Len());

    PrintLine(TEXT(" you have %i lives remaining."), Lives);

    return;

}

    

if (!IsIsogram(Guess)) {

    PrintLine(TEXT("No repeating letters, guess again."));

    return;

}

   

        PrintLine(TEXT("lost a life!"));

        --Lives;

        if (Lives <= 0) {

                ClearScreen();

                PrintLine(TEXT("You have no lives left, you lose."));

                PrintLine(TEXT("The hidden word was: %s "), *HiddenWord);

                EndGame();

                return;

            }

        

        PrintLine(TEXT("Guess again, you have %i lives left"), Lives);

}

bool UBullCowCartridge::IsIsogram(FString Word) const {

    int32 Index = 0;

    int32 Comparison = Index + 1;

    for (; Comparison < Word.Len(); Comparison++)

    {

        if (Word[Index] == Word[Comparison]) {

            return false;

        }

    }

    

    return true;

   /* for (int32 Index = 0; Index < Word.Len(); Index++) {

        PrintLine(TEXT("%c"), Word[Index]);

    }

    */

 /*  for each letter

    start at [0]

    Compare against the next letter

    until we reach [Word.len() -1]

    if any letters are the same return

*/

}

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) const;

// Your declarations go below!

private:

FString HiddenWord;

int32 Lives;

bool bGameOver;

};

This code does work according to lesson “checking parameters 1”
I just can’t figure out how does the user input is being used both with the word “Guess” and the word “Word”, how does the value being directed into these FStrings?

Please help! :frowning:

In the future could you please format your code correctly using the preformatted text option marked by </> in the toolbar.


By being passed it’s value. It’s no different to how things were done in the previous section.

bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);
 //..
}
int main()
{
    int LevelDifficulty = 1;
    //..
    PlayGame(LevelDifficulty);
}

Here the value of LevelDifficulty is being passed to PlayGame and then Difficulty is passed down to PrintIntro.

And it’s no different in this case with ProcessGuess and IsIsogram

The ProcessGuess function and the IsIsogram function are called and passed data called parameters. These parameters (“Guess” and “Word”) act as variables to be used in the function. While the naming of these parameters usually don’t matter, it’s sensible to use relevant names.

I like to think of parameters as tiny containers for the functions. The ProcessGuess function is called using the Guess parameter data (which was provided by the player). Then the IsIsogram function was called using the Word parameter data (which was provided by the Guess parameter) .

In a latter lesson, you have to check if any of the words on a word list are isograms. You could make a function called CheckWords with a parameter “CurrentWord” that uses the IsIsogram function; the Word parameter data (for IsIsogram) would then be provided by this CurrentWord parameter.

Thanks guys, and sorry for not using the right format for the code.
Sorry if i’m making dumb questions here but where in my code is IsIsogram getting the value?
I can see the process of passing value in your code DanM, but i can’t find it in mine, in fact, IsIsogram appears in only 3 instances so far, the header file declaration, the function declaration in the end of the code and the if statements in the ProcessGuess function, is that it? does the IsIsogram Function get the value through ProcessGuess?

Thank again to both of you!

Yes.

void UBullCowCartridge::ProcessGuess(FString Guess) {
    //...
    if (!IsIsogram(Guess)) {

Here it’s being passed ProcesGuess's parameter Guess .

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms