
import java.util.Scanner;
/********************************************************
 * This class will start the game of Hangman
 *
 * @author - Nancy Harris
 * @version - 2 - WordGuess Driver
 ********************************************************/
public class WordGuessDriver
{

	/** main builds a new game and starts play
	 *
	 * @param args Provides the seed value to use in the game
	 */
	 
	public static void main(String args[])
	{
		long seed;   // seed value to start random generator
		
		// This code gets a seed value to use to control words displayed
		// Use the parameter and the same number to control which words will
		// be chosen for testing purposes
		if (args.length == 0)
			seed = 123456789;
		else 
			seed = Long.parseLong(args[0]);

		playGame(seed);
	
		System.out.println("Game over.  Play again soon.");	
	} // END main method

	/** playGame runs through the game.  Ends when the user chooses 
	 *  not to play another game
	 */
	public static void playGame(long seed)
	{
	
		Dictionary words;			// a collection of words
		Scanner keyboard;
		String userWord;			// the word the user is working on 
        String userGuesses;
		String playAgain;
		String word;				// the current word in play
		WordGuess tool;

		int guessCount;			// tracks guesses
		char test;					
		char userGuess;			// letter user has guessed
		
		// starts the game by making a dictionary.
		words = new Dictionary(seed);
		keyboard = new Scanner(System.in);

		// display greeting										
		System.out.println("Welcome to CS139 -- Word Guess!!\n");
        do
		{			
			word = words.getWord();
			      	
			tool = new WordGuess(word); // pass word to use in display
			
			// activate this line for testing
//			System.out.println("The WORD: " + word);
			
			System.out.println("Guess a letter to see if it's in the word.");
			System.out.println("The word you are trying to guess has " + word.length() + " letters.\n");
			
			System.out.println(tool.getUserWord());
			System.out.println(tool.getUserGuesses());
			
								
			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 (tool.isInWord(userGuess))
				{
					System.out.println("\nYES!! " + userGuess + " is in the word.\n"); 
					userWord = tool.updateUserWord(userGuess);
					userGuesses = tool.updateGuesses(userGuess);
				}
				else
				{
					if (!tool.isInWord(userGuess))
						tool.updateGuesses(userGuess);
					System.out.println("\nNope... " + userGuess + " is not in the word.\n");
   
				}
			
                        
				System.out.println(tool.getUserWord());
				System.out.println(tool.getUserGuesses());
				
				if(tool.isWordComplete())
					System.out.println("\n\tYou Win!!!! \n");
				else if(tool.isOut())
				{
					System.out.print("\nToo many guesses...  You lose!\n");
					System.out.println("The word was " + tool.getTheWord() + ".\n");
				}
				else
					System.out.println("\nKeep trying..."); 			
			 
			} while(!tool.isWordComplete() && !tool.isOut()); //END do loop
			
			System.out.println("Do you want to play again (yes/no)? ");
			playAgain = keyboard.nextLine();
			System.out.println();
							
		} while(!playAgain.equalsIgnoreCase("no")); // END play again do loop
	} // END playGame	
} // END class WordGuessDriver
