/********************************************************
 * This class will play variant of Hangman
 *
 * @author - Vincent D. Capaccio and Nancy Harris
 * @version - 2 - WordGuess
 ********************************************************/
 /*******************************************************
 /  References and Ackknowledgements: I have recieved
 /  no help on this programming assignment
 /
 /*******************************************************/
import java.util.Scanner; // needed for keyboard input
 
public class WordGuess
{
	final int NUM_GUESSES = 5;
	
	Scanner keyboard;   // used by many methods
	Dictionary words;   // provides the words that we will use

	/** HangMan constructor takes in the seed and uses it to instantiate the 
	    Dictionary object
		
		@param seed The seed value to set the random number generator
		*/
	public WordGuess(long seed)
	{
		words = new Dictionary(seed);
		keyboard = new Scanner(System.in);
	}
	
	/** playGame runs through the game.  Ends when the user chooses 
	 *  not to play another game
	 */
	public void playGame()
	{
		// runs through the game asking the user whether they want to play
		// display greeting
		// while user wants to play
		
			// do
				// get and display word template
				// ask user to guess a letter
				// give results
				// if they have exceeded the limit end
				// if the word is the right word end
			// display results
			// ask if they want to play
		// display exit message
		
		String word;
		char userGuess;
		String userWord;
      String userGuesses;
		String playAgain;
		int guessCount;
		char test;

		// display greeting										
		System.out.println("Welcome to CS139 -- Word Guess!\n");
      do
		{			
			// init variables 
			word = words.getWord();

			guessCount = 0;
         userGuesses = "";
         	
			userWord = setUserWord(word);   // pass word to use it in display

			System.out.println("Guess a letter to see if it's in the word.\nThe word you are trying to guess has " + word.length() + " letters.\n");
			displayUserWord(userWord, userGuesses);
								
			do
			{
				System.out.println();
				System.out.print("What is your guess: ");
				userGuess = keyboard.nextLine().charAt(0);

				// change the letter to lower case  
				userGuess = Character.toLowerCase(userGuess);
					
				if (isInWord(userGuess, word))
				{
					System.out.println("\nYES!! " + userGuess + " is in the word.\n"); 
					userWord = updateUserWord(userGuess, userWord, word);
				}
				else
				{
					guessCount++; // increment on bad
					System.out.println("\nNope... " + userGuess + " is not in the word.\n");
               userGuesses = updateGuesses(userGuesses, userGuess);

				}
			
                        
				displayUserWord(userWord, userGuesses);
				
				if(word.compareToIgnoreCase(userWord) == 0)
					System.out.println("\n\tYou WIN!!!!\n");
				else if(guessCount > NUM_GUESSES)
				{
					System.out.print("\nToo many guesses...  You lose!\n");
					System.out.println("The word was " + word + ".\n");
				}
				else
					System.out.println("\nKeep trying..."); 			
			 
			} while(word.compareToIgnoreCase(userWord) != 0 && guessCount <= NUM_GUESSES); //END do loop
			
			System.out.println("Do you want to play again? ");
			playAgain = keyboard.nextLine();
			System.out.println();
							
		} while(!playAgain.equalsIgnoreCase("no")); // END play again do loop
	} // END playGame
	
		/** setUserWord takes in the word to play with and the user word
		 *  and returns a word of the proper length set to the values
		 *  that indicate a letter has not been guessed yet
		 * 
		 * @param theWord the word from the dictionary
		 * @param userWord a word that will be the userWord
		 * @return the userWord filled in with the no guess symbol
		 */	
		public String setUserWord(String theWord)
		{
			String userWord;
			userWord = "";
			
			for(int counter = 0; counter < theWord.length(); counter++)
			{	
				userWord = userWord + "*";
			}
			
			return userWord;
		}
		
		/** isInWord takes a guess and a word and returns true if 
		 *  the letter is in the word, and false if it is not.
		 *
		 * @param guess The letter that the user guessed
		 * @param word The word to inspect
		 * @return true if guess is in word, false otherwise
		 */
		public boolean isInWord(char guess, String word)
		{	
			boolean result;
			
			result = false;
			
			for (int ii = 0; ii < word.length() && !result; ii++)
			{
				if (guess == word.charAt(ii))
					result = true;
			}
			
			return result;
		}
		/** updateUserWord takes a guess, a progress word and the dictionary word
		 *  and produces a new progress word with the corresponding guess locations
		 *  filled in.  For example, if the guess were e and the word fetch, and the 
		 *  progress word _____, then this method would return a new String _e___
		 * 
		 * @param guess The letter the user guessed
		 * @param userWord The progress word that shows where the user is
		 * @param word The dictionary word
		 * @return a String representing the new progress word
		 */
		public String updateUserWord(char guess, String userWord, String word)
		{
			String newUserWord;
			newUserWord = "";
			
			for (int ii = 0; ii < word.length(); ii++)
			{
				if (guess == word.charAt(ii))
					newUserWord = newUserWord + guess; // replace * by guess
				else
					newUserWord = newUserWord + userWord.charAt(ii); //keep * or letter
			}
			
			return newUserWord;
		}
		/** updateGuesses updates the bad guess string with the new bad guess
		 *
		 * @param userGuesses The prior bad guesses
		 * @param guess The current bad guess
		 * @return The bad guess String updated with the new bad guess
		 */
      public String updateGuesses(String userGuesses, char guess)
      {
         return userGuesses + guess;
      }
		
		/** displayUserWord displays the word as the user is guessing and 
		 *  filling in the spaces.  The display will include a space before 
		 *  each letter (or underscore) in the word.
		 * 
		 * @param userWord The user's progress word
		 * @param userGuesses The bad guess String
		 */
		public void displayUserWord(String userWord, String userGuesses)
		{	
         for (int ii = 0; ii < userWord.length(); ii++)
			{	
				if (ii != 0)
				   System.out.print(" ");		
				if (userWord.charAt(ii) == '*')
					System.out.print("_");
				else
					System.out.print(userWord.charAt(ii));
			}
			System.out.println();
         System.out.printf("Strikes: %d\tBad Guesses: ", userGuesses.length());
         for (int ii = 0; ii < userGuesses.length(); ii++)
         {
            if (ii != 0)
               System.out.print(" " + userGuesses.charAt(ii));
            else
               System.out.print(userGuesses.charAt(ii));
        	}
	      System.out.println();
      }
} // END class WordGuess
