Jumble Word Game

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//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] =
    {
          {"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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
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 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






Last edited on
So I believe I have figured out the answer to the second part of the question by thinking about how the programming was working all on my ownsome. If anyone would like to weigh in and verify my accuracy it would be appreciated.

I believe the way variables work is they follow the assignment of the most recent but previouse assignment. So by calling a it looks and sees that it has been assigned to b but follows the assignment of the most recent b. When I call b I get the newest assignment of b which is the last line before the cout line and it uses this assigned b value which was stored as c.

I have terrible grammer

good day
closed account (D80DSL3A)
The code in question, lines 7,8,9 above:
1
2
3
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp; 

results in the values of jumble[index1] and jumble[index2] being swapped.
The variable 'temp' is serving as "auxiliary memory" for the swap (to temporarily save a value).
This would be similar:
1
2
3
4
5
char a='C', b='A', c='T';

char temp = a;// save the value 'C' in temp
a = b;// store the value 'A' in a. Now both a and b contain 'A'
b = temp;// store the saved value 'C' into b 

This should leave a='A', b='C' (and c='T' still). ie, the values of a and b were swapped.
So, cout << a << b << c; should give "ACT"
Last edited on
Oh okay! This makes sense. This way the program does not lose any values or repeart char's that are not suppose to be repeated? The word stays the word but still gets jumbled.

Thanks!
Last edited on
Topic archived. No new replies allowed.