© Corel Final works

This is the last part of out chess programming course. Here we make some small works that we haven't done until now. We will implement the pawn promotion and the castling. We also define a new board representation for the graphic.

We start with the new board representation for the graphics. So graphical errors won't happen when the board is repainted while the AI is calculating. First we define the array:

//The board for the graphics
int [] graphboard = new int [120];

Now we have to ensure that the board is initialized. So we change the method newgame ():

for (int i=0; i < 120; i++) {
   board [i] = org [i];
   graphboard [i] = org [i];
}

All access to the board for the painting of the pieces (in the methods paintField (), mousePressed () and mouseDragged ()) have to be changed:

//paint piece
try {
   g.drawImage (pieces [graphboard [index] % 100 - 10], x * 40, y * 40, 40, 40, parent);
} catch (ArrayIndexOutOfBoundsException e) {}

Of course we also have to execute the moves, that are executed on the normal board, on the graph board. So we change the method execute ().

graphboard [start] = board [start];
graphboard [end] = board [end];

Next we will implement the pawn promotion. The pawn always promote into a queen.We have to change the methods execute () and simulize (). Because the new code is similar in both methods, we print it only once:

//pawn promotion?
if ((board [end] % 10 == 1) && ((end < 29) || (end > 90)))
   board [end] += 4;;

The changes for the castling are more difficult. The method genmove () must regocnize if the castling is valid, and the method execute () has to be able to move the rook automaticaly because only the king move is given. We first change execute ():

//castling ?
if (board [end] % 10 == 6) {
   if ( end == start + 2) {
      //small castling
      board [start + 1] = board [start + 3] % 100;
      board [start + 3] = 0;

      graphboard [start + 1] = board [start + 1];
      graphboard [start + 3] = 0;

      paintField (start + 3);
      paintField (start + 1);
   }
   if (end == start - 2) {
      //big castling
      board [start - 1] = board [start - 4] % 100;
      board [start - 4] = 0;

      graphboard [start - 1] = board [start - 1];
      graphboard [start - 4] = 0;

      paintField (start - 4);
      paintField (start - 1);
   }
}

In genmove (), we check if the castling is valid. We first check if the king is in check. Then the king will be put one field on the right and we check again if the king is in check. Then the king is put back, the rook will be moved and the move simulized:

if ((board [i] / 100 == 1) && (! ischeck ())) {
   if (((board [i+1] == 0) && (board [i+2] == 0)) && (board [i+3] / 100 == 1)) {
      //small castling
      board [i+1] = board [i] % 100;
      board [i] = 0;

      if (! ischeck ()) {
         //king back
         board [i] = board [i+1];

         //move rook
         board [i + 1] = board [i + 3] % 100;
         board [i + 3] = 0;

         simulize (i, i+2);

         //undo
         board [i + 3] = board [i + 1] + 100;
         board [i+1] = board [i];
      }

      //undo
      board [i] = board [i + 1] + 100;
      board [i + 1] = 0;
   }

   if (((board [i-1] == 0) && (board [i-2] == 0)) && ((board [i-3] == 0) && (board [i-4] / 100 == 1))) {
      //big castling
      board [i-1] = board [i] % 100;
      board [i] = 0;

      if (! ischeck ()) {
         //king back
         board [i] = board [i-1];

         //move rook
         board [i - 1] = board [i - 4] % 100;
         board [i - 4] = 0;

         simulize (i, i-2);

         //undo
         board [i - 4] = board [i - 1] + 100;
         board [i - 1] = board [i];
      }

      //undo
      board [i] = board [i - 1] + 100;
      board [i - 1] = 0;
   }
}

Now we're at the end of our programming course. We hope you will have fun while you develop your programm further.

show Applet step 10
source code of step 10


to the overview | back