// Arup Guha
// 1/31/07
// Solution to COP 3330 Program #1

import java.util.*;
import java.io.*;

public class Hangman {
	
	public static void main(String[] args) throws IOException {
		
		Scanner stdin = new Scanner(System.in);
		
		System.out.println("Welcome to the Hangman Program!\n");

		// Get the file with the words for the game.		
		System.out.println("What is the name of the file with all of the words?");
		String filename = stdin.next();
		Scanner fin = new Scanner(new File(filename));
		
		// Figure out which word in the file will be used for the game.
		int numwords = fin.nextInt();
		System.out.println("What number word, from 1 to "+numwords+", do you want to use?");
		int whichword = stdin.nextInt();
		
		String word = "default";	// This is so the compiler doesn't complain.
		for (int i=0; i<whichword; i++)
			word = fin.next();
			
		int badguesses = 0;
		
		// Main game loop - we'll break out of it when the game's over.
		while (true) {
			
			// Print out the board and get the player's guess.
			System.out.println("Here is the board:\n");
			printBoard(word);
			System.out.println("What is your guess?");
			String guess = stdin.next();
			char guesschar = guess.charAt(0);
			
			
			// Replace their guess with its uppercase version and store that answer.
			String newword = word.replace(guesschar, Character.toUpperCase(guesschar));
			
			// If the guess was bad, this new string will be the same as the old one.
			if (newword.equals(word)) {
				
				// Output for this case.
				System.out.println("\nSorry, that letter is not on the board.");
				badguesses++;
				
				// Check if the user lost!
				if (badguesses == 5) {
					
					// Finish up the game.
					System.out.println("Sorry, you have not guessed the correct word in time.");
					word = word.toUpperCase();
					System.out.print("The correct word was ");
					printBoard(word);
					break;
				}
			}
			
			// At least one letter matched!
			else {
				
				// Output for this case.
				System.out.println("Good job!");
				word = newword;
				
				// See if they won, which occurs when the board is completely upper case.
				if (word.equals(word.toUpperCase())) {
					
					// Finish up the game.
					System.out.println("Congratulations, you win!");
					System.out.println("Here is the final board:");
					printBoard(word);
					break;
				}
					
				
			} // end if-else
		}	// end while
	} // end main
	
	
	// Prints out the board according to word.
	public static void printBoard(String word) {
		
		// Loop through all the letters.
		for (int i=0; i<word.length(); i++) {
			
			// If a letter is upper case, the user's already guessed it correctly.
			if (Character.isUpperCase(word.charAt(i)))
				System.out.print(word.charAt(i));
				
			// Otherwise it hasn't been guessed yet so we print an underscore.
			else
				System.out.print("_");
				
			// The specification asked for a blank between each letter.
			System.out.print(" ");
		}
		System.out.println();
	}
	
}