import java.awt.Color;
import java.lang.Math;
import hsa.Console;

public class Mastermind
{
    static Console c;

    public static void main (String[] args)
    {
	Mastermind p = new Mastermind ();
	p.process ();
    }


    void process ()
    {
	c = new Console ();

	int i, j, correct, semi, guess, wins = 0, games = 0;
	String answer, input, replay = "";
	char[] list = {'B', 'G', 'O', 'R', 'P', 'Y'};
	boolean[] used = new boolean [4];
	boolean[] omfg = new boolean [4];
	boolean found;
	
	Color yellow = new Color (0xee, 0xee, 0x00);
	Color green = new Color (0x00, 0xcc, 0x00);

	while (!replay.equals ("exit"))
	{
	    games++;
	    c.println ("Game #" + games + "\n");

	    answer = "";
	    for (i = 0 ; i < 4 ; i++)
		answer = answer + Character.toString (list [(int) Math.round (Math.random () * 5)]);

	    System.out.println (answer); // Debug
	    guess = 0;
	    while (true)
	    {
		guess++;
		input = "";
		while (input.length () < 4)
		{
		    c.print ("Guess " + guess + "/9: ");
		    input = c.readString ().toUpperCase ().trim ();
		}

		c.setCursor (c.getRow () - 1, 12);
		c.println ("");
		c.setCursor (c.getRow () - 1, 12);

		for (i = 0 ; i < 4 ; i++)
		{
		    c.setTextColor (Color.black);
		    found = false;
		    if (input.charAt (i) == 'B')
		    {
			c.setTextColor (Color.blue);
			found = true;
		    }
		    else if (input.charAt (i) == 'G')
		    {
			c.setTextColor (green);
			found = true;
		    }
		    else if (input.charAt (i) == 'O')
		    {
			c.setTextColor (Color.orange);
			found = true;
		    }
		    else if (input.charAt (i) == 'R')
		    {
			c.setTextColor (Color.red);
			found = true;
		    }
		    else if (input.charAt (i) == 'P')
		    {
			c.setTextColor (Color.magenta);
			found = true;
		    }
		    else if (input.charAt (i) == 'Y')
		    {
			c.setTextColor (yellow);
			found = true;
		    }
		    if (found)
			c.print ("\u2022");
		    else
			c.print ("-");
		    
		    used [i] = false;
		    omfg [i] = false;
		}
		c.println ("");

		correct = 0;
		for (i = 0 ; i < 4 ; i++)
		{
		    if (answer.charAt (i) == input.charAt (i))
		    {
			correct++;
			used [i] = true;
			omfg [i] = true;
		    }
		}

		if (correct == 4 || guess > 8)
		    break;

		semi = 0;
		for (i = 0 ; i < 4 ; i++)
		{
		    if (!used [i])
		    {
			for (j = 0 ; j < 4 ; j++)
			{
			    if (!omfg [j] && i != j && input.charAt (i) == answer.charAt (j))
			    {
				System.out.println (input.charAt (i) + "-" + answer.charAt (j)); // Debug
				semi++;
				used [i] = true;
				omfg [j] = true;
				break;
			    }
			}
		    }
		}

		c.setTextColor (Color.red);
		for (i = 0 ; i < correct ; i++)
		    c.print ("o");
		c.setTextColor (Color.black);
		for (i = 0 ; i < semi ; i++)
		    c.print ("o");
		c.println ("");
	    }


	    if (correct == 4)
	    {
		c.println ("You win after " + guess + " guesses.");
		wins++;
	    }
	    else
		c.println ("You lose. The answer was " + answer);

	    c.println ("\nIf you would like to stop, enter \u2018exit\u2019. If not, enter anything else.");
	    replay = c.readString ().toLowerCase ().trim ();
	    c.clear ();
	}


	c.print ("\nGame over.\nYou have won " + wins + " out of " + games + " games, with a winning percentage of ");
	c.print (100 * (float) wins / (float) games, 0, 1);
	c.println ("%.");
    }
}


