示例#1
0
文件: attack.cpp 项目: adua/chess
bool is_in_check(const board_t * board, int colour) {

    ASSERT(board_is_ok(board));
    ASSERT(colour_is_ok(colour));

    return is_attacked(board,king_pos(board,colour),colour_opp(colour));
}
示例#2
0
文件: attack.cpp 项目: adua/chess
bool is_pinned(const board_t * board, int from, int to, int colour) {

    int king;
    int inc;
    int sq, piece;

    ASSERT(board!=NULL);
    ASSERT(square_is_ok(from));
    ASSERT(square_is_ok(to));
    ASSERT(colour_is_ok(colour));

    king = king_pos(board,colour);

    inc = DELTA_INC(king-from);
    if (inc == IncNone) return false; // not a line

    sq = from;
    do sq += inc;
    while (board->square[sq] == Empty);

    if (sq != king) return false; // blocker

    sq = from;
    do sq -= inc;
    while ((piece=board->square[sq]) == Empty);

    return square_is_ok(sq)
           && (piece & DELTA_MASK(king-sq)) != 0
           && piece_colour(piece) == colour_opp(colour)
           && DELTA_INC(king-to) != inc;
}
示例#3
0
文件: san.cpp 项目: ageneau/scid
static bool san_to_lan(const char san[], const board_t * board, char string[], int size) {

   int len;
   int left, right;
   int c;
   int king, rook;
   char king_string[3], rook_string[3];

   ASSERT(san!=NULL);
   ASSERT(board_is_ok(board));
   ASSERT(string!=NULL);
   ASSERT(size>=8);

   // init

   if (size < 8) return false;
   strcpy(string,"???????");

   len = strlen(san);

   left = 0;
   right = len;

   // skip trailing '+' or '#'

   if (left < right) {
      c = san[right-1];
      if (c == '+' || c == '#') right--;
   }

   // castling

   ASSERT(left==0);

   if (false) {

   } else if (right == 3 && strncmp(san,"O-O",3) == 0) {

      if (board->castle[board->turn][SideH] == SquareNone) return false;

      king = king_pos(board,board->turn);
      rook = board->castle[board->turn][SideH];

      square_to_string(king,king_string,3);
      square_to_string(rook,rook_string,3);

      sprintf(string,"K%s?%s?",king_string,rook_string);

   } else if (right == 5 && strncmp(san,"O-O-O",5) == 0) {

      if (board->castle[board->turn][SideA] == SquareNone) return false;

      king = king_pos(board,board->turn);
      rook = board->castle[board->turn][SideA];

      square_to_string(king,king_string,3);
      square_to_string(rook,rook_string,3);

      sprintf(string,"K%s?%s?",king_string,rook_string);

   } else {

      // moved piece

      if (left < right) {

         c = san[left];

         if (char_is_piece(c)) {
            string[0] = c;
            left++;
         }
      }

      // promotion

      if (left < right) {

         c = toupper(san[right-1]);

         if (char_is_piece(c)) {

            string[6] = c;
            right--;

            // skip '='

            if (left < right && san[right-1] == '=') right--;
         }
      }

      // to-square rank

      if (left < right) {

         c = san[right-1];

         if (char_is_rank(c)) {
            string[5] = c;
            right--;
         }
      }

      // to-square file

      if (left < right) {

         c = san[right-1];

         if (char_is_file(c)) {
            string[4] = c;
            right--;
         }
      }

      // captured piece

      if (left < right) {

         c = san[right-1];

         if (char_is_piece(c)) {
            string[3] = c;
            right--;
         }
      }

      // skip middle '-' or 'x'

      if (left < right) {
         c = san[right-1];
         if (c == '-' || c == 'x') right--;
      }

      // from-square file

      if (left < right) {

         c = san[left];

         if (char_is_file(c)) {
            string[1] = c;
            left++;
         }
      }

      // from-square rank

      if (left < right) {

         c = san[left];

         if (char_is_rank(c)) {
            string[2] = c;
            left++;
         }
      }

      if (left != right) return false;
   }

   // end

   return true;
}