For my extra credit, I created a function which gives the user a bull, for the price of a life.
The function I created for this is called Clue and it does the following:
- Takes in the “HiddenWord” and counts it’s length.
- Finds a random number within that length.
- If the random number is different from the “HiddenWord” string number add " - " to the string “FinalClue”.
- If the random number is the same as the “HiddenWord” string number add the matching letter.
- Return “FinalClue”.
FString UBullCowCartridge::Clue(const FString& HiddenWord) const
{
int32 RandLetter = FMath::RandRange(0, HiddenWord.Len()-1);
// PrintLine(TEXT("DEBUG: RandLetter: %i"), RandLetter);
FString FinalClue;
for (int32 HiddenWordSize = 0; HiddenWordSize <= HiddenWord.Len()-1; HiddenWordSize++)
{
if (RandLetter == HiddenWordSize)
{
FinalClue += HiddenWord[HiddenWordSize];
// FinalClue += "-";
// PrintLine(TEXT("DEBUG: HiddenWordSize: %i"), HiddenWordSize);
}
else FinalClue += "-";
}
return FinalClue;
}
Then within the “Check Guess” function I added a new If statement which performs the following:
- If user enters “c”
- Remove a life
- Get “FinalClue” from Clue function.
- Print the “FinalClue” on the screen.
if (Guess == "c")
{
// ClearScreen();
Lives--;
FString TheFinalClue = Clue(HiddenWord);
PrintLine(TEXT("Here is a clue %s"), *TheFinalClue);
return;
}
Just sharing, but suggestions on how I can improve always welcome!