// The "Memory" class.
package Memory;
import Memory.*;
import java.awt.*;
import java.lang.Math;
import hsa.Console;

public class Memory
{
    static Console c;           // The output console
    static boolean[] [] cleared;
    static int tmpx, tmpy;
    static Color tmpcol;
    static char tmpchar;
    static int rows, cols = 4, height, width;


    public static void main (String[] args)
    {
	c = new Console ();
	c.setFont (new Font ("Courier New", 0, 72));
	boolean correct = false;
	char input;
	int y = 1, x = 1, counterinput = 0, oldx = 1, oldy = 1, diff;
	int[] point = { - 1, -1};
	boolean select = false, oneflip = false;

	do
	{
	    c.print ("Please select a difficulty. 1 for Easy, 2 for Medium, 3 for Hard.");
	    diff = c.readInt ();
	}
	while (diff < 1 || diff > 3);
	c.clear ();
	if (diff == 1)
	{
	    rows = 2;
	    cols = 2;
	    height = 8;
	    width = 12;
	}
	else if (diff == 2)
	{
	    rows = 2;
	    cols = 4;
	    height = 8;
	    width = 12;
	}
	else
	{
	    rows = 4;
	    cols = 5;
	    height = 5;
	    width = 9;
	}

	MemoryRecord[] [] word;
	word = new MemoryRecord [rows] [cols];

	cleared = new boolean [rows] [cols];
	for (int i = 0 ; i < rows ; i++)
	    for (int j = 0 ; j < cols ; j++)
		cleared [i] [j] = false;

	for (int i = 0 ; i < 2 ; i++)
	{
	    word [0] [0 + i] = new MemoryRecord (Color.black, '\u2660');
	    word [1] [0 + i] = new MemoryRecord (Color.black, '\u2663');
	}
	if (diff > 1)
	    for (int i = 0 ; i < 2 ; i++)
	    {
		word [0] [2 + i] = new MemoryRecord (Color.blue, '\u266a');
		word [1] [2 + i] = new MemoryRecord (Color.red, '\u266b');
	    }
	if (diff > 2)
	{
	    for (int i = 0 ; i < 2 ; i++)
	    {
		word [2] [0 + i] = new MemoryRecord (Color.black, '\u2642');
		word [2] [2 + i] = new MemoryRecord (Color.orange, '\u263C');
		word [3] [0 + i] = new MemoryRecord (Color.red, '\u2665');
		word [3] [2 + i] = new MemoryRecord (Color.red, '\u2666');
	    }
	    for (int i = 0 ; i < 2 ; i++)
	    {
		word [0 + i] [4] = new MemoryRecord (Color.black, '\u263a');
		word [2 + i] [4] = new MemoryRecord (Color.black, '\u2640');
	    }
	}
	word (word, diff);
	drawboard ();
	while (!correct)
	{
	    while (point [0] == -1 || point [1] == -1)
	    {
		c.setCursor (y, x);
		c.print ('\u2022');
		for (int i = 0 ; i < 2 ; i++)
		{
		    do
		    {
			drawboard ();
			input = c.getChar ();

			if (!(i == 1 && y == oldx && x == oldy))
			{
			    c.setCursor (y, x);
			    c.print (' ');
			}

			if (input == '1')
			{
			    if (y != 25 && x != 1)
			    {
				y++;
				x = x - 3;
			    }
			}
			else if (input == '2')
			{
			    if (y != 25)
				y++;
			}
			else if (input == '3')
			{
			    if (y != 25 && x < 78)
			    {
				y++;
				x = x + 3;
			    }
			}
			else if (input == '4')
			{
			    if (x != 1)
				x = x - 3;
			}
			else if (input == '6')
			{
			    if (x < 78)
				x = x + 3;
			}
			else if (input == '7')
			{
			    if (x != 1 && y != 1)
			    {
				x = x - 3;
				y--;
			    }
			}
			else if (input == '8')
			{
			    if (y != 1)
				y--;
			}
			else if (input == '9')
			    if (y != 1 && x < 78)
			    {
				y--;
				x = x + 3;
			    }

			drawboard ();
			c.setCursor (y, x);
			c.print ('\u2022');
			if (oneflip)
			{
			    c.setColor (tmpcol);
			    c.drawString ("" + tmpchar, tmpx, tmpy);
			}
			c.setCursor (24, 1);
			c.println (y + ", " + x);
		    }
		    while (input != '5');

		    c.setCursor (y, x);
		    c.print ('\u2022');
		    point [i] = getinput (y, x);

		    if (point [i] != -1 && cleared [point [i] / cols] [point [i] % cols])
		    {
			i--;
			continue;
		    }

		    if (i == 0 && point [i] != -1)
		    {
			simpleflip (point [i], word, diff);
			oneflip = true;
		    }
		    if (point [i] == -1)
		    {
			delay (1500);
			oneflip = false;
			break;
		    }
		    else if (i == 0) // stores position of first dot
		    {
			oldx = y;
			oldy = x;
		    }
		    else if (point [0] == point [1])
		    {
			i--;
			continue;
		    }
		}
		if (point [0] != -1 && point [1] != -1)
		{
		    c.setCursor (oldx, oldy);
		    c.print (' ');
		    drawboard ();
		    c.setColor (tmpcol);
		    c.drawString ("" + tmpchar, tmpx, tmpy);
		    flipcard (point, word, diff);
		    oneflip = false;
		    drawboard ();
		}
		c.clear ();
	    }
	    point [0] = -1;
	    point [1] = -1;
	    c.setCursor (23, 1); // Debug
	    c.print (point [0] + ", " + point [1]);

	    correct = true;
	    for (int i = 0 ; i < rows ; i++)
	    {
		for (int j = 0 ; j < cols ; j++)
		    if (!cleared [i] [j])
		    {
			correct = false;
			break;
		    }
		if (!correct)
		    break;
	    }
	}
	drawboard ();


    }


    public static void drawboard ()
    {
	c.setColor (Color.black);
	for (int i = 0 ; i < rows ; i++)
	    for (int j = 0 ; j < cols ; j++)
	    {
		if (cleared [i] [j])
		    c.setColor (Color.white);
		c.drawRect ((j * 8) * (width + 3), (i * 20) * (height + 1), width * 8, height * 20);
		c.setColor (Color.black);
	    }
    }



    public static int getinput (int y, int x)
    {
	int counter = -1;

	for (int i = 0 ; i < rows ; i++)
	    if (y > i * (height + 1) && y < (i + 1) * (height + 1))
		for (int j = 0 ; j < cols ; j++)
		    if (x > j * (width + 3) - 2 && x < (j + 1) * (width + 3) - 2)
		    {
			counter = i * cols + j;
			return counter;
		    }

	c.setCursor (1, 1);
	c.print ("You have selected an invalid point.");
	return -1;
    }


    public static void word (MemoryRecord[] [] array, int diff)
    {
	int index, a, b, c, d;
	MemoryRecord temp = new MemoryRecord (Color.black, ' ');

	for (int i = 0 ; i < (int) Math.pow (rows * height, 2) ; i++)
	{
	    a = (int) (Math.random () * rows);
	    c = (int) (Math.random () * rows);
	    b = (int) (Math.random () * cols);
	    d = (int) (Math.random () * cols);
	    temp = array [a] [b];
	    array [a] [b] = array [c] [d];
	    array [c] [d] = temp;
	}
    }


    public static void simpleflip (int point, MemoryRecord[] [] word, int diff)
    {
	int elx, ely, locx, locy;
	ely = point / cols;
	elx = point % cols;
	c.setColor (word [ely] [elx].getColor ());
	tmpcol = word [ely] [elx].getColor ();

	locy = height * 10 + ely * 20 * (height + 1) + 10;
	locx = width * 4 + elx * 8 * (width + 3) - 20;
	c.drawString ("" + word [ely] [elx].getShape (), locx, locy);

	tmpchar = word [ely] [elx].getShape ();
	tmpx = locx;
	tmpy = locy;
    }


    public static void flipcard (int[] point, MemoryRecord[] [] word, int diff)
    {
	int oldx = -1, oldy = -1, elx, ely;
	Color oldcolor = Color.white;
	char oldshape = ' ';

	ely = point [1] / cols;
	elx = point [1] % cols;
	c.setColor (word [ely] [elx].getColor ());
	c.drawString ("" + word [ely] [elx].getShape (), width * 4 + elx * 8 * (width + 3) - 20, height * 10 + ely * 20 * (height + 1) + 10);

	oldy = point [0] / cols;
	oldx = point [0] % cols;
	oldcolor = (word [oldy] [oldx].getColor ());
	oldshape = (word [oldy] [oldx].getShape ());

	if (oldcolor == word [ely] [elx].getColor () && oldshape == word [ely] [elx].getShape ())
	{
	    cleared [ely] [elx] = true;
	    cleared [oldy] [oldx] = true;
	}

	delay (1000);
	c.setColor (Color.white);
	c.drawString ("" + word [ely] [elx].getShape (), width * 4 + elx * 8 * (width + 3) - 20, height * 10 + ely * 20 * (height + 1) + 10);
	c.drawString ("" + word [oldy] [oldx].getShape (), width * 4 + oldx * 8 * (width + 3) - 20, height * 10 + oldy * 20 * (height + 1) + 10);

	c.setColor (Color.black);
	c.setCursor (24, 70);
	c.print ((int) word [oldy] [oldx].getShape () + ", " + (int) word [ely] [elx].getShape ()); // Debug
    }


    public static void delay (int num)
    {
	try
	{
	    Thread.sleep (num);
	}


	catch (InterruptedException e)
	{
	}
    }
} // Memory class


