Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,630 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,064 people online right now. Registration is fast and FREE... Join Now!




word jumble c++

 
Reply to this topicStart new topic

word jumble c++

rainbow78
post 18 Jul, 2007 - 07:38 PM
Post #1


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 4


My Contributions


I'm lost on a few things to do after this, can anyone give me some help or hints
here what I got left:



Restrict the user to 5 guesses only.


Add a scoring system that assigns a point value to each word.
Base the point value on the word length (hint: use string funtions).
Deduct a point every time the player asks for a hint.
When the player guesses the word, tell him how many points he earned.

Add a loop so the player can guess another word.
Keep a running total of the overall points.


what I got so far is:
CODE

// Word Jumble
// The classic word jumble game where the player can ask for a hint

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

using namespace std;

int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it?"},
{"persistent", "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 for word

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;
}

cout << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble 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;

while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
cout << theHint;
else
cout << "Sorry, that's not it.";

cout <<"\n\nYour guess: ";
cin >> guess;
}

if (guess == theWord)
cout << "\nThat's it! You guessed it!\n";

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

return 0;
}

User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 19 Jul, 2007 - 04:28 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Can you describe the problem you are having? any error messages?
User is offlineProfile CardPM

Go to the top of the page

Topher84
post 19 Jul, 2007 - 07:41 AM
Post #3


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 232



Dream Kudos: 25
My Contributions


Neat program... to restrict the user to only 5 guesses you need to use a FOR loop and restric the max value ( which you can make a constant as I have done ) to 5. For allowing the user to guess again you need to put your code in a DO WHILE loop which will DO the code 1 time regardless and at the end you prompt for a repeat and DO the code again WHILE the repeat character is valid. For the point system, since you are using strings you can use the .size() operator so if the guess is valid then you say guess.size() to get the # of letters in the word and "+=" and essentially continuously add the total # of letters of the correct guess to the points! To decrease one you just use points--; to subtract 1 from the user.

WALL OF TEXT I KNOW! smile.gif

You can clean up the code however you want but this does exactly what you want:

Note: you may want to add more error checking for the user choice at the end to play again and clean up the variable names and comment!

CODE

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

using namespace std;

int main()
{
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 5;
    const int MAX_GUESSES = 5;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
    {"wall", "Do you feel you're banging your head against something?"},
    {"glasses", "These might help you see the answer."},
    {"labored", "Going slowly, is it?"},
    {"persistent", "Keep at it."},
    {"jumble", "It's what the game is all about."}
    };

    string guess;
    int points = 0;
    char another;


    do
    {
        system("cls");
        srand(time(0));
        int choice = (rand() % NUM_WORDS);
        string theWord = WORDS[choice][WORD]; // word to guess
        string theHint = WORDS[choice][HINT]; // hint for word

        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;
        }
        cout << "\t\t\tWelcome to Word Jumble!\n\n";
        cout << "Unscramble 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;
    
        for(int nIndex = 0; nIndex < MAX_GUESSES; nIndex++)
        {
            cout << "\n\nGuess " << nIndex+1 << ": ";
            cin >> guess;

            if (guess == "hint")
            {
                cout << theHint;
                points--; //subtract 1 point
            }//end if
            else if (guess == theWord)
            {
                cout << "\nThat's it! You guessed it!\n";
                points += guess.size(); //points = # of letters in word
                break;
            }//end else if
            else
            {
                cout << "Sorry, that's not it.";
            }//end else
        }//end for
        cout << "\n\nYour Total Points Are: " << points;
        cout << "\n\n\nWould You Like To Play Again? (y/n): ";
        cin  >> another;
    }while(another == 'y' || another == 'Y');

    system("cls");
    cout << "Thanks for playing!";

    return 0;
}//end main


This post has been edited by Topher84: 19 Jul, 2007 - 07:42 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:54AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month