import java.util.Collection;
import java.util.Scanner;

/**
 * Driver class for playing a text-based game of Hangman.
 *   @author Dave Reed (based on code by Stuart Reges and Keith Schwarz)
 *   @version 2/5/14
 */
public class HangmanGame {
    public static final String DICTIONARY_FILE = "dictionary.txt";

    public static void main(String[] args) throws java.io.FileNotFoundException {
        Hangman hangman = new HonestHangman(DICTIONARY_FILE);
        Scanner input = new Scanner(System.in);
        
        char play = 'y';
        while (play == 'y') {
        	hangman.selectWord();
	        System.out.println("I'm thinking of a " + 
	                           hangman.getRedactedWord().length() + 
	                           " letter word.");
	        
	        while (!hangman.wordIsGuessed()) {
	            Collection<Character> pastGuesses = hangman.getPastGuesses();
	            System.out.println("\nGuessed so far: " + pastGuesses);
	            System.out.println("Current pattern: " + hangman.getRedactedWord());
	            System.out.print("Your guess (#" + (pastGuesses.size()+1) + "): ");
	            
	            char guess = input.next().toLowerCase().charAt(0);
	            if (guess == '?') {
	            	for (char ch : "abcdefghijklmnopqrstuvwxyz".toCharArray()) {
		        		hangman.recordGuess(ch);
		        	}
	            }
	            else if (pastGuesses.contains(guess)) {
	                System.out.println("You already guessed " + guess + ".");
	            } 
	            else if (hangman.recordGuess(guess)) {
	                System.out.println("Good guess!");
	            }
	            else {
	                System.out.println("Sorry, " + guess + " is not in the word.");
	            }
	        }
	        
	        System.out.println("\nThe word is: " + hangman.getRedactedWord());
	        System.out.println("\nPlay again? (y/n) ");
	        play = input.next().toLowerCase().charAt(0);
        }
        System.out.println("\nThanks for playing!");
        input.close();
    }
}
