This feature reveals the HiddenWord “Wheel of Fortune” style. If the HiddenWord was “cakes,” it would be revealed as: c####, then c#k##, etc.
I included a new private declaration in the header file to store this:
FString PartiallyRevealedHiddenWord;
I then created this function that accepts the user’s guess w/ a fully-commented version below:
FString UBullCowCartridge::DetermineCorrectlyGuessedLetters(const FString& Guess) const {
FString CorrectlyGuessedLetters = "";
for(int32 i = 0; i < HiddenWord.Len(); i++) {
if(PartiallyRevealedHiddenWord[i] != '#') {
CorrectlyGuessedLetters += PartiallyRevealedHiddenWord[i];
} else if(Guess[i] == HiddenWord[i]) {
CorrectlyGuessedLetters += Guess[i];
} else {
CorrectlyGuessedLetters += "#";
}
}
return CorrectlyGuessedLetters;
}
//returns an FString, accepting a reference to an FString that promises to not be modified, and promises to not mutate any member variables
FString UBullCowCartridge::DetermineCorrectlyGuessedLetters(const FString& Guess) const {
//create an empty FString to build upon
FString CorrectlyGuessedLetters = "";
//for each letter in HiddenWord
for(int32 i = 0; i < HiddenWord.Len(); i++) {
//if the PartiallyRevealedHiddenword at the same index is already revealed, add it.
//This keeps Bulls revealed even if User guesses incorrectly on future tries.
if(PartiallyRevealedHiddenWord[i] != '#') {
CorrectlyGuessedLetters += PartiallyRevealedHiddenWord[i];
//else if the user gets a Bull, add it
} else if(Guess[i] == HiddenWord[i]) {
CorrectlyGuessedLetters += Guess[i];
} else {
//else the user guessed incorrectly and keep the letter hidden
CorrectlyGuessedLetters += "#";
}
}
return CorrectlyGuessedLetters;
}
I chose to reveal the first letter of the hidden word to the player to make the game a bit easier, but I also included a round-system that cranks up the difficulty as the user guesses correctly. Round 1 starts the player with a 4 letter word, then increases by 1 for each round reached, up to a maximum of 5 rounds.