Jumble Word Game with Point Tracker: A Fun Way to Practice Coding

  • Thread starter triley
  • Start date
  • Tags
    Game
In summary: I am assuming that it is somehow creating a jumble of the words entered. Can you clarify this for me?
  • #1
triley
15
1
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] =
    {
          {"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 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

ACTI 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:
Physics news on Phys.org
  • #2
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.

triley said:
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
triley said:
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:
  • #3
Merlin3189 said:
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.
button.png
 
Last edited:
  • Like
Likes sysprog and Merlin3189
  • #4
triley said:
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.
 
  • #5
Merlin3189 said:
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
 
  • #6
Merlin3189 said:
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
 
  • #7
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"
 
  • #8
I haven't followed this thread in any detail, but thought perhaps inspiration can be found in how std::random_shuffle works.
 
  • #9
triley said:
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

triley said:
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.
triley said:
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.
 

FAQ: Jumble Word Game with Point Tracker: A Fun Way to Practice Coding

What is the purpose of the Jumble Word Game with Point Tracker?

The purpose of the Jumble Word Game with Point Tracker is to provide a fun and interactive way for individuals to practice and improve their coding skills. By playing the game, users can learn coding concepts and improve their problem-solving abilities.

How does the Point Tracker work in the Jumble Word Game?

The Point Tracker in the Jumble Word Game keeps track of the number of correct answers a user gets while playing the game. Each correct answer earns the user a point, and the points are displayed on the screen for the user to see their progress.

Is the Jumble Word Game suitable for all levels of coding experience?

Yes, the Jumble Word Game is designed to be suitable for all levels of coding experience. The game offers different difficulty levels, allowing beginners to start with simpler coding concepts and more advanced coders to challenge themselves with more complex coding challenges.

Can the Jumble Word Game be played on any device?

Yes, the Jumble Word Game is a web-based game and can be played on any device with an internet connection. It is compatible with desktops, laptops, tablets, and smartphones.

Are there any rewards for completing the Jumble Word Game?

While there are no tangible rewards for completing the Jumble Word Game, users will have the satisfaction of improving their coding skills and earning points. Additionally, playing the game regularly can help users in their coding journey and may lead to future career opportunities.

Similar threads

Replies
6
Views
3K
Replies
14
Views
5K
Replies
3
Views
2K
Replies
9
Views
3K
Replies
19
Views
2K
Back
Top