When I follow this coding video, I got compile error about Bulls and Cows variable initailize.
This is my solution.
- I declare Bulls and Cows variable at header file.
- I remove “BullCount” and “CowCount” from the function
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(const FString& Guess);
bool IsIsogram(const FString& Word) const;
TArray<FString> GetValidWords(const TArray<FString>& Words) const;
void GetBullCows(const FString& Guess);
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
**int32 Bulls, Cows;**
};
- At ProcessGuest function, I change it to this code
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if(Guess == HiddenWord)
{
PrintLine(TEXT("You have won!"));
EndGame();
return;
}
if(Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The hidden word is %i letter long"), HiddenWord.Len());
PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining"),Lives);
return;
}
// Check If Isogram
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters, guess again"));
return;
}
// Remove Life
PrintLine(TEXT("Lost a life!"));
--Lives;
if(Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left!"));
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
EndGame();
return;
}
**//Show the player Bull and Cows**
** GetBullCows(Guess);**
** PrintLine(TEXT("You have %i Bulls and %i Cows"),Bulls,Cows);**
PrintLine(TEXT("Guess again, you have %i lives left"),Lives);
}
- At “GetBullCows” function, I use Bulls and Cows instread of BullCount and CowCount
void UBullCowCartridge::GetBullCows(const FString& Guess)
{
Bulls = 0;
Cows = 0;
// for every index Guess is same as index Hidden, BullCount++
// if not a bull was it a cow? if yes CowCount++
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
{
if (Guess[GuessIndex] == HiddenWord[GuessIndex])
{
Bulls++;
continue;
}
for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
{
if (Guess[GuessIndex] == HiddenWord[GuessIndex])
{
Cows++;
}
}
}
}
And It work.
Why before I self edit this game. It has compile error?
Remark: I cannot bold or hilight the code I change
It show “** … **” in the code I posted.