CHAPTER 6
Using Parameters in Applets TOC

The Chess Engine ExampleTOC


The code for the applets from this section will be found in the

  • jvexpls/params

folder.

Suppose that we wish to build a chess engine which can be passed certain parameters or data from an HTML file to play (or run) a given game of chess. Of course we can think of many other possible problems of this type. We have an applet that needs a set of datas to start. Another example would be an applet that could show the constellations in the sky. Such an applet would need to know the position of the observer and the time of the observation to faithfully give a rendition of the night time sky.

Here we will consider the problem of building a chess engine. Then we will contruct an HTML file which will pass a set of moves from a game onto the chess engine which it will then be able to play. We begin by outlining some object that we will need we begin with the properties of of chess pieces:


  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

We will define a rook (and other pieces in a similar way) by a set of points that can be used to draw it.

// 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;

Next we show how to draw the rook

//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;
}

We show how to draw the board:

//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;
}

On a mouseDown event we want to see if the operator has chosen a piece:

//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;
  }

On a mouseDrag we want to move the piece:

//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;
}

The code above allows us to create a chess board. We may move pieces on the chess board from one position to another. It does not play chess games.

We turn our attention to another problem. Namely, writing out a chess game and having the applet play the game. Of course one could write a program to play a specific game, but the more general solution is to write an engine that will read a file and play the game associated to the file. Such an engine would be like the program above in most respects. However, we will not need to rewrite mouseDown or mouseDrag. The following long sequence appears in the engine.java file which reads the parameter from the gotw.htm file and plays each move when the forward button is pressed.


if(event.target==frw_button){

if(moves < (int)(game1.length()/4))
// game defines current object
if(game1.charAt(4*moves+0)=='a'){xold=1;}
if(game1.charAt(4*moves+0)=='b'){xold=2;}
if(game1.charAt(4*moves+0)=='c'){xold=3;}
if(game1.charAt(4*moves+0)=='d'){xold=4;}
if(game1.charAt(4*moves+0)=='e'){xold=5;}
if(game1.charAt(4*moves+0)=='f'){xold=6;}
if(game1.charAt(4*moves+0)=='g'){xold=7;}
if(game1.charAt(4*moves+0)=='h'){xold=8;}
if(game1.charAt(4*moves+0)=='o'){castle=true;}
if(game1.charAt(4*moves+1)=='8'){yold=1;}
if(game1.charAt(4*moves+1)=='7'){yold=2;}
if(game1.charAt(4*moves+1)=='6'){yold=3;}
if(game1.charAt(4*moves+1)=='5'){yold=4;}
if(game1.charAt(4*moves+1)=='4'){yold=5;}
if(game1.charAt(4*moves+1)=='3'){yold=6;}
if(game1.charAt(4*moves+1)=='2'){yold=7;}
if(game1.charAt(4*moves+1)=='1'){yold=8;}

if(game1.charAt(4*moves+2)=='a'){xnew=1;}
if(game1.charAt(4*moves+2)=='b'){xnew=2;}
if(game1.charAt(4*moves+2)=='c'){xnew=3;}
if(game1.charAt(4*moves+2)=='d'){xnew=4;}
if(game1.charAt(4*moves+2)=='e'){xnew=5;}
if(game1.charAt(4*moves+2)=='f'){xnew=6;}
if(game1.charAt(4*moves+2)=='g'){xnew=7;}
if(game1.charAt(4*moves+2)=='h'){xnew=8;}
if(game1.charAt(4*moves+3)=='x'){kside=true;}
if(game1.charAt(4*moves+3)=='o'){kside=false;}
if(game1.charAt(4*moves+3)=='8'){ynew=1;}
if(game1.charAt(4*moves+3)=='7'){ynew=2;}
if(game1.charAt(4*moves+3)=='6'){ynew=3;}
if(game1.charAt(4*moves+3)=='5'){ynew=4;}
if(game1.charAt(4*moves+3)=='4'){ynew=5;}
if(game1.charAt(4*moves+3)=='3'){ynew=6;}
if(game1.charAt(4*moves+3)=='2'){ynew=7;}
if(game1.charAt(4*moves+3)=='1'){ynew=8;}
// game1 defines next white move


if((int)(moves/2)==(int)(moves+1)/2){
if(castle){
if(kside){
wset[12].xplace=7;
wset[15].xplace=6;
} //end kside
else{
wset[12].xplace=3;
wset[8].xplace=4;
} // end else qside
castle=false; } //end if castle
else{for(i=0;i<16;i++){

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

wset[cobj].xplace=xnew;
wset[cobj].yplace=ynew;

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

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;

kills[moves]=i+16;

} //end if black piece there
} // end for i
} // end if not castle
} // end if even

else{
if(castle){
if(kside){
bset[12].xplace=7;
bset[15].xplace=6;
} //end kside
else{
bset[12].xplace=3;
bset[8].xplace=4;
} // end else qside
castle=false; } //end if castle
else{for(i=0;i<16;i++){

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

bset[cobj-16].xplace=xnew;
bset[cobj-16].yplace=ynew;

for(i=0;i<16;i++){
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;

kills[moves]=i;
} //end if white piece there
} // end for i
} // end if not castle
} // else odd

moves=moves+1;

dBoard();

} // end if moves < length/4

} // end frw button

Finally we turn to the question of reading data into an applet from an HTML file. We place thefollowing parameter tag in our HTML applet file:

< APPLET CODE="engine.class" NAME="Chess" WIDTH=1000 HEIGHT=400 >
< PARAM name="game" value="e2e4e7e5 ... h6g6 >
< /APPLET>

See the file gotw.htm in the directory jvexpls/params for a complete example of the code. The parameters from this applet statement are read into the applet program by the code:


// Read the specified parameter
  protected String getGameParameter(String name){
    String value=this.getParameter(name);

    return new String(value);
  } // end get game string

Once the string given by "value" has been read into the applet program we may parse the string to determine the moves of the game. This is done in the portion of the program related to the forward button as seen above.

References TOC