SetupGame with RandRange()

This is my implementation of SetupGame() using RandRange(). I used SRandInit() to initialise the RNG seed similar to the TripleX game previously. I couldn’t find a UE-specific method of initialising the seed so used time(0) as before and had to include <time.h> . It seemed to work.

void UBullCowCartridge::SetupGame() // Initialise the game state
{
    // initialise class variables
    
    FMath::SRandInit(time(0));
    TArray<FString>ValidWords = GetValidWords(Words);

    // Assign a random word from the valid word list to the hidden word
    HiddenWord = ValidWords[FMath::RandRange(0, ValidWords.Num() - 1)];

    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine(TEXT("Welcome to the Bull Cows Game."));
    PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());
    PrintLine(TEXT("You have %i lives."), Lives);
    PrintLine(TEXT("Type in your guesss and press ENTER."));
    PrintLine(TEXT("The hidden word is %s"), *HiddenWord);
}

Privacy & Terms