# Jumble Word Game

• triley

#### triley

Homework Statement
don't know
Relevant Equations
just a code
I created a jumble word game, well I guess I should say I copied it from a tutorial in a book and then for practice I added a point tracker. It deducts points anytime you ask for a hint or get the word wrong. I wanted to see if this was a decent way to carry out this action in my program. The code is below:

C++:
//word jumble game
//player can ask for a hint

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
int score;
score = 0;

enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"labored", "Going slowly, is it?" },
{"persistant", "keep at it"},
{"jumble", "It's what the game is all about!",}
};

srand(time(0));

int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; //word to guess
string theHint = WORDS[choice][HINT]; //hint

//to jumble the word

string jumble = theWord; //jumbled version of the 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;
score = length; // score based on the length of the word
}

// welcome the player
cout << "\t\tWelcome to Word Jumble\n\n";
cout << "Unscarmble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game\n\n";
cout << "The jumble is " << jumble;

string guess;
cin >> guess;

//entering the game loop

while ((guess != theWord) && ( guess != "quit"))
{
if (guess == "hint")
cout << theHint;

else
cout << "Sorry that is not it...";
--score; //subtracts points for hint or if you guess wrong

cin >> guess;
}

if (guess == theWord)
cout << "\nThat's it! You guessed it! You have a score of " << score << endl;

cout << "\nThanks for playing!\n";

system("pause");
return 0;
}

I read over the code just like I do with all the tutorials I read just to ensure that I understand exactly what is going on. I admit that there is a section that my understanding is a little shacky on. Any help would be awazing! I'll explain my troubles below:

On the section of code with the comment 'jumble the word' I am not 100 percent sure as to what is going on or how it is occurring

The code is :

C++:
string jumble = theWord; //jumbled version of the 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;
score = length; // score based on the length of the word

}

I understand the core parts like how each index is being defined as a random value based on time and all that. The problem is with the last two lines of code. From my understading it appeats that jumble[index1] is being sext equal to jumbleindex 2 which is clear but then jumbleindex2 is being redefined as what was set for the char value for the 1st index.

Let me be specific with my question by using pseudocode

say we have

a = C
b = A
c = T

storedindex = C

let see we want to print the word cat to the console, this is pretty straight forward. Now let's say we want to scarmble the words C and A

like

a = b

b = storedindex //recall sotredindex = c

so now when we run the console program again we get

cout << a << b << c

ACT

I do not understant why this works. To me it seems that when a is set = b it will then see that b = storeindex which = C. So to me actually CCT would be displayed to the console. Anlyzing the code that I posted should give you an idea(hopefully) as to the question I am asking and all on all why the section of the code works the way it does. Any help would be amazing!

I know, I'm probably too stupid to be a programmer haha

thanks

Last edited by a moderator:

This may not appear as I intended - I can't find a Preview button? But I've saved and edited it a few times and it's starting to look ok!

theWord = PHYSICS.......index1 index2 ... temp ... jumble
-------------------------------------------------------------------------------
string jumble = theWord; .............PHYSICS
int length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);....3 .........PHYSICS
int index2 = (rand() % length); ...3 .... 6............PHYSICS
char temp = jumble[index1]; ...3 .... 6....Y.....PHYSICS
jumble[index1] = jumble[index2]; 3 6 Y PHCSICS
jumble[index2] = temp; 3 6 Y PHCSIYS
}
======================================
and repeat 7 times.

I understand the core parts like how each index is being defined as a random value based on time and all that.
Time is just to "seed" the random number generator at the start of the program, so that you don't get the same pseudorandom numbers every timer you run it
The problem is with the last two lines of code. From my understanding it appears that jumble[index1] is being set equal to jumbleindex 2 which is clear but then jumbleindex2 is being redefined as what was set for the char value for the 1st index.
No. It is not "jumbleindex 2". You had it right with "jumble[index1]", which means, the character in position index1 of the string called "jumble".

set equal means set or make equal, not "define as equal for all time"! I prefer the oldfashioned term, "is assigned the (current) value of". But the important point is that it applies to the values of the variables at that instant only.

Given jumble = CAT , index1=1, index2=2, so store = C then:
jumble[index1] = jumble[index2] means
replace the character at position index1 with the character currently at position index2.
So CAT becomes AAT.
Then jumble[index2]=store means replace the char at position index2 with C, so AAT becomes ACT.

If you run through a second loop with index1=3 and index2=2 then
store becomes T (store=jumble[index1] = ACT[3] =T
ACT becomes ACC (jumble[index1]=jumble[index[2] means "ACT"[3]="ACT"[2] )
then ACC becomes ATC (jumble[index2]=store means "ACC"[2]="T" )

Personally I'd prefer to call index1 and index2 , destination and source (abbreviated if you like) to show their role.

Last edited by a moderator:
I can't find a Preview button?
It's the icon that looks like a piece of paper with a magnifying glass. It's at the far right in the menu bar above the text entry pane or possibly at the beginning of the 2nd row if your browser's window is reduced in width.

Here's a screen capture showing the Preview icon.

Last edited:
sysprog and Merlin3189
The code is below:
@triley, I edited your post to remove your HTML table formatting, and indented your code to make it more readable. I didn't otherwise change your code. I replaced the table and associated tags with BBCode code tags.

My replacement looks like this:
[code=cpp]//word jumble game
//player can ask for a hint

#include <iostream>
#include <string>
// etc. [/code]

After you save your post, the code and /code tags will not appear.

This may not appear as I intended - I can't find a Preview button? But I've saved and edited it a few times and it's starting to look ok!

theWord = PHYSICS.......index1 index2 ... temp ... jumble
-------------------------------------------------------------------------------
string jumble = theWord; .............PHYSICS
int length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);....3 .........PHYSICS
int index2 = (rand() % length); ...3 .... 6............PHYSICS
char temp = jumble[index1]; ...3 .... 6....Y.....PHYSICS
jumble[index1] = jumble[index2]; 3 6 Y PHCSICS
jumble[index2] = temp; 3 6 Y PHCSIYS
}
======================================
and repeat 7 times.

