import java.awt.*;
import javax.swing.*;

/**
 *   This class contains information and methods associated with the piece 
 *   for a checker board
 *   public
 *   @author Casey Daughtery, Joshua Canter, Yvon Feaster
 *	 @version 2.0
*/
public abstract class Piece
{
   protected Point pos, coord;
   protected int width;
   protected Color color;
   protected Player player;
   protected int checkerNumber;
   protected boolean isNull;
 
   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return     the x position of the piece in pixels
   */    
   public int getXpos()
   {
      return this.pos.x;
   }

    /**
    *   This method sets x position in pixels.
    *   public
    *   @param x    the x position of the piece in pixels
   */ 
   public void setXpos(int x)
   {
      this.pos.x = x;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return     the y position of the piece in pixels
   */  
   public int getYpos()
   {
      return this.pos.y;
   }

  /**
    *   This method sets y position in pixels.
    *   public
    *   @param y    the y position of the piece in pixels
   */ 
   public void setYpos(int y)
   {
      this.pos.y = y;
   }

   /**
    *   This method gets protected information from the checker class.
    *   public
    *   @return     color of the checker piece
   */   
   public Color getColor()
   {
      return this.color;
   }

   /**
    *   This method sets color.
    *   public
    *   @param c   sets the new color for the piece
   */   
   public void setColor(Color c)
   {
      this.color = c;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return    the x coordinate of the Piece
   */    
   public int getCol()
   {
      return this.coord.x;
   } 
 
    /**
    *   This method sets col.
    *   public
    *   @param c   the x coordinate of the Piece
   */  
   public void setCol(int c)
   {
      this.coord.x = c;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return    y coordinate of the Piece
   */    
   public int getRow()
   {
      return this.coord.y;
   }

   /**
    *   This method sets row.
    *   public
    *   @param r   sets the y coordinate of the piece
   */   
   public void setRow(int r)
   {
      this.coord.y = r;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return    the width of the piece
   */    
   public int getWidth()
   {
      return this.width;
   }
 
   /**
    *   This method sets width of the piece.
    *   public
    *   @param  w   the width of the piece
   */   
   public void setWidth(int w)
   {
      this.width = w;
   }

   /**
    *   This method check to see if a checker move is valid.
    *   public
    *   @param isBlackSquare   true if it is occupiable square
    *   @param isNotEmpty      true if the square has a piece
    *   @param c               destination of x coordinate
    *   @param r							 destination of y coordinate
    *   @param b               a reference to Board
    *	  @return                true if the move is valid
   */  
   public abstract boolean isMoveValid (boolean isBlackSquare, 
                                        boolean isNotEmpty, int c, 
                                        int r, Board b);

   /**
    *   This method removes a jumped piece from the board.
    *   public
    *   @param containedBy     the square that contains this piece
   */
   public void hasBeenJumped(Square containedBy)
   {
      this.color = new Color(0, 0, 0);
      this.pos = new Point(0, 0);
      this.coord = new Point(0, 0);      
      this.player = null;
      this.isNull = true;
      containedBy.setHasChecker(false);
   }
   
   /**
     *  This method gets the number of the current piece
     *  public
     *  @return    the piece number
    */
   public int getCheckerNumber()
   {
      return this.checkerNumber;
   }
   /**
     *  This method sets the number of the current piece
     *  public
     *  @param  chNo    Sets the number of the current piece
    */
   public void setCheckerNumber(int chNo)
   {
      this.checkerNumber = chNo;
   }
   /**
     *  This method checks to see if the current piece is "null".
     *  Rather than having truly null pieces, this method allows the
     *  program to verify whether or not it is dealing with a valid piece.  
     *  @return   True if the piece is not currently defined for play.
    */
   public boolean isNull()
   {
      return this.isNull;
   }
   
   /**
    *   This method is a forward declaration for the draw methods in King 
    *   and Pawn.
    *   public
    *   @param g      the passed Graphics object
    */   
   public abstract void drawChecker(Graphics g);
   
   /**
     *  This method is a forward declaration for a functor that 
     *  returns whether or not the piece is a King.
     *  public
     *  @return     True if the piece is a member of the King
    */
   public abstract boolean isKing();
   
   /**
     *  Since the move has been verified as valid this method
     *  only concerns itself with moving the pieces.
     *  @param  col  x coordinate of its destination
     *  @param  row  y coordinate of its destination
     *  @param  b    a reference to the board in order to refer to other pieces
    */
   public void executeMove(int col, int row, Board b)
   {
      b.sq[this.getCol()][this.getRow()].hasChecker = false;
      b.sq[this.getCol()][this.getRow()].contents = new Pawn ();

      if(col - this.getCol() == 2 && row - this.getRow() == 2)
      {
         b.sq[this.getCol()+1][this.getRow()+1].contents.
            hasBeenJumped(b.sq[this.getCol()+1][this.getRow()+1]);
      }
      if(col - this.getCol() == -2 && row - this.getRow() == -2)
      {
         b.sq[this.getCol()-1][this.getRow()-1].contents.
            hasBeenJumped(b.sq[this.getCol()-1][this.getRow()-1]);
      }
      if(col - this.getCol() == -2 && row - this.getRow() == 2)
      {
         b.sq[this.getCol()-1][this.getRow()+1].contents.
            hasBeenJumped(b.sq[this.getCol()-1][this.getRow()+1]);
      }
      if(col - this.getCol() == 2 && row - this.getRow() == -2)
      {
         b.sq[this.getCol()+1][this.getRow()-1].contents.
         hasBeenJumped(b.sq[this.getCol()+1][this.getRow()-1]);
      }
               
      //Sets checkers new col row xpos ypos and new squares hasChecker                
      this.setCol(col);
      this.setRow(row);
      this.setXpos(col*b.width+(b.width * 5 / 90));
      this.setYpos(row*b.width+(b.width * 5 / 90));
      b.sq[this.getCol()][this.getRow()].hasChecker = true;
      b.sq[this.getCol()][this.getRow()].contents = b.draggedPiece;
      b.ch[this.player.getPlayerNumber()]
          [this.getCheckerNumber()] = b.draggedPiece;
      b.players[this.player.getPlayerNumber()].
          setCheckersArray(b.ch[this.player.getPlayerNumber()]);        
   }
}