For my extra, I decided to add visual hints if the letters are in the right place or not, something like Wordle.
Instead of returning the count of Bulls and Cows, I created a function that returns a Hint FString, with a #
for a Bull, a +
for a Cow, and a .
otherwise:
in BullCowCartridge.h
:
FString GetHint(const FString& Guess) const;
in BullCowCartridge.cpp
:
FString UBullCowCartridge::GetHint(const FString& Guess) const
{
FString Hint(TEXT(""));
FString GuessLower = Guess.ToLower();
FString HiddenWordLower = HiddenWord.ToLower();
for (int GuessIndex = 0; GuessIndex < GuessLower.Len(); ++GuessIndex)
{
if (GuessIndex < HiddenWordLower.Len() && GuessLower[GuessIndex] == HiddenWordLower[GuessIndex])
{
Hint += TEXT("#");
continue;
}
bool Found = false;
for (int HiddenIndex = 0; HiddenIndex < HiddenWordLower.Len(); ++HiddenIndex)
{
if (GuessLower[GuessIndex] == HiddenWordLower[HiddenIndex])
{
Hint += TEXT("+");
Found = true;
break;
}
}
if (!Found)
{
Hint += TEXT(".");
}
}
return Hint;
}
and at the end of ProcessGuess
:
const FString Hint = GetHint(Guess);
PrintLine(TEXT("Wrong word! You now have %i lives..."), Lives);
PrintLine(TEXT("Hint: %s\n %s"), *Guess.ToUpper(), *Hint);
That was fun, thanks for the course!