import java.applet.*;
import java.awt.*;
public class chess extends Applet {
   private int last_x=0;
   private int last_y=0;
   private int i;
   private int j;
   private int xtemp;
   private int ytemp;
   private int cobj;
   private int xclick;
   private int yclick;
   private Color current_color=Color.black;
   private Button draw_button;
   private Button new_button;
   private int sqsize=40;
   private int bishsize=6;
   private int pawnsize=5;
   private int kingsize=17;
   private int queesize=5;
   private int knigsize=7;
   private int rooksize=9;
   private int[] xrook=new int[rooksize];
   private int[] yrook=new int[rooksize];
   private int[] xbish=new int[bishsize];
   private int[] ybish=new int[bishsize];
   private int[] xpawn=new int[pawnsize];
   private int[] ypawn=new int[pawnsize];
   private int[] xking=new int[kingsize];
   private int[] yking=new int[kingsize];
   private int[] xquee=new int[queesize];
   private int[] yquee=new int[queesize];
   private int[] xknig=new int[knigsize];
   private int[] yknig=new int[knigsize];
   private int[] xset=new int[17];
   private int[] yset=new int[17];



     public class Piece implements Cloneable{
                 
                 public int xplace=0;
                 public int yplace=0;
                 public int xkill=0;
                 public int ykill=0;
                 public int rank=0;

                  public Object clone(){ //begin clone
                    try{ //begin try
                        Piece cp=(Piece)super.clone();
                        return cp;
                        } catch(CloneNotSupportedException e){
                           return null;
                          }//end catch

                    } //end cloning structure for chess piece 
  
   
           } //end virtual chess piece def

