import java.lang.Object;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.applet.*;



public class Knight extends Piece
{
    //  jump directions
    //
    //      0   1
    //  7           2
    //       i,j
    //  6           3
    //      5   4
    //
    final static int   JUMPS = 8;

    public      GeneticAlgorithm    GA;
	public      boolean             success;

    int   i;
    int   j;
    int[] di = {-1, +1, +2, +2, +1, -1, -2, -2};
    int[] dj = {-2, -2, -1, +1, +2, +2, +1, -1};
    int[] directions = new int[path.Size()];
    boolean[][] directionss = new boolean[path.Size()][JUMPS];

    public  String  FindPathStatus = new String();



	public Knight(int _value, Color _color, Image _image, Board _board, Position _start, int _speed)
	{
        super(_value, _color, _image, _board, _start);
        // PopulationSize, ChromosomeLength, GeneMinValue, GeneMaxValue
        GA = new GeneticAlgorithm(256, 63, 0, 7, _start);
	}



	protected synchronized boolean LegalMove(Position _from, Position _to)
	{
        if ( !(board.Squares[_to.i][_to.j].visited()) )
        {
		    if ( (_from.i == -1) || (_from.j == -1) ) // from outside board can go anywhere
    		{
    			return true;
    		}

    		if (
    		      ( (Math.abs(_to.i - _from.i) == 1) && (Math.abs(_to.j - _from.j) == 2) )
    		    ||
    		      ( (Math.abs(_to.i - _from.i) == 2) && (Math.abs(_to.j - _from.j) == 1) )
    		   )
    		{
    			return true;
    		}
        }

		return false;
	}




	public synchronized void Move(Position _to)
	{
   	    board.Visit(_to);
        path.Add(_to);
    }



	public synchronized void UndoMove()
	{
   	    board.UndoVisit(path.Last());
        path.UndoAdd();
    }



	public synchronized void Draw(Graphics _g)
	{
        // Not Yet Implemented
	}





	public String FindPathStatus()
	{
        return FindPathStatus;
    }



	public boolean FindPath(Applet _applet)
	{
        GA.Run(_applet, this);
        return true;
    }



}
