© Corel Check for checks

Our programm knows already the way of moving of the different pieces. But there is missing something: We can go with our king in a check! To avoid that, we need a new method called ischeck ():

//We check if the king of the side,
//that can move, in check is

public boolean ischeck () {
   int king = 0;
   //look for king
   for ( int i = 21; i < 99; i++) {
      if ((board [i] % 100 / 10 == color) &&
            (board [i] % 10 == 6)) {
         king = i;
         break;
      }

      if ( i % 10 == 8)
         i += 2;
   }

   //knight
   if ((board [king-21] % 10 == 2) &&
         (board [king-21] % 100 / 10 != color))
      return true;
   if ((board [king+21] % 10 == 2) &&
         (board [king+21] % 100 / 10 != color))
      return true;
   if ((board [king-19] % 10 == 2) &&
         (board [king-19] % 100 / 10 != color))
      return true;
   if ((board [king+19] % 10 == 2) &&
         (board [king+19] % 100 / 10 != color))
      return true;
   if ((board [king- 8] % 10 == 2) &&
         (board [king- 8] % 100 / 10 != color))
      return true;
   if ((board [king+ 8] % 10 == 2) &&
         (board [king+ 8] % 100 / 10 != color))
      return true;
   if ((board [king-12] % 10 == 2) &&
         (board [king-12] % 100 / 10 != color))
      return true;
   if ((board [king+12] % 10 == 2) &&
         (board [king+12] % 100 / 10 != color))
      return true;

    //bishop
   int j = king;
   while (board [j - 9] != 99) {
      j -= 9;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ((board [j] % 10 == 3) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   j = king;
   while (board [j+9] != 99) {
      j += 9;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ((board [j] % 10 == 3) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   j = king;
   while (board [j-11] != 99) {
      j -= 11;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ( (board [j] % 10 == 3) || (board [j] % 10 == 5))
         return true;
      else
         break;
    }

   j = king;
   while (board [j+11] != 99) {
      j +=11;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ( (board [j] % 10 == 3) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   //rook
   
j = king;
   while (board [j-10] != 99) {
      j -= 10;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   j = king;
   while (board [j+10] != 99) {
      j += 10;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   j = king;
   while (board [j-1] != 99) {
      j -=1;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   j = king;
   while (board [j+1] != 99) {
      j +=1;
      if (board [j] % 100 / 10 == color)
         break;
      if (board [j] == 0)
         continue;
      if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
         return true;
      else
         break;
   }

   //pawn
   if (color == 1) {
      if ((board [king-11] % 10 == 1) &&
            (board [king-11] % 100 / 10 == 2))
         return true;
      if ((board [king- 9] % 10 == 1) &&
            (board [king- 9] % 100 / 10 == 2))
         return true;
   } else {
      if ((board [king+11] % 10 == 1) &&
            (board [king+11] % 100 / 10 == 1))
         return true;
      if ((board [king+ 9] % 10 == 1) &&
            (board [king+ 9] % 100 / 10 == 1))
         return true;
   }

   //king
   if ( board [king+ 1] % 10 == 6 )
      return true;
   if ( board [king- 1] % 10 == 6 )
      return true;
   if ( board [king+10] % 10 == 6 )
      return true;
   if ( board [king-10] % 10 == 6 )
      return true;
   if ( board [king+11] % 10 == 6 )
      return true;
   if ( board [king-11] % 10 == 6 )
      return true;
   if ( board [king+ 9] % 10 == 6 )
      return true;
   if ( board [king- 9] % 10 == 6 )
      return true;

   return false;
}

The method is really simple: We look for the king who's side can move. Then we go out of this field and look if any enemy piece can attak this field. If yes, the method returns true, if not it returns false.

We have to change the method simulize () in such a way that it executes the move, checks for check and then undo the move:

//simulize move
int orgstart = board [start];
int orgend = board [end];

board [end] = board [start];
board [start] = 0;

if (! ischeck ()) {
   movelist [movecounter] = start * 100 + end;
   movecounter++;
}

//undo move
board [start] = orgstart;
board [end] = orgend;

Now only moves that do not lead to a king in check are on the movelist.

show applet step 6
source code of step 6


to the overview | back | continue