import java.awt.*;
import javax.swing.*;

/**
 *   This class contains information associated with the checker 
 *   for a checker board
 *   public
 *   @author Casey Daughtery, Joshua Canter, Yvon Feaster
 *	 @version 1.0
*/
public class Checker 
{
   private int xpos, ypos;
   private int row, col;
   private int width;
   private Color color;
   private boolean crowned;

   /**
    *   This constructor initializs all the necessary
    *   information for the checker class
    *   public
    *   @param int, int, int, Color
    *   @return none
   */
    public Checker (int x, int y, int w, Color c)
   {  
      color = new Color(255, 255, 255);
      xpos = x;
      ypos = y;
      color = c;
      width = w;
      row = y / width;
      col = x / width;
   }
 
   /**
    *   This method gets protected information from the checker class.
    *   (xpos - the "x" position of the checker)
    *    public
    *   @param none
    *   @return int
   */    
   public int getXpos()
   {
      return this.xpos;
   }

    /**
    *   This method sets xpos.
    *   public
    *   @param int
    *   @return int
   */ 
   public int setXpos(int x)
   {
      return (xpos = x);
   }

   /**
    *   This method gets protected information from the checker class.
    *   (ypos - the "y" position of the square)
    *   public
    *   @param none
    *   @return int
   */    
   public int getYpos()
   {
      return this.ypos;
   }

   /**
    *   This method sets ypos.
    *   public
    *   @param int
    *   @return int
   */   
   public int setYpos(int y)
   {
      return (ypos = y);
   }

   /**
    *   This method gets protected information from the checker class.
    *   (color of the checker piece)
    *   public
    *   @param none
    *   @return Color
   */   
   public Color getColor()
   {
      return this.color;
   }

   /**
    *   This method sets color.
    *   public
    *   @param Color
    *   @return Color
   */   
   public Color setColor(Color c)
   {
      return (color = c);
   }

   /**
    *   This method gets protected information from the checker class.
    *   (col - column)
    *   public
    *   @param none
    *   @return int
   */    
   public int getCol()
   {
      return this.col;
   } 
 
    /**
    *   This method sets col.
    *   public
    *   @param int
    *   @return int
   */  
   public int setCol(int c)
   {
      return (col = c);
   }

   /**
    *   This method gets protected information from the checker class.
    *   (row)
    *   public
    *   @param none
    *   @return int
   */    
   public int getRow()
   {
      return this.row;
   }

   /**
    *   This method sets row.
    *   public
    *   @param int
    *   @return int
   */   
   public int setRow(int r)
   {
      return (row = r);
   }

   /**
    *   This method gets protected information from the checker class.
    *   (width - the width of the checker)
    *   public
    *   @param none
    *   @return int
   */    
   public int getWidth()
   {
      return this.width;
   }
 
   /**
    *   This method sets width.
    *   public
    *   @param int
    *   @return int
   */   
   public int setWidth(int w)
   {
      return (width = w);
   }

   /**
    *   This method gets protected information from the checker class.
    *   (crowned - whether or not the checker is a king)
    *   public
    *   @param none
    *   @return boolean
   */    
   public boolean isCrowned()
   {
      return this.crowned;
   }

   /**
    *   This method check to see if a checker move is valid.
    *   public
    *   @param boolean, boolean, int, int
    *	  @return boolean
   */  
   public boolean isMoveValid (boolean isBlackSquare, boolean isNotEmpty, 
                               int c, int r, Checker [][] ch, Square [][] sq)
   {
      // call to see if occupiable is true
      int side = -1, eside = -1;
      int jumpedNumber;
           
      if (color == Color.red)
      {
         side = 0;
         eside = 1;
      }
      
      if (color == Color.black)
      {
         side = 1;
         eside = 0;
      }
      
      if (isBlackSquare && !isNotEmpty && (side == 0 || crowned))
      {         
         if(r == row + 1)
         {
            if (c == col - 1 || c == col + 1)
            {
               if (row + 1 == 7)
               {
                  crowned = true;
               }
               return true;
            }
         }
          
         if(r == row + 2)
         {
            if(c == col - 2)
            {
               for (int count = 0; count < 12; count++)
               {
                  if (ch[eside][count].getCol() == col - 1 && 
                      ch[eside][count].getRow() == row + 1)
                  {
                     ch[eside][count].isJumped(sq[col-1][row+1]);
                     if (row + 2 == 7)
                     {
                        crowned = true;
                     }
                     return true;
                  }
               }                
            }

            if(c == col + 2)
            {
               for (int count = 0; count < 12; count++)
               {
                  if (ch[eside][count].getCol() == col + 1 && 
                      ch[eside][count].getRow() == row + 1)
                  {
                     ch[eside][count].isJumped(sq[col+1][row+1]);
                     if (row + 2 == 7)
                     {
                        crowned = true;
                     }
                     return true;
                  }
               }                
            }
         }
      }
      
      if (isBlackSquare && !isNotEmpty && (side == 1 || crowned))
      {         
         if(r == row - 1)
         {
            if (c == col - 1 || c == col + 1)
            {
               if (row - 1 == 0)
               {
                  crowned = true;
               }          
               return true;
            }
         }
          
         if(r == row - 2)
         {
            if(c == col - 2)
            {
               for (int count = 0; count < 12; count++)
               {
                  if (ch[eside][count].getCol() == col - 1 && 
                      ch[eside][count].getRow() == row - 1)
                  {
                     ch[eside][count].isJumped(sq[col-1][row-1]);
                     if (row - 2 == 0)
                     {
                        crowned = true;
                     }
                     return true;
                  }
               }                
            }

            if(c == col + 2)
            {
               for (int count = 0; count < 12; count++)
               {
                  if (ch[eside][count].getCol() == col + 1 && 
                      ch[eside][count].getRow() == row - 1)
                  {
                     ch[eside][count].isJumped(sq[col+1][row-1]);
                     if (row - 2 == 0)
                     {
                        crowned = true;
                     }
                     return true;
                  }
               }                
            }
         }
      }

      return false;
   }

   /**
    *   This method removes a jumped checker from the board.
    *   public
    *   @param Square
    *	  @return void
   */
   public void isJumped(Square containedBy)
   {
      this.setXpos(-this.getWidth());
      this.setYpos(-this.getWidth());
      this.setCol(-1);
      this.setRow(-1);
      containedBy.setHasChecker(false);
   }
}