home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter03 / word_jumble.cpp < prev   
Encoding:
C/C++ Source or Header  |  2003-11-17  |  1.8 KB  |  68 lines

  1. // Word Jumble
  2. // The classic word jumble game where the player can ask for a hint
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <cstdlib>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     enum fields {WORD, HINT, NUM_FIELDS};
  14.     const int NUM_WORDS = 5;
  15.     const string WORDS[NUM_WORDS][NUM_FIELDS] =
  16.     {
  17.         {"wall", "Do you feel you're banging your head against something?"},
  18.         {"glasses", "These might help you see the answer."},
  19.         {"labored", "Going slowly, is it?"},
  20.         {"persistent", "Keep at it."},
  21.         {"jumble", "It's what the game is all about."}
  22.     };
  23.  
  24.       srand(time(0));
  25.     int choice = (rand() % NUM_WORDS);
  26.     string theWord = WORDS[choice][WORD];  // word to guess
  27.     string theHint = WORDS[choice][HINT];  // hint for word
  28.  
  29.     string jumble = theWord;  // jumbled version of word
  30.     int length = jumble.size();
  31.     for (int i=0; i<length; ++i)
  32.     {
  33.         int index1 = (rand() % length);
  34.         int index2 = (rand() % length);
  35.         char temp = jumble[index1];
  36.         jumble[index1] = jumble[index2];
  37.         jumble[index2] = temp;
  38.     }
  39.  
  40.     cout << "\t\t\tWelcome to Word Jumble!\n\n";
  41.     cout << "Unscramble the letters to make a word.\n";
  42.     cout << "Enter 'hint' for a hint.\n";
  43.     cout << "Enter 'quit' to quit the game.\n\n";
  44.     cout << "The jumble is: " << jumble;
  45.  
  46.     string guess;
  47.     cout << "\n\nYour guess: ";
  48.     cin >> guess;
  49.  
  50.     while ((guess != theWord) && (guess != "quit"))
  51.     {
  52.         if (guess == "hint")
  53.             cout << theHint;
  54.         else
  55.             cout << "Sorry, that's not it.";
  56.  
  57.         cout <<"\n\nYour guess: ";
  58.         cin >> guess;
  59.     }
  60.  
  61.     if (guess == theWord)
  62.         cout << "\nThat's it!  You guessed it!\n";
  63.  
  64.     cout << "\nThanks for playing.\n";
  65.  
  66.     return 0;
  67. }
  68.