Extra Credit - Clue for a life

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:

  1. Takes in the “HiddenWord” and counts it’s length.
  2. Finds a random number within that length.
  3. If the random number is different from the “HiddenWord” string number add " - " to the string “FinalClue”.
  4. If the random number is the same as the “HiddenWord” string number add the matching letter.
  5. 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:

  1. If user enters “c”
  2. Remove a life
  3. Get “FinalClue” from Clue function.
  4. 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!

Privacy & Terms