      public Piece upiece=new Piece(); 
      public Piece wset[]=new Piece[16];
      public Piece bset[]=new Piece[16];

//init applet
   public void init(){

//set backgd color
    this.setBackground(Color.white);

//create redo button
    draw_button=new Button("Draw/Redraw");
    draw_button.setForeground(Color.black);
    draw_button.setBackground(Color.lightGray);
    this.add(draw_button);

// create a new board
     new_button=new Button("Clear");
     new_button.setForeground(Color.black);
     new_button.setBackground(Color.lightGray);
     this.add(new_button);


      upiece.xplace=0;
      upiece.yplace=0;
      upiece.xkill=0;
      upiece.ykill=0;
      upiece.rank=0;

  for(i=0;i<16;i++){ 
          wset[i]=(Piece)upiece.clone();
          bset[i]=(Piece)upiece.clone();
                    }

    for(i=0;i<8;i++){
         wset[i].xplace=i+1;
         bset[i].xplace=i+1;
         wset[i].yplace=7;
         bset[i].yplace=2;
         wset[i].xkill=0;
         wset[i].ykill=i;
         bset[i].xkill=9;
         bset[i].ykill=i+1;
         wset[i].rank=1;
         bset[i].rank=1;  
           } //defines pawns in set


   for(i=8;i<16;i++){
         wset[i].xplace=i-7;   
         wset[i].yplace=8;
         wset[i].xkill=i-7;
         wset[i].ykill=0; 
         bset[i].xplace=i-7;
         bset[i].yplace=1;
         bset[i].xkill=i-7;
         bset[i].ykill=9;
      if((i==8)|(i==15)){wset[i].rank=4; bset[i].rank=4;}
      if((i==9)|(i==14)){wset[i].rank=2; bset[i].rank=2;}
      if((i==10)|(i==13)){wset[i].rank=3; bset[i].rank=3;}
      if(i==11){wset[i].rank=6; bset[i].rank=6;}
      if(i==12){wset[i].rank=5; bset[i].rank=5;}
            } // defines back row

 

// define rook
xrook[0]=2;
xrook[1]=18;
xrook[2]=15;
xrook[3]=18;
xrook[4]=18;
xrook[5]=2;
xrook[6]=2;
xrook[7]=5;
xrook[8]=2;
yrook[0]=30;
yrook[1]=30;
yrook[2]=15;
yrook[3]=15;
yrook[4]=10;
yrook[5]=10;
yrook[6]=15;
yrook[7]=15;
yrook[8]=30;

//define bish
xbish[0]=0;
xbish[1]=16;
xbish[2]=13;
xbish[3]=8;
xbish[4]=3;
xbish[5]=0;
ybish[0]=30;
ybish[1]=30;
ybish[2]=10;
ybish[3]=17;
ybish[4]=10;
ybish[5]=30;

//define pawn
 xpawn[0]= 0;
 xpawn[1]= 10;
 xpawn[2]= 7;
 xpawn[3]= 3;
 xpawn[4]= 0;
 ypawn[0]= 30;
 ypawn[1]= 30;
 ypawn[2]= 20;
 ypawn[3]= 20;
 ypawn[4]= 30;
             
//define king
 xking[0]= 0;
 xking[1]= 16;
 xking[2]= 13;
 xking[3]= 10;
 xking[4]= 10;
 xking[5]= 13;
 xking[6]= 13;
 xking[7]= 10;
 xking[8]= 10;
 xking[9]= 6;
 xking[10]= 6;
 xking[11]= 3;
 xking[12]= 3;
 xking[13]= 6;
 xking[14]= 6;
 xking[15]= 3;
 xking[16]= 0;
 yking[0]= 30;
 yking[1]= 30;
 yking[2]= 10;
 yking[3]= 10;
 yking[4]= 8;
 yking[5]= 8;
 yking[6]= 6;
 yking[7]= 6;
 yking[8]= 4;
 yking[9]= 4;
 yking[10]= 6;
 yking[11]= 6;
 yking[12]= 8;
 yking[13]= 8;
 yking[14]= 10;
 yking[15]= 10;
 yking[16]= 30;

//define knight

xknig[0]=0;
xknig[1]=16;
xknig[2]=13;
xknig[3]=18;
xknig[4]=16;
xknig[5]=3;
xknig[6]=0;
yknig[0]=30;
yknig[1]=30;
yknig[2]=18;
yknig[3]=18;
yknig[4]=10;
yknig[5]=10;
yknig[6]=30;

//define queen
 xquee[0]= 0;
 xquee[1]= 16;
 xquee[2]= 13;
 xquee[3]= 3;
 xquee[4]= 0;
 yquee[0]= 30;
 yquee[1]= 30;
 yquee[2]= 10;
 yquee[3]= 10;
 yquee[4]= 30;

   }
//end init applet

// called to draw pawn
   public boolean dPawn(Color pc, int xp, int yp){
          Graphics g=this.getGraphics();
             for(j=0;j<5;j++){
             xset[j]=xpawn[j]+ xp;
             yset[j]=yp+ypawn[j]; 
                     } //end for j 
   if(pc==Color.white){g.setColor(Color.white);} 
          else { g.setColor(Color.black);}             
                g.fillPolygon(xset,yset,pawnsize);
                g.fillArc(xset[3]-1,yset[3]-5,5,5,0,360);
   if(pc==Color.white){g.setColor(Color.black);} 
          else { g.setColor(Color.white);}             
                g.drawArc(xset[3]-1,yset[3]-5,5,5,0,360);
                g.drawPolygon(xset,yset,pawnsize);    
   
   return true;
    }
//called to draw rook
    public boolean dRook(Color pc, int xp, int yp){
          Graphics g=this.getGraphics();
           for(j=0; j<rooksize;j++){
             xset[j]=xrook[j]+xp;
             yset[j]=yp+yrook[j]; 
                 } //end for j
   if(pc==Color.white){g.setColor(Color.white);} 
          else { g.setColor(Color.black);}             
                 g.fillPolygon(xset,yset,rooksize);
   if(pc==Color.white){g.setColor(Color.black);} 
          else { g.setColor(Color.white);}             
                g.drawPolygon(xset,yset,rooksize);
              return true;
     }

//called to draw knight
    public boolean dKnig(Color pc, int xp, int yp){
          Graphics g=this.getGraphics();
           for(j=0; j<knigsize;j++){
             xset[j]=xknig[j]+xp;
             yset[j]=yp+yknig[j]; 
                 } //end for j
   if(pc==Color.white){g.setColor(Color.white);} 
          else { g.setColor(Color.black);}             
                 g.fillPolygon(xset,yset,knigsize);
   if(pc==Color.white){g.setColor(Color.black);} 
          else { g.setColor(Color.white);}             
                g.drawPolygon(xset,yset,knigsize);
              return true;
     }

//called to draw bishop
    public boolean dBish(Color pc, int xp, int yp){
          Graphics g=this.getGraphics();
           for(j=0; j<bishsize;j++){
             xset[j]=xbish[j]+xp;
             yset[j]=yp+ybish[j]; 
                 } //end for j
   if(pc==Color.white){g.setColor(Color.white);} 
          else { g.setColor(Color.black);}             
                 g.fillPolygon(xset,yset,bishsize);
   if(pc==Color.white){g.setColor(Color.black);} 
          else { g.setColor(Color.white);}             
                g.drawPolygon(xset,yset,bishsize);
              return true;
     }

//called to draw king
    public boolean dKing(Color pc, int xp, int yp){
          Graphics g=this.getGraphics();
           for(j=0; j<kingsize;j++){
             xset[j]=xking[j]+xp;
             yset[j]=yp+yking[j]; 
                 } //end for j
   if(pc==Color.white){g.setColor(Color.white);} 
          else { g.setColor(Color.black);}             
                 g.fillPolygon(xset,yset,kingsize);
   if(pc==Color.white){g.setColor(Color.black);} 
          else { g.setColor(Color.white);}             
                g.drawPolygon(xset,yset,kingsize);
              return true;
     }

//called to draw Queen
    public boolean dQuee(Color pc, int xp, int yp){
          Graphics g=this.getGraphics();
           for(j=0; j<queesize;j++){
             xset[j]=xquee[j]+xp;
             yset[j]=yp+yquee[j]; 
                 } //end for j
   if(pc==Color.white){g.setColor(Color.white);} 
          else { g.setColor(Color.black);}             
                g.fillPolygon(xset,yset,queesize);
                g.fillArc(xset[3]+1,yset[3]-7,7,7,0,360);
    if(pc==Color.white){g.setColor(Color.black);} 
          else { g.setColor(Color.white);}             
                g.drawPolygon(xset,yset,queesize);
                g.drawArc(xset[3]+1,yset[3]-7,7,7,0,360);

              return true;
     }

//called to draw board
     public boolean dBoard(){
           Graphics g=this.getGraphics();
               g.setColor(Color.gray);
               g.fillRect(0,0,10*sqsize,10*sqsize);
               for (i=0;i<8;i++){
               for (j=0;j<8;j++){
               if((int)(i+j)/2==(int)(i+j+1)/2){
               g.setColor(Color.white); 
               g.fillRect( sqsize+j*sqsize,sqsize + i*sqsize,sqsize,sqsize);                
                 } // end parity true
              else {
               g.setColor(Color.black); 
               g.fillRect(sqsize+j*sqsize,sqsize + i*sqsize,sqsize,sqsize);                
                 } // else
             } //end j draw board         
             } //end  i draw board

       return true;
     }



//called when mouse click
    public boolean mouseDown(Event e, int x, int y){
        last_x=x; last_y=y;

for(i=0;i<10;i++){
for(j=0;j<10;j++){
 if((1 +40*i < last_x )&(last_x < 40*i + 39)){xclick=i;}
 if((1 +40*j < last_y) & (last_y < 40*j + 39)){yclick=j;}
   } //end for j
   } //end for i

for(i=0;i<16;i++){

if((wset[i].xplace==xclick)&(wset[i].yplace==yclick))
{cobj=i;} // end if wset defines cobj for white

if((bset[i].xplace==xclick)&(bset[i].yplace==yclick))
{cobj=i + 16;} // end if bset

        } //end for i

        return true;
       }


//called when mouse moves
    public boolean mouseDrag(Event e, int x, int y){
          Graphics g=this.getGraphics();
          last_x=x;
          last_y=y;

if(cobj<32){
  if(cobj<8){dPawn(Color.white,last_x,last_y-30);}  // end if cobj <8
  else if(cobj==8){dRook(Color.white,last_x,last_y-30);} // end 8
  else if(cobj==24){dRook(Color.black,last_x,last_y-30);} // end 24
  else if(cobj==9){dKnig(Color.white,last_x,last_y-30);} // end 9
  else if(cobj==25){dKnig(Color.black,last_x,last_y-30);} // end 25
  else if(cobj==10){dBish(Color.white,last_x,last_y-30);} // end 10
  else if(cobj==26){dBish(Color.black,last_x,last_y-30);} // end 26
  else if(cobj==12){dKing(Color.white,last_x,last_y-30);} // end 12
  else if(cobj==28){dKing(Color.black,last_x,last_y-30);} // end 28
  else if(cobj==11){dQuee(Color.white,last_x,last_y-30);}  // end 11
  else if(cobj==27){dQuee(Color.black,last_x,last_y-30);} // end 27
  else if(cobj==13){dBish(Color.white,last_x,last_y-30);}  // end 13
  else if(cobj==29){dBish(Color.black,last_x,last_y-30);} // end 29
  else if(cobj==14){dKnig(Color.white,last_x,last_y-30);}  // end 14
  else if(cobj==30){dKnig(Color.black,last_x,last_y-30);}  // end 30
  else if(cobj==15){dRook(Color.white,last_x,last_y-30);} // end 15
  else if(cobj==31){dRook(Color.black,last_x,last_y-30);}  //end 31
  else if(cobj<24){dPawn(Color.black,last_x,last_y-30);} // end black pawns
   } //end cobj <32

          return true;
      }

//user lets go of mouse
     public boolean mouseUp(Event e, int x, int y){
          // in case of unclick
 
    last_x=x;
     last_y=y;
   if(cobj<16){
    for(i=0;i<10;i++){
    for(j=0;j<10;j++){
     if((1 +40*i < last_x )&(last_x < 40*i + 39)){
            wset[cobj].xplace=i; 
                }  //end if last x
     if((1 +40*j < last_y) & (last_y < 40*j + 39)){
            wset[cobj].yplace=j; 
                }   // end if last y
       } //end j
       } //end i

for(i=0;i<16;i++){
 if(i != cobj){
 if((wset[i].xplace==wset[cobj].xplace)&
               (wset[i].yplace==wset[cobj].yplace)){
               wset[cobj].xplace=xclick;
               wset[cobj].yplace=yclick;
               } // end if white piece already there
               } // i not cobj
 if((bset[i].xplace==wset[cobj].xplace)&
          (bset[i].yplace==wset[cobj].yplace)){ 
     bset[i].xplace=bset[i].xkill;
     bset[i].yplace=bset[i].ykill;  
        } //end if black piece there
    } //end for i
  } //end if cobj<16
 
  else if(cobj<32){
     for(i=0;i<10;i++){
     for(j=0;j<10;j++){
      if((1 +40*i < last_x )&(last_x < 40*i + 39)){
          bset[cobj-16].xplace=i; 
                } // end if last x
      if((1 +40*j < last_y) & (last_y < 40*j + 39)){
           bset[cobj-16].yplace=j; 
                } // end if last y
   } //end j
   } //end i

for(i=0;i<16;i++){
 if(i != cobj -16){
 if((bset[i].xplace==bset[cobj-16].xplace)&
          (bset[i].yplace==bset[cobj-16].yplace)){
    bset[cobj-16].xplace=xclick;
    bset[cobj-16].yplace=yclick;
               } // end if piece there bset
               } //  end i not cobj -16
 if((wset[i].xplace==bset[cobj-16].xplace)&
         (wset[i].yplace==    bset[cobj-16].yplace)){ 
    wset[i].xplace=wset[i].xkill;
    wset[i].yplace=wset[i].ykill; 
        } //end  if wset
    } //end for i
  } //end if cobj<32

cobj=35;
 dBoard();      

         for(i=0;i<8;i++){
  dPawn(Color.white,16+wset[i].xplace*40,wset[i].yplace*40);
  dPawn(Color.black,16+bset[i].xplace*40,bset[i].yplace*40);
               } //end i draw pawns

 dRook(Color.white,10+wset[8].xplace*40,wset[8].yplace*40);
 dRook(Color.black,10+bset[8].xplace*40,bset[8].yplace*40);
 dKnig(Color.white,10+wset[9].xplace*40,wset[9].yplace*40);
 dKnig(Color.black,10+bset[9].xplace*40,bset[9].yplace*40);
 dBish(Color.white,10+wset[10].xplace*40,wset[10].yplace*40);
 dBish(Color.black,10+bset[10].xplace*40,bset[10].yplace*40);
 dKing(Color.white,10+wset[12].xplace*40,wset[12].yplace*40);
 dKing(Color.black,10+bset[12].xplace*40,bset[12].yplace*40);
 dQuee(Color.white,10+wset[11].xplace*40,wset[11].yplace*40);
 dQuee(Color.black,10+bset[11].xplace*40,bset[11].yplace*40);
 dBish(Color.white,10+wset[13].xplace*40,wset[13].yplace*40);
 dBish(Color.black,10+bset[13].xplace*40,bset[13].yplace*40);
 dKnig(Color.white,10+wset[14].xplace*40,wset[14].yplace*40);
 dKnig(Color.black,10+bset[14].xplace*40,bset[14].yplace*40);
 dRook(Color.white,10+wset[15].xplace*40,wset[15].yplace*40);
 dRook(Color.black,10+bset[15].xplace*40,bset[15].yplace*40);

               return true;
              }

//user clicks button 
     public boolean action(Event event, Object arg){
          // in case of click
          if(event.target==new_button){
    for(i=0;i<8;i++){
         wset[i].xplace=i+1;
         bset[i].xplace=i+1;
         wset[i].yplace=7;
         bset[i].yplace=2;
         wset[i].rank=1;
         bset[i].rank=1;  
           } //defines pawns in set


   for(i=8;i<16;i++){
         wset[i].xplace=i-7;
         wset[i].yplace=8;
         bset[i].xplace=i-7;
         bset[i].yplace=1;
      if((i==8)|(i==15)){wset[i].rank=4; bset[i].rank=4;}
      if((i==9)|(i==14)){wset[i].rank=2; bset[i].rank=2;}
      if((i==10)|(i==13)){wset[i].rank=3; bset[i].rank=3;}
      if(i==11){wset[i].rank=6; bset[i].rank=6;}
      if(i==12){wset[i].rank=5; bset[i].rank=5;}
            } // defines back row

               Graphics g=this.getGraphics();
               Rectangle r=this.bounds();
               g.setColor(this.getBackground());
               g.fillRect(r.x,r.y,r.width,r.height);

            } // end new button

          if(event.target==draw_button){
                Graphics g=this.getGraphics();
               Rectangle r=this.bounds();
               g.setColor(this.getBackground());
               g.fillRect(r.x,r.y,r.width,r.height);
          
                dBoard();
 

for(i=0;i<8;i++){
  dPawn(Color.white,16+wset[i].xplace*40,wset[i].yplace*40);
  dPawn(Color.black,16+bset[i].xplace*40,bset[i].yplace*40);
               } //end i draw pawns

 dRook(Color.white,10+wset[8].xplace*40,wset[8].yplace*40);
 dRook(Color.black,10+bset[8].xplace*40,bset[8].yplace*40);
 dKnig(Color.white,10+wset[9].xplace*40,wset[9].yplace*40);
 dKnig(Color.black,10+bset[9].xplace*40,bset[9].yplace*40);
 dBish(Color.white,10+wset[10].xplace*40,wset[10].yplace*40);
 dBish(Color.black,10+bset[10].xplace*40,bset[10].yplace*40);
 dKing(Color.white,10+wset[12].xplace*40,wset[12].yplace*40);
 dKing(Color.black,10+bset[12].xplace*40,bset[12].yplace*40);
 dQuee(Color.white,10+wset[11].xplace*40,wset[11].yplace*40);
 dQuee(Color.black,10+bset[11].xplace*40,bset[11].yplace*40);
 dBish(Color.white,10+wset[13].xplace*40,wset[13].yplace*40);
 dBish(Color.black,10+bset[13].xplace*40,bset[13].yplace*40);
 dKnig(Color.white,10+wset[14].xplace*40,wset[14].yplace*40);
 dKnig(Color.black,10+bset[14].xplace*40,bset[14].yplace*40);
 dRook(Color.white,10+wset[15].xplace*40,wset[15].yplace*40);
 dRook(Color.black,10+bset[15].xplace*40,bset[15].yplace*40);


               return true;
              } // draw button
      

           // Otherwise let the superclass handle it.
           else return super.action(event, arg);
     }
}

