コード例 #1
0
ファイル: move_evasion.cpp プロジェクト: Distrotech/gnuchess
static bool add_piece_moves(list_t * list, const board_t * board, int to, bool legal, bool stop) {

   int me;
   const sq_t * ptr;
   int from, piece;

   ASSERT(list!=NULL);
   ASSERT(board!=NULL);
   ASSERT(SQUARE_IS_OK(to));
   ASSERT(legal==true||legal==false);
   ASSERT(stop==true||stop==false);

   me = board->turn;

   for (ptr = &board->piece[me][1]; (from=*ptr) != SquareNone; ptr++) { // HACK: no king

      piece = board->square[from];

      if (PIECE_ATTACK(board,piece,from,to)) {
         if (!legal || !is_pinned(board,from,me)) {
            if (stop) return true;
            LIST_ADD(list,MOVE_MAKE(from,to));
         }
      }
   }

   return false;
}
コード例 #2
0
bool move_is_check(int move, board_t * board) {

    undo_t undo[1];
    bool check;
    int me, opp, king;
    int from, to, piece;

    ASSERT(move_is_ok(move));
    ASSERT(board!=NULL);

    // slow test for complex moves

    if (MOVE_IS_SPECIAL(move)) {

        move_do(board,move,undo);
        check = IS_IN_CHECK(board,board->turn);
        move_undo(board,move,undo);

        return check;
    }

    // init

    me = board->turn;
    opp = COLOUR_OPP(me);
    king = KING_POS(board,opp);

    from = MOVE_FROM(move);
    to = MOVE_TO(move);
    piece = board->square[from];
    ASSERT(COLOUR_IS(piece,me));

    // direct check

    if (PIECE_ATTACK(board,piece,to,king)) return true;

    // indirect check

    if (is_pinned(board,from,opp)
            && DELTA_INC_LINE(king-to) != DELTA_INC_LINE(king-from)) {
        return true;
    }

    return false;
}
コード例 #3
0
static void add_quiet_checks(list_t * list, const board_t * board) {

    int me, opp;
    int king;
    const sq_t * ptr, * ptr_2;
    int from, to, sq;
    int piece;
    const inc_t * inc_ptr;
    int inc;
    int pawn;
    int rank;
    int pin[8+1];

    ASSERT(list!=NULL);
    ASSERT(board!=NULL);

    // init

    me = board->turn;
    opp = COLOUR_OPP(me);

    king = KING_POS(board,opp);

    find_pins(pin,board);

    // indirect checks

    for (ptr = pin; (from=*ptr) != SquareNone; ptr++) {

        piece = board->square[from];

        ASSERT(is_pinned(board,from,opp));

        if (PIECE_IS_PAWN(piece)) {

            inc = PAWN_MOVE_INC(me);
            rank = PAWN_RANK(from,me);

            if (rank != Rank7) { // promotes are generated with captures
                to = from + inc;
                if (board->square[to] == Empty) {
                    if (DELTA_INC_LINE(to-king) != DELTA_INC_LINE(from-king)) {
                        ASSERT(!SQUARE_IS_PROMOTE(to));
                        LIST_ADD(list,MOVE_MAKE(from,to));
                        if (rank == Rank2) {
                            to = from + (2*inc);
                            if (board->square[to] == Empty) {
                                ASSERT(DELTA_INC_LINE(to-king)!=DELTA_INC_LINE(from-king));
                                ASSERT(!SQUARE_IS_PROMOTE(to));
                                LIST_ADD(list,MOVE_MAKE(from,to));
                            }
                        }
                    }
                }
            }

        } else if (PIECE_IS_SLIDER(piece)) {

            for (inc_ptr = PIECE_INC(piece); (inc=*inc_ptr) != IncNone; inc_ptr++) {
                for (to = from+inc; board->square[to] == Empty; to += inc) {
                    ASSERT(DELTA_INC_LINE(to-king)!=DELTA_INC_LINE(from-king));
                    LIST_ADD(list,MOVE_MAKE(from,to));
                }
            }

        } else {

            for (inc_ptr = PIECE_INC(piece); (inc=*inc_ptr) != IncNone; inc_ptr++) {
                to = from + inc;
                if (board->square[to] == Empty) {
                    if (DELTA_INC_LINE(to-king) != DELTA_INC_LINE(from-king)) {
                        LIST_ADD(list,MOVE_MAKE(from,to));
                    }
                }
            }
        }
    }

    // piece direct checks

    for (ptr = &board->piece[me][1]; (from=*ptr) != SquareNone; ptr++) { // HACK: no king

        for (ptr_2 = pin; (sq=*ptr_2) != SquareNone; ptr_2++) {
            if (sq == from) goto next_piece;
        }

        ASSERT(!is_pinned(board,from,opp));

        piece = board->square[from];
        inc_ptr = PIECE_INC(piece);

        if (PIECE_IS_SLIDER(piece)) {

            for (; (inc=*inc_ptr) != IncNone; inc_ptr++) {
                for (to = from+inc; board->square[to] == Empty; to += inc) {
                    if (PIECE_ATTACK(board,piece,to,king)) {
                        LIST_ADD(list,MOVE_MAKE(from,to));
                    }
                }
            }

        } else {

            for (; (inc=*inc_ptr) != IncNone; inc_ptr++) {
                to = from + inc;
                if (board->square[to] == Empty) {
                    if (PSEUDO_ATTACK(piece,king-to)) {
                        LIST_ADD(list,MOVE_MAKE(from,to));
                    }
                }
            }
        }

next_piece:
        ;
    }

    // pawn direct checks

    inc = PAWN_MOVE_INC(me);
    pawn = PAWN_MAKE(me);

    to = king - (inc-1);
    ASSERT(PSEUDO_ATTACK(pawn,king-to));

    from = to - inc;
    if (board->square[from] == pawn) {
        if (board->square[to] == Empty) {
            ASSERT(!SQUARE_IS_PROMOTE(to));
            LIST_ADD(list,MOVE_MAKE(from,to));
        }
    } else {
        from = to - (2*inc);
        if (board->square[from] == pawn) {
            if (PAWN_RANK(from,me) == Rank2
                    && board->square[to] == Empty
                    && board->square[from+inc] == Empty) {
                ASSERT(!SQUARE_IS_PROMOTE(to));
                LIST_ADD(list,MOVE_MAKE(from,to));
            }
        }
    }

    to = king - (inc+1);
    ASSERT(PSEUDO_ATTACK(pawn,king-to));

    from = to - inc;
    if (board->square[from] == pawn) {
        if (board->square[to] == Empty) {
            ASSERT(!SQUARE_IS_PROMOTE(to));
            LIST_ADD(list,MOVE_MAKE(from,to));
        }
    } else {
        from = to - (2*inc);
        if (board->square[from] == pawn) {
            if (PAWN_RANK(from,me) == Rank2
                    && board->square[to] == Empty
                    && board->square[from+inc] == Empty) {
                ASSERT(!SQUARE_IS_PROMOTE(to));
                LIST_ADD(list,MOVE_MAKE(from,to));
            }
        }
    }
}
コード例 #4
0
bool move_is_pseudo(int move, board_t * board) {

    int me, opp;
    int from, to;
    int piece, capture;
    int inc, delta;

    ASSERT(move_is_ok(move));
    ASSERT(board!=NULL);

    ASSERT(!board_is_check(board));

    // special cases

    if (MOVE_IS_SPECIAL(move)) {
        return move_is_pseudo_debug(move,board);
    }

    ASSERT((move&~07777)==0);

    // init

    me = board->turn;
    opp = COLOUR_OPP(board->turn);

    // from

    from = MOVE_FROM(move);
    ASSERT(SQUARE_IS_OK(from));

    piece = board->square[from];
    if (!COLOUR_IS(piece,me)) return false;

    ASSERT(piece_is_ok(piece));

    // to

    to = MOVE_TO(move);
    ASSERT(SQUARE_IS_OK(to));

    capture = board->square[to];
    if (COLOUR_IS(capture,me)) return false;

    // move

    if (PIECE_IS_PAWN(piece)) {

        if (SQUARE_IS_PROMOTE(to)) return false;

        inc = PAWN_MOVE_INC(me);
        delta = to - from;
        ASSERT(delta_is_ok(delta));

        if (capture == Empty) {

            // pawn push

            if (delta == inc) return true;

            if (delta == (2*inc)
                    && PAWN_RANK(from,me) == Rank2
                    && board->square[from+inc] == Empty) {
                return true;
            }

        } else {

            // pawn capture

            if (delta == (inc-1) || delta == (inc+1)) return true;
        }

    } else {

        if (PIECE_ATTACK(board,piece,from,to)) return true;
    }

    return false;
}