/**
 * class Move
 *
 * Represents a move
 *
 * @author Jonatan Pettersson (jonatan_pettersson@hotmail.com)
 * Date: 2006-12-14
 * Updated: 2006-12-18
 */

public class Move implements Definitions
{
	/** Variables used to describe the move */

	public int pieceMoving; // Type of piece moving
	public int fromIndex; // The square the piece is moving from
	public int toIndex; // The square the piece is moving to	
	public int capture; // Value of the captured piece (0 if no capture)
	public int moveType; // ordinary, castling long/short, en passant
	public int[] prevPos; // An array with 4 places with information about previous position
			      // prevPos[0] = en passant
			      // prevPos[1] = white castle rights
			      // prevPos[2] = black castle rights
			      // prevPos[3] = half-moves

	/** *********************************** */	

	// Constructor with parameters
	public Move(
		int pieceMoving, int fromIndex, int toIndex,
		int capture, int moveType, int[] prevPos)
	{
		this.pieceMoving = pieceMoving;
		this.fromIndex = fromIndex;
		this.toIndex = toIndex;
		this.capture = capture;
		this.moveType = moveType;
		this.prevPos = prevPos;
	}

	/**
	 *  Returns a string holding the short notation of the move
	 *
	 *  @return String Short notation
	 */
	public String notation()
	{
		String notation = "";

		// Add the piece notation
		switch(pieceMoving)
		{
			case W_KING:
				{
					if(moveType == SHORT_CASTLE) return "0-0";
					if(moveType == LONG_CASTLE) return "0-0-0";
					notation += "K"; break;
				}	
			case B_KING:
				{
					if(moveType == SHORT_CASTLE) return "0-0";
					if(moveType == LONG_CASTLE) return "0-0-0";
					notation += "K"; break;
				}				
			case W_QUEEN: notation += "Q"; break;
			case B_QUEEN: notation += "Q"; break;
			case W_ROOK: notation += "R"; break;
			case B_ROOK: notation += "R"; break;
			case W_BISHOP: notation += "B"; break;
			case B_BISHOP: notation += "B"; break;
			case W_KNIGHT: notation += "N"; break;
			case B_KNIGHT: notation += "N"; break;
		}

		if(capture != 0) // The move is a capture
		{
			// If the moving piece is a pawn we need to add the row it's moving from
			if((pieceMoving == W_PAWN) || (pieceMoving == B_PAWN))
			{
				switch(fromIndex % 16) // Find the row
				{
					case 0: notation += "a"; break;
					case 1: notation += "b"; break;
					case 2: notation += "c"; break;
					case 3: notation += "d"; break;
					case 4: notation += "e"; break;
					case 5: notation += "f"; break;
					case 6: notation += "g"; break;
					case 7: notation += "h"; break;
				}
			}
			notation += "x";
		}

		switch(toIndex % 16) // Find the row
		{
			case 0: notation += "a"; break;
			case 1: notation += "b"; break;
			case 2: notation += "c"; break;
			case 3: notation += "d"; break;
			case 4: notation += "e"; break;
			case 5: notation += "f"; break;
			case 6: notation += "g"; break;
			case 7: notation += "h"; break;
		}

		notation += (toIndex-(toIndex%16))/16 + 1; // Add the rank
		
		if(moveType == EN_PASSANT)
			notation += " e.p.";

		// Add promotion
		switch(moveType)
		{
			case PROMOTION_QUEEN: notation += "=Q"; break;
			case PROMOTION_ROOK: notation += "=R"; break;
			case PROMOTION_BISHOP: notation += "=B"; break;
			case PROMOTION_KNIGHT: notation += "=N"; break;
		}
		
		return notation;
	}
}
