# Math Is Fun Forum

Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2021-12-23 13:52:55

michelleperms
Member
Registered: 2021-12-19
Posts: 2

### Jumble Word Game Source

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 #include #include #include 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] = { {"wall", "Do you feel like banging your head against something?"}, {"glasses", "These might help you see the answer" }, {"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; cout <<"\n\nYour 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 cout << "\n\nYour guess: "; 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 occuring 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 answer [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 lets 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 probaly too stupid to be a programmer haha thanks

Offline