import java.awt.*;
import java.io.*;
import hsa.Console;

public class Hangman
{
    static Console c;

    public static void main (String[] args)
    {
	Hangman p = new Hangman ();
	p.process ();
    }


    void process ()
    {
	c = new Console ();
	String input = "", answer, badlist = "";
	char[] guess = new char [1];
	int i, correct = 0, count = 0, tries = 0;
	boolean wrong = false, noans = false;

	c.print ("Input the hangman's word: ");
	answer = c.readLine ().toLowerCase ().trim ();

	boolean[] taken = new boolean [answer.length ()];
	for (i = 0 ; i < taken.length ; i++)
	    taken [i] = false;

	while (count < 5 && correct < answer.length ())
	{
	    c.clear ();
	    if (count != 0)
	    {
		if (wrong)
		    c.setTextColor (Color.red);
		c.print (count + " error");
		if (count != 1)
		    c.print ("s");
		c.setTextColor (Color.black);
		c.print (": ");
		if (noans)
		    c.println (badlist);
		else
		{                    
		    c.print (badlist.substring (0, badlist.length () - input.length () - 1));
		    if (wrong)
			c.setTextColor (Color.red);
		    c.println (badlist.substring (badlist.length () - input.length () - 1, badlist.length ()));
		    c.setTextColor (Color.black);
		}
	    }
	    
	    if (!noans)
		tries++;

	    for (i = 0 ; i < taken.length ; i++)
	    {
		if (taken [i])
		    c.print (answer.charAt (i));
		else
		    c.print ("_");
		c.print (" ");
	    }
	    c.println ("");

	    c.print ("Guess: ");
	    input = c.readLine ().trim ();
	    wrong = true;
	    noans = false;

	    if (input.length () == 0)
	    {
		wrong = true;
		noans = true;
	    }
	    else if (input.length () == 1)
	    {
		guess = input.toCharArray ();
		for (i = 0 ; i < answer.length () ; i++)
		{
		    if (guess [0] == answer.charAt (i) && !taken [i])
		    {
			correct++;
			taken [i] = true;
			wrong = false;
			break;
		    }
		}
		if (wrong)
		    badlist = badlist + new String (guess) + " ";
	    }
	    else if (input.length () == answer.length ())
	    {
		if (input.equals (answer))
		    break;
		else
		    badlist = badlist + input + " ";
	    }
	    else
	    {
		wrong = false;
		noans = true;
	    }

	    if (wrong)
		count++;
	    c.println ("");            
	}

	if (count == 5)
	    c.println ("You lose after " + tries + " attempts. Word: " + answer);
	else if (correct == 0)
	    c.println ("Cheater!");
	else
	    c.println ("You win after " + tries + " attempts!");
    }
}
