import java.util.Scanner;
import java.util.Random;

class Mar16 {

	public static void main(String[] args) {
		
		// declare an array of word
		String[] words = {"pizza", "house", "rock"};
	
        while(true) {

            // pick one randomly
            Random rand = new Random();
            int index = rand.nextInt(words.length);
            String randWord = words[index];

            // convert word to character array
            char[] word = randWord.toCharArray();

            // create an guessed array
            char[] guessed = randWord.toCharArray();

            // initialize guessed array
            for(int i = 0; i < guessed.length; i++ ) {
                guessed[i] = '*';
            }

            // initialize guess count
            int numGuesses = 0;

            //  Initialize a scanner
            Scanner kb = new Scanner(System.in);

            while(true) {
                // print to screen guessed array
                for(char c : guessed) {
                    System.out.print(c);
                }
                System.out.println();

                // ask user to enter character
                System.out.println("Please enter a character");

                // read in character
                char guess = kb.nextLine().charAt(0);

                // increment guess count
                numGuesses++;

                // see if char is in word
                boolean found = false;
                for(char c : word) {
                    if (guess == c) {
                        found = true;
                        break;
                    }
                }

                // if in the word, update guessed array
                if (found) {
                    for(int i = 0; i < guessed.length; i++) {
                        if(word[i] == guess) {
                            guessed[i] = guess;
                        }
                    }
                }
                // else print message saying not in word
                else {
                    System.out.println("Sorry, not in the word");
                }

                // check and see if we are done
                boolean done = true;
                for(char c : guessed) {
                    if (c == '*') {
                        done = false;
                    }
                }

                if (done) {
                    System.out.printf("Good job.  Took you %d tries\n", numGuesses);
                    break;
                }
            } // inner while loop

            System.out.println("Do you wish to continue (y/n)?");

            char choice = kb.nextLine().charAt(0);

            if (choice == 'n') {
                break;
            }

        } // outer while loop
 
	} // end of main method

} // end of class
