// Word Jumble Junior using sight words to help with reading
// The classic word jumble game where the person can ask for a hint
// Written by Janine Bouyssounouse 1/28/07

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

using namespace std;

void welcome();
char askYesNo(string question);
string jumbleWord(string theWord);
void playGame(string theWord, string theHint, string jumbledWord);

int main()
{
	enum fields {WORD, HINT, NUM_FIELDS};
	const int NUM_WORDS = 10; // number of jumbled words/hints
	
	// Listing of all possible jumbled words
	const string WORDS[NUM_WORDS][NUM_FIELDS] =
	{
		{"wall", "Can you climb over it?"},
		{"see", "What do the eyes do?"},
		{"the", "A cat in ___ house is nice."},
		{"this", "Do you want ____ one?"},
		{"that", "There is a cookie in ____ jar."},
		{"there", "I see one over _____."},
		{"their", "It is _____ book."},
		{"yes", "The opposite of no."},
		{"said", "He ____ he would like some juice."},
		{"want", "I ____ the green one."}
	};
	
	welcome();
	
	do
	{
	   srand(time(0));
	   int choice = (rand() % NUM_WORDS); // chooses random word to jumble
	   string theWord = WORDS[choice][WORD];  // word to guess
	   string theHint = WORDS[choice][HINT];  // hint for word
	
	   string jumbledWord = jumbleWord(theWord); // jumbled version of word
	
	   playGame(theWord, theHint, jumbledWord); // directs play of game
	  	    
	} while (askYesNo("\nDo you want to play again?") == 'y');
	
	cout << "\nThanks for playing.\n\n";
	
	system("PAUSE"); // holds screen open at end of program
	
	return 0;
} // end main
//-----------------------------------------------------------------
// functions:
//-----------------------------------------------------------------
void welcome() // First greetings to explain the game
{
    cout << "\tWelcome to Word Jumble Junior!\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";  
} // end function welcome
//-----------------------------------------------------------------
char askYesNo(string question) // asks a given yes/no question and returns response
{
    char response;
	do
	{
		cout << question << " (y/n): ";
		cin >> response;
	} while (response != 'y' && response != 'n');
	
	return response;
} // end function askYesNo
//-----------------------------------------------------------------
string jumbleWord(string theWord) // jumbles the word to use in the game
{
    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;
	}
	
	return jumble;
} // end function jumbleWord
//-----------------------------------------------------------------
void playGame(string theWord, string theHint, string jumbledWord) // plays game
{
   string guess;

   // plays game until the word is guessed or quit is typed
   do
   {		   
      cout << "\n\nThe jumble is: " << jumbledWord;
		
      cout << "\n\nYour guess: ";
      cin >> guess;
		   
	  if (guess == "hint")
	      cout << "\n\n" << theHint; // shows hint for jumbled word
	  else if (guess == "quit")
          cout << "\n\nThe word is " << theWord << ".\n"; // gives answer 
	  else if (guess != theWord)
	      cout << "Sorry, that's not it."; // response for incorect guess
		    
   } while ((guess != theWord) && (guess != "quit"));
	
   if (guess == theWord)
       cout << "\nGreat job. You guessed it!\n";
} // end function playGame
//-----------------------------------------------------------------