My extra feature was to have the game increase in difficulty each time you successfully guess a word (starting with x letter word, then x+1, x+2 … ).
I’ve just come from a different c++ course on udemy and learnt a bit about using the STL. So I used ue4’s TMap and TSet, to create a map.
TMap<int32, TSet<FString>> Words; //in header file
I used a random short story I found on the internet and used a different c++ program I wrote to quickly strip out punctuation, and generate a file with one word per line - I couldn’t quite figure out how to do this within the project, using ue4’s file reading/writing tools.
Then for each FString Word I pulled from the file, I put it into the map using the length of the word as the key, before emplacing it into the appropriate set. This way words were sorted into their appropriate ‘difficulty’, and duplicates were ignored.
FString FileName = FPaths::ProjectDir();
FileName += "Content/wordlist.txt";
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*FileName))
{
return false;
}
else
{
FFileHelper::LoadANSITextFileToStrings(*(FileName), NULL, Result);
for (FString Each : Result)
{
if (Each.Len() >= StartingDifficulty)
{
Each = Each.ToLower(); //otherwise words like Dad were being considered isograms
if (IsIsogram(Each))
{
Words.FindOrAdd(Each.Len()); //make sure that the key exists in the map, otherwise add it
Words[Each.Len()].Emplace(Each); //insert word into set
}
}
}
}
Then for selecting a random word, i used an iterator to select the “randomly chosen” word from the set.
if (Words.Contains(Difficulty))
{
int32 Picker = FMath::RandRange(0,Words[Difficulty].Num()-1);
auto It = Words[Difficulty].CreateIterator();
for (int32 i{0}; i < Picker; i++)
++It; //advance iterator
HiddenWord = *It;
}
If there is a better way to do this, please let me know!