import java.awt.*;

abstract class ChessPiece extends Sprite
{
    public final static int WHITE = 0, BLACK = 1,
	ROOK = 0, KNIGHT = 1, BISHOP = 2, KING = 3, QUEEN = 4, PAWN = 5;
    protected final static int STRAIGHT = 0, DIAGONAL = 1, LSHAPED = 2;
    protected int squareX, squareY;
    protected int homeX, homeY;
    protected int color;
    protected boolean hasMoved = false;

    public ChessPiece (int c, Image p, Component l)
    {
	super (p, l);
	if (c != WHITE && c != BLACK)
	    c = WHITE;
	color = c;
	squareX = squareY = -1;
	hasMoved = false;
    }

    public int getX ()
    {
	return squareX;
    }
    public int getY ()
    {
	return squareY;
    }
    public int getHomeX ()
    {
	return homeX;
    }
    public int getHomeY ()
    {
	return homeY;
    }
    public boolean atHome ()
    {
	return (squareX == homeX && squareY == homeY);
    }
    public void setSquare (int x, int y)
    {
	squareX = x;
	squareY = y;
    }
    public void setHome (int x, int y)
    {
	homeX = x;
	homeY = y;
    }
    public int getColor ()
    {
	return color;
    }
    public void setMoved (boolean m)
    {
	hasMoved = m;
    }
    public boolean getMoved ()
    {
	return hasMoved;
    }

    public boolean legalMove (ChessBoard cb, int x, int y)
    {
	return true;
    }

    //legalCapture function:
    //returns 1 if the move is a legal capture for this piece,
    //-1 if it is an illegal capture (same color), 0 if no
    //capture occurred
    public int legalCapture (ChessBoard cb, int x, int y)
    {
	ChessPiece target = cb.getPieceAt (x, y);
	if (target != null && target != this) {
	    if (color == target.color)
		return -1;
	    return 1;
	}
	return 0;
    }

    public boolean moveBlocked (ChessBoard cb, int x, int y)
    {
	int testX = squareX, testY = squareY;
	while (testX != x || testY != y) {
	    if (testX < x) testX ++;
	    if (testX > x) testX --;
	    if (testY < y) testY ++;
	    if (testY > y) testY --;
	    if (testX != x || testY != y) {
		if (cb.getPieceAt (testX, testY) != null) {
		    return true;
		}
	    }
	}
	return false;
    }

    public int moveDistance (int x, int y)
    {
	int distX = Math.abs (x-squareX),
	    distY = Math.abs (y-squareY);
	return distX > distY ? distX : distY;
    }

    public boolean moveStraight (int x, int y)
    {
	return (x == squareX || y == squareY);
    }

    public boolean moveDiagonally (int x, int y)
    {
	return (Math.abs (x-squareX) == Math.abs (y-squareY));
    }

    public boolean moveLShaped (int x, int y)
    {
	int xDiff = Math.abs (x-squareX), yDiff = Math.abs (y-squareY);
	return (xDiff == 1 && yDiff == 2
	    || xDiff == 2 && yDiff == 1);
    }

    public boolean moveForward (int x, int y)
    {
	if (color == WHITE) {
	    return y > squareY;
	} else {
	    return y < squareY;
	}
    }
}

