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