
/** This class is used for representing moves in object form.  It has
methods used to set and return the source and destination square in row/column
form.*/
public class Move implements Comparable
{
        private int sourceRow, sourceCol, destRow, destCol;
        boolean isCapture;
        
	/** Builds a move object using the given data. */
	public Move(int sourceRow, int sourceCol, int destRow, int destCol, boolean isCapture)
	{
                this.sourceRow = sourceRow;
                this.sourceCol = sourceCol;
                this.destRow = destRow;
                this.destCol = destCol;
                this.isCapture = isCapture;
	}
        
        // This method is only invoked with usermoves and moves sent by xboard
        // Precondition: move must have length at least four characters.
        // Updated to promote pawns.  The shared board instance is now needed to
        // promote the pawns.
        public Move(String move, Board b)
        {
                sourceCol = move.charAt(0) - 'a';
                sourceRow = move.charAt(1) - '1';
                destCol = move.charAt(2) - 'a';
                destRow = move.charAt(3) - '1';
                if (move.length() == 5) {  // This is a pawn promotion
                    char t = move.charAt(4);
                    if (t == 'q') {
                        b.getPiece(sourceRow, sourceCol).promotePawn(ChessPiece.TYPE_QUEEN);
                    } else if (t == 'r') {
                        b.getPiece(sourceRow, sourceCol).promotePawn(ChessPiece.TYPE_ROOK);
                    } else if (t == 'b') {
                        b.getPiece(sourceRow, sourceCol).promotePawn(ChessPiece.TYPE_BISHOP);
                    } else if (t == 'n') {
                        b.getPiece(sourceRow, sourceCol).promotePawn(ChessPiece.TYPE_KNIGHT);
                    }
                }
                isCapture = false;
        }
	
	/** Returns the source row. */
	public int getSourceRow()
	{
		return sourceRow;
	}
	
	/** Returns the source column. */
	public int getSourceCol()
	{
		return sourceCol;
	}
	
	/** Returns the destination row. */
	public int getDestRow()
	{
		return destRow;
	}
	
	/** Returns the destination column. */
	public int getDestCol()
	{
		return destCol;
	}
        
        public boolean isCapture() {
                return isCapture;
        }
	
	/** Sets the source square. */
	public void setSource(int sourceRow, int sourceCol)
	{
	}
	
	/** Sets the destination square. */
	public void setDestination(int destRow,  int destCol)
	{
	}
        
        public String toString() {
                return ("" + (char)('a' + sourceCol) + (1 + sourceRow) +
                        (char)('a' + destCol) + (1 + destRow));
        }
        
        //
        public int compareTo(Object m) {
            // The moves are equal (i.e. both captures or both non-captures).
            if (((Move)m).isCapture() == isCapture) {
                return 0;
            // This move is less than m (i.e. m is a capture and this is not).
            } else if (((Move)m).isCapture() && !isCapture) {
                return -1;
            // This move is greater that m (i.e. this is a capture and m is not).
            } else {   // Implies !((Move)m).isCapture() && isCapture
                return 1;
            }
        }
}