Need help with maps-Trying to map words to random number generator

I Still dont fully understand how to use maps. If someone can explain to me how to randomly select words from a map i would really appreciate it. heres what im working on.

void FBullCowGame::Reset()//TODO make more RICH return value
{
    MyCurrentTry = 1;
    MyMaxTries = MAX_TRIES;
    
    //words for hidden guess
    TMAP <int32, FString> HiddenWordFromMap
    {
	{1, "power" },
	{2,"stick"},
	{3,"comedy"},
	{ 4,"wizard" },
	{ 5,"magic" },
	{ 6,"house" },
	{ 7,"eight" },
	{ 8,"sharp" },
	{ 9,"sight" },
	{ 10,"knight" },
	{ 11,"solar" },
	{ 12,"vertical" },
	{ 13,"desktop" },
	{ 14,"terminal" },
	{ 15,"symbolic" },
	{ 16,"storage" }
    };

    //Not sure how to get the random number to select a word
    FString HIDDEN_WORD = HWords[WordRandomizer()];
    MyHiddenWord = HIDDEN_WORD;
}


//returns random number
int32 FBullCowGame::WordRandomizer()const
{
    srand((unsigned int) time(NULL));
    int32 WordToChoose = (int32) rand() % 16 + 1;
    return WordToChoose;
}

Also just more understanding of how to use maps would be great. ive been doing fine until i started using maps.

Wow im clueless sometimes

FString HIDDEN_WORD = HWords[WordRandomizer()];

should be

FString HIDDEN_WORD = HiddenWordFromMap[WordRandomizer()];

I just forgot to change the name. However id still like help with the maps please.

it need not (unsigned int) and down (int32) it’s look like more simple
you need in header #include
int32 WordRandomizer()
{
srand(time(NULL));
int32 k = rand() % 16 + 1;
return k;
}
and simple function

I solved this quite a while ago, and infact have a better solution than the one i posted.

TMAP <int32, FString> HiddenWordMap//Words for hidden guess--Can add more here
{
//MUST BE ISOGRAMS ELSE GAME CANT BE PLAYED--see IsIsogram
{ 1, "power" },{ 2,"stick" },{ 3,"comedy" },
{ 4,"wizard" },{ 5,"magic" },{ 6,"house" },
{ 7,"eight" },{ 8,"sharp" },{ 9,"sight" },
{ 10,"knight" },{ 11,"solar" },{ 12,"vertical" },
{ 13,"desktop" },{ 14,"terminal" },{ 15,"symbolic" },
{ 16,"storage" },{ 17, "shark" },{ 18,"rodent" },
{ 19,"flower" },{ 20,"cloud" },{ 21,"teach" },
{ 22,"dialogue" },{ 23,"english" },{ 24,"phone" },
{ 25,"table" },{ 26,"isogram" },{ 27,"triangle" },
{ 28,"python" },{ 29,"single" },{ 30,"double" },
{ 31,"graphics" },{ 32,"geomancy" }
};

void const FBullCowGame::Reset()//initializes private members
{
MyCurrentTry = 1;
srand((unsigned int) time(NULL));
FString HIDDEN_WORD = HiddenWordMap[rand() % HiddenWordMap.size() + 1];//Gets a hidden word from the map
MyHiddenWord = HIDDEN_WORD;
}

I usually just post code in small snippets to get to the point however you can view my whole project here

Thanks for the tip about (unsigned int)
while working on an older version of my code i needed it, but i dont anymore

Privacy & Terms