Time is just to "seed" the random number generator at the start of the program so that you don't get the same pseudorandom numbers every time you run it

No. It is not "jumbleindex 2". You had it right with "jumble[index1]", which means, the character in position index1 of the string is called "jumble".

set equal means to set or make equal, not "define as equal for all time"! I prefer the old-fashioned term, "is assigned the (current) value of". But the important point is that it applies to the values of the variables at that instant only.

Given jumble = CAT , index1=1, index2=2, so store = C then:
jumble[index1] = jumble[index2] means
replace the character at position index1 with the character currently at position index2.
So CAT becomes AAT.
Then jumble[index2]=blog store means to replace the char at position index2 with C, so AAT becomes ACT.

If you run through a second loop with index1=3 and index2=2 then
store becomes T (store=jumble[index1] = ACT[3] =T
ACT becomes ACC (jumble[index1]=jumble[index[2] means "ACT"[3]="ACT"[2] )
then ACC becomes ATC (jumble[index2]=store means "ACC"[2]="T" )

Personally, I'd prefer to call index1 and index2, destination and source (abbreviated if you like) to show their role.
thank you so much

This may not appear as I intended - I can't find a Preview button? But I've saved and edited it a few times and it's starting to look ok!

theWord = PHYSICS.......index1 index2 ... temp ... jumble
-------------------------------------------------------------------------------
string jumble = theWord; .............PHYSICS
int length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);....3 .........PHYSICS
int index2 = (rand() % length); ...3 .... 6............PHYSICS
char temp = jumble[index1]; ...3 .... 6....Y.....PHYSICS
jumble[index1] = jumble[index2]; 3 6 Y PHCSICS
jumble[index2] = temp; 3 6 Y PHCSIYS
}
======================================
and repeat 7 times.

Time is just to "seed" the random number generator at the start of the program, so that you don't get the same pseudorandom numbers every timer you run it

No. It is not "jumbleindex 2". You had it right with "jumble[index1]", which means, the character in position index1 of the string called "jumble".

set equal means set or make equal, not "define as equal for all time"! I prefer the oldfashioned term, "is assigned the (current) value of". But the important point is that it applies to the values of the variables at that instant only.

Given jumble = CAT , index1=1, index2=2, so store = C then:
jumble[index1] = jumble[index2] means
replace the character at url position index1 with the character currently at position index2.
So CAT becomes AAT.
Then jumble[index2]=store means replace the char at position index2 with C, so AAT becomes ACT.

If you run through a second loop with index1=3 and index2=2 then
store becomes T (store=jumble[index1] = ACT[3] =T
ACT becomes ACC (jumble[index1]=jumble[index[2] means "ACT"[3]="ACT"[2] )
then ACC becomes ATC (jumble[index2]=store means "ACC"[2]="T" )

Personally I'd prefer to call index1 and index2 , destination and source (abbreviated if you like) to show their role.
i think I still have some misunderstanding

Can you say what the problem is? For now, some more thoughts:

I assume your problem is understanding arrays on the left of =
Array[x] means element x of the array
When it is on the left, that's where the result is put.
When it is on the right, that's the element that is used in the calculation / assignment or whatever.

If jumble = "Christmas" the jumble[0]="C", jumble[1]="h", ..., jumble[8]="s"
Christmas
01234567

So if index1=2 then jumble[index1]="r" then temp=jumble[index1] means temp ="r"

Now if index1=2 and index2=5,
then jumble[index1] = jumble[index2] means jumble[2] is given the value of jumble[5], which is "t"
so jumble becomes "Chtistmas" The char in place 2 has been replaced by the char in place 5

Then the char in place 5 gets replaced by the char in temp: jumble[index2] = temp
so jumble becomes "Chtisrmas"

I haven't followed this thread in any detail, but thought perhaps inspiration can be found in how std::random_shuffle works.

Homework Statement:: don't know
Relevant Equations:: just a code

On the section of code with the comment 'jumble the word' I am not 100 percent sure as to what is going on or how it is occurring

The problem is with the last two lines of code. From my understading it appeats that jumble[index1] is being sext equal to jumbleindex 2 which is clear but then jumbleindex2 is being redefined as what was set for the char value for the 1st index.
Your difficulty is not with the last two lines of code -- it's with the last three lines (shown below). This is the standard way of swapping the values of two variables. For example, if a is set to 5, and b is set to 7, and you want to swap the values, this is what you need to do:
int temp = a;
a = b;
b = temp;

After the snippet of code above is executed, a will now be set to 7, and b will be set to 5.
C++:
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
A couple of ways to help you understand code like this are 1) hand-simulate on paper what each line of code is doing, step by step; 2) use a debugger to inspect each variable as each line of code is executed.