import java.lang.Object;
import java.awt.Color;
import java.awt.Graphics;



public class Board
{
	private int    			width;
	private int    			height;
	private int    			rows;
	private int    			columns;
	private Color 			oddColor;
	private Color 			evenColor;
	public  Square[][]		Squares;
	public  int             number_of_visits;


	public Board(int _width, int _height, int _rows, int _columns, Color _oddColor, Color _evenColor)
	{
		width		= _width;
		height		= _height;
		rows		= _rows;
		columns		= _columns;
		oddColor	= _oddColor;
		evenColor	= _evenColor;

		Squares = new Square[rows][columns];
		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
            	int   sq_left   = i*width/rows;
       			int   sq_top    = j*height/columns;
            	int   sq_width  = width/rows;
            	int   sq_height = height/columns;
            	Color sq_color  = ColorOfSquare(i, j);
				Squares[i][j]   = new Square(sq_left, sq_top, sq_width, sq_height, sq_color);
			}
		}
        number_of_visits  = 0;
	}


	public int   width()        { return width;}
	public int   height()       { return height;}
	public int   rows()         { return rows;}
	public int   columns()      { return columns;}


	private synchronized Color ColorOfSquare(int _i, int _j)
	{
		if (((_i + _j) % 2) == 1)
			return oddColor;
		else
			return evenColor;
	}


	public synchronized void Visit(Position _to)
	{
  	    number_of_visits++;
  	    Squares[_to.i][_to.j].Visit(number_of_visits);
	}


	public synchronized void UndoVisit(Position _from)
	{
  	    Squares[_from.i][_from.j].UndoVisit();
  	    number_of_visits--;
	}



	public synchronized void Clear()
	{
		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
				Squares[i][j].Clear();
			}
		}
        number_of_visits  = 0;
	}


	public synchronized void Draw(Graphics _g)
	{
		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
				Squares[i][j].Draw(_g);
			}
		}
	}


	public synchronized void Display()
	{
        int number = 0;
        for (int i=0; i<rows; i++)
        {
            for (int j=0; j<columns; j++)
            {
                number = Squares[j][j].number();
                System.out.print(number + " ");
            }
         }
         System.out.println();
    }


}
