< Previous | Contents | Next >
Now that I have the word for the player to guess, I need to create a jumbled version of it.
string jumble = theWord; // jumbled version of word int length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length); int index2 = (rand() % length); char temp = jumble[index1]; jumble[index1] = jumble[index2]; jumble[index2] = temp;
}
In the preceding code, I created a copy of the word jumble to.. .well, jumble. I generated two random positions in the string object and swapped the characters at those positions. I did this a number of times equal to the length of the word.