
/** This is the class used for building all chess pieces.  This has integer
constants in it for piece types and values.  It also has methods for pawn
promotion, tracking wether a piece has moved or not, and a few accessor methods
to retrieve from the object, such as color, type, etc.  Last of all is the
copy() method that is used to copy the piece itself, making a new object. */
public class ChessPiece implements Cloneable
{
        private int type;
        private boolean isWhite;
        
	/** Integer constant representing a pawn. */
	public static final int TYPE_PAWN = 0;
	/** Integer constant representing a knight. */
	public static final int TYPE_KNIGHT = 1;
	/** Integer constant representing a bishop. */
	public static final int TYPE_BISHOP = 2;
	/** Integer constant representing a rook. */
	public static final int TYPE_ROOK = 3;
	/** Integer constant representing a queen. */
	public static final int TYPE_QUEEN = 4;
	/** Integer constant representing a king. */
	public static final int TYPE_KING = 5;
	
	/** Integer constant holding the point value of a pawn. */
	public static final int VALUE_PAWN = 100;
	/** Integer constant holding the point value of a knight. */
	public static final int VALUE_KNIGHT = 300;
	/** Integer constant holding the point value of a bishop. */
	public static final int VALUE_BISHOP = 300;
	/** Integer constant holding the point value of a rook. */
	public static final int VALUE_ROOK = 500;
	/** Integer constant holding the point value of a queen. */
	public static final int VALUE_QUEEN = 900;
	
	//It's supposed to be infinity, but 1000000 should do!
	/** Integer constant holding the point value of a king.  It's supposed to be
	infinite, but 1000000 should do. */
	public static final int VALUE_KING = 1000000;
	
	/** Builds a new chess piece based on the given type and color.  Simply calls
	setupPiece(). */
	public ChessPiece(int type, boolean isWhite)
	{
		setupPiece(type, isWhite);
	}

	/** Sets up the piece according to the given data.  Did it this way so
	promotePawn() can just run this method.  This is needed by the constructor
	and promotePawn(). */
	protected void setupPiece(int type, boolean isWhite)
	{
                this.type = type;
                this.isWhite = isWhite;
	}
	
	/** This is called when a pawn reaches the other side of the board, allowing
	it to change into one of the other types of pieces.  This method should only
	be called on pawns! */
	public void promotePawn(int type)
	{
	}
	
	/** Returns an integer representing the type of piece: pawn, knight, bishop,
	rook, queen, or king. */
	public int type()
	{
		return type;
	}
	/** Returns true if the piece is white.  False otherwise. */
	public boolean isWhite()
	{
		return isWhite;
	}
	/** Returns the material value of the piece.  Needed in the evaluation
	routines to help the engine decide what the best move is, based on value
	of pieces taken and lost. */
	public int value()
	{
		return 0;
	}
	/** Needed for king (castling), rooks (castling), and pawns (they can
	move two spaces initially).  This is needed in the Board class's
	movePiece() method. */
	public boolean hasMoved()
	{
		return false;
	}

	/** Needs to be called the first time the piece has moved.
	It's simply used to setup hasMoved(), so it will return the proper value.
	Only needed for pawns, rooks, and kings.*/
	public void firstMove()
	{
	}

	/** Returns a field for field copy of this object. */
	public ChessPiece copy()
	{
                try {
		        return (ChessPiece)this.clone();
                } catch (Exception ex) {
                        ex.printStackTrace();
                        return null;
                }
	}
        
        public String toString() {
                if (isWhite) {
                        if (type == 0)
                                return "p";
                        else if (type == 1)
                                return "n";
                        else if (type == 2)
                                return "b";
                        else if (type == 3)
                                return "r";
                        else if (type == 4)
                                return "q";
                        else if (type == 5)
                                return "k";
                } else {
                        if (type == 0)
                                return "P";
                        else if (type == 1)
                                return "N";
                        else if (type == 2)
                                return "B";
                        else if (type == 3)
                                return "R";
                        else if (type == 4)
                                return "Q";
                        else if (type == 5)
                                return "K";
                }
                
                return null;
        }

}
