ChessBoolean chess_generate_is_square_attacked(const ChessPosition* position, ChessSquare sq, ChessColor color) { ChessSquare from; ChessPiece piece; int dirs, dir, d, dist; /* Check for knight attacks */ dirs = jump_dirs[sq]; for (d = 0; d < 8; d++) { dir = dirs_array[d]; if ((dir & dirs) == 0) continue; from = sq + jumps_array[d]; piece = chess_position_piece(position, from); if (chess_position_piece(position, from) == chess_piece_of_color(CHESS_PIECE_WHITE_KNIGHT, color)) return CHESS_TRUE; } for (d = 0; d < 8; d++) { dir = dirs_array[d]; from = sq; dist = 1; do { dirs = slide_dirs[from]; if ((dir & dirs) == 0) break; from += slides_array[d]; piece = chess_position_piece(position, from); if (piece != CHESS_PIECE_NONE && chess_piece_color(piece) == color) { switch (piece) { case CHESS_PIECE_WHITE_QUEEN: case CHESS_PIECE_BLACK_QUEEN: return CHESS_TRUE; case CHESS_PIECE_WHITE_BISHOP: case CHESS_PIECE_BLACK_BISHOP: if (dir & bishop_dirs) return CHESS_TRUE; break; case CHESS_PIECE_WHITE_ROOK: case CHESS_PIECE_BLACK_ROOK: if (dir & rook_dirs) return CHESS_TRUE; break; case CHESS_PIECE_WHITE_KING: case CHESS_PIECE_BLACK_KING: if (dist == 1) return CHESS_TRUE; break; case CHESS_PIECE_WHITE_PAWN: if (dist == 1 && dir & (DIR_SE | DIR_SW)) return CHESS_TRUE; break; case CHESS_PIECE_BLACK_PAWN: if (dist == 1 && dir & (DIR_NE | DIR_NW)) return CHESS_TRUE; break; case CHESS_PIECE_WHITE_KNIGHT: case CHESS_PIECE_BLACK_KNIGHT: break; default: assert(CHESS_FALSE); break; } } dist++; } while (piece == CHESS_PIECE_NONE); } return CHESS_FALSE; }
void chess_position_undo_move(ChessPosition* position, ChessUnmove unmove) { ChessSquare from = chess_unmove_from(unmove); ChessSquare to = chess_unmove_to(unmove); ChessUnmoveCaptured captured = chess_unmove_captured(unmove); ChessUnmoveEp ep = chess_unmove_ep(unmove); ChessPiece piece; ChessColor other = position->to_move; ChessColor color = chess_color_other(other); ChessFile file; if (from == 0 && to == 0) { /* Null move */ piece = CHESS_PIECE_NONE; } else { if (chess_unmove_promotion(unmove)) piece = chess_piece_of_color(CHESS_PIECE_WHITE_PAWN, color); else piece = position->piece[to]; assert(color == chess_piece_color(piece)); /* Unmove the piece */ position->piece[from] = piece; position->piece[to] = captured_piece(captured, other); /* Handle castling */ if (piece == CHESS_PIECE_WHITE_KING && from == CHESS_SQUARE_E1) { if (to == CHESS_SQUARE_G1) { position->piece[CHESS_SQUARE_F1] = CHESS_PIECE_NONE; position->piece[CHESS_SQUARE_H1] = CHESS_PIECE_WHITE_ROOK; } else if (to == CHESS_SQUARE_C1) { position->piece[CHESS_SQUARE_D1] = CHESS_PIECE_NONE; position->piece[CHESS_SQUARE_A1] = CHESS_PIECE_WHITE_ROOK; } } else if (piece == CHESS_PIECE_BLACK_KING && from == CHESS_SQUARE_E8) { if (to == CHESS_SQUARE_G8) { position->piece[CHESS_SQUARE_F8] = CHESS_PIECE_NONE; position->piece[CHESS_SQUARE_H8] = CHESS_PIECE_BLACK_ROOK; } else if (to == CHESS_SQUARE_C8) { position->piece[CHESS_SQUARE_D8] = CHESS_PIECE_NONE; position->piece[CHESS_SQUARE_A8] = CHESS_PIECE_BLACK_ROOK; } } position->castle = chess_unmove_castle(unmove); } /* Handle ep */ if (ep == CHESS_UNMOVE_EP_NONE) { position->ep = CHESS_FILE_INVALID; } else if (ep == CHESS_UNMOVE_EP_CAPTURE) { assert(piece == CHESS_PIECE_WHITE_PAWN || piece == CHESS_PIECE_BLACK_PAWN); /* Restore the captured pawn */ file = chess_square_file(to); if (color == CHESS_COLOR_WHITE) position->piece[chess_square_from_fr(file, CHESS_RANK_5)] = CHESS_PIECE_BLACK_PAWN; else position->piece[chess_square_from_fr(file, CHESS_RANK_4)] = CHESS_PIECE_WHITE_PAWN; position->ep = file; } else { position->ep = ep - CHESS_UNMOVE_EP_AVAILABLE; } /* Update king positions */ if (piece == CHESS_PIECE_WHITE_KING) position->wking = from; else if (piece == CHESS_PIECE_BLACK_KING) position->bking = from; /* Update move counters */ position->fifty = chess_unmove_fifty(unmove); position->to_move = color; if (position->to_move == CHESS_COLOR_BLACK) position->move_num--; }
static ChessMove gen_next(ChessMoveGenerator* gen) { const ChessPosition* position = gen->position; ChessPiece piece; ChessColor color = chess_position_to_move(position); ChessPiece target; int dirs, dir; int piece_dirs; ChessRank start_rank, end_rank; int slide; int capture_dirs; ChessFile ep_file; ChessSquare ep; ChessMove move; if (gen->promote != CHESS_MOVE_PROMOTE_NONE) { gen_pawn_promotes: if (++gen->promote <= CHESS_MOVE_PROMOTE_QUEEN) return chess_move_make_promote(gen->sq, gen->to, gen->promote); gen->promote = CHESS_MOVE_PROMOTE_NONE; } for (; gen->sq <= CHESS_SQUARE_H8; gen->sq++) { piece = chess_position_piece(position, gen->sq); if (piece == CHESS_PIECE_NONE) continue; if (color != chess_piece_color(piece)) continue; switch (piece) { case CHESS_PIECE_WHITE_PAWN: case CHESS_PIECE_BLACK_PAWN: start_rank = (color == CHESS_COLOR_WHITE) ? CHESS_RANK_2 : CHESS_RANK_7; end_rank = (color == CHESS_COLOR_WHITE) ? CHESS_RANK_8 : CHESS_RANK_1; slide = (color == CHESS_COLOR_WHITE) ? SLIDE_N : SLIDE_S; capture_dirs = (color == CHESS_COLOR_WHITE) ? DIR_NE | DIR_NW : DIR_SE | DIR_SW; dirs = capture_dirs & slide_dirs[gen->sq]; ep_file = chess_position_ep(position); ep = (ep_file == CHESS_FILE_INVALID) ? CHESS_SQUARE_INVALID : chess_square_from_fr(ep_file, (color == CHESS_COLOR_WHITE) ? CHESS_RANK_6 : CHESS_RANK_3); if (gen->d == 0) { if (gen->to == CHESS_SQUARE_INVALID) { gen->to = gen->sq + slide; if (chess_position_piece(position, gen->to) == CHESS_PIECE_NONE) { if (chess_square_rank(gen->to) == end_rank) goto gen_pawn_promotes; else return chess_move_make(gen->sq, gen->to); } } else if (chess_square_rank(gen->sq) == start_rank && gen->to == gen->sq + slide) { gen->to += slide; if (chess_position_piece(position, gen->to) == CHESS_PIECE_NONE) return chess_move_make(gen->sq, gen->to); } gen->to = CHESS_SQUARE_INVALID; gen->d = 1; } while (gen->d < 8) { if (dirs_array[gen->d] & dirs) { gen->to = gen->sq + slides_array[gen->d]; piece = chess_position_piece(position, gen->to); gen->d += 2; if (piece != CHESS_PIECE_NONE && chess_piece_color(piece) != color) { if (chess_square_rank(gen->to) == end_rank) goto gen_pawn_promotes; else return chess_move_make(gen->sq, gen->to); } else if (gen->to == ep) { return chess_move_make(gen->sq, gen->to); } } else { gen->d += 2; } } gen->to = CHESS_SQUARE_INVALID; gen->d = 0; break; case CHESS_PIECE_WHITE_KNIGHT: case CHESS_PIECE_BLACK_KNIGHT: dirs = jump_dirs[gen->sq]; for (; gen->d < 8; gen->d++) { dir = dirs_array[gen->d]; if ((dir & dirs) == 0) continue; gen->to = gen->sq + jumps_array[gen->d]; target = chess_position_piece(position, gen->to); if (target == CHESS_PIECE_NONE || chess_piece_color(target) != color) { gen->d++; return chess_move_make(gen->sq, gen->to); } } gen->to = CHESS_SQUARE_INVALID; gen->d = 0; break; case CHESS_PIECE_WHITE_BISHOP: case CHESS_PIECE_BLACK_BISHOP: piece_dirs = bishop_dirs; goto gen_slide_moves; case CHESS_PIECE_WHITE_ROOK: case CHESS_PIECE_BLACK_ROOK: piece_dirs = rook_dirs; goto gen_slide_moves; case CHESS_PIECE_WHITE_QUEEN: case CHESS_PIECE_BLACK_QUEEN: piece_dirs = queen_dirs; gen_slide_moves: while (gen->d < 8) { dir = dirs_array[gen->d] & piece_dirs; if (gen->to == CHESS_SQUARE_INVALID) gen->to = gen->sq; do { dirs = slide_dirs[gen->to]; if ((dir & dirs) == 0) break; gen->to += slides_array[gen->d]; target = chess_position_piece(position, gen->to); if (target == CHESS_PIECE_NONE) return chess_move_make(gen->sq, gen->to); else if (chess_piece_color(target) != color) { move = chess_move_make(gen->sq, gen->to); gen->d++; gen->to = CHESS_SQUARE_INVALID; return move; } } while (target == CHESS_PIECE_NONE); gen->to = CHESS_SQUARE_INVALID; gen->d++; } gen->to = CHESS_SQUARE_INVALID; gen->d = 0; break; case CHESS_PIECE_WHITE_KING: case CHESS_PIECE_BLACK_KING: dirs = slide_dirs[gen->sq]; for (; gen->d < 8; gen->d++) { dir = dirs_array[gen->d]; if ((dir & dirs) == 0) continue; gen->to = gen->sq + slides_array[gen->d]; target = chess_position_piece(position, gen->to); if (target == CHESS_PIECE_NONE || chess_piece_color(target) != color) { gen->d++; return chess_move_make(gen->sq, gen->to); } } gen->to = CHESS_SQUARE_INVALID; gen->d = 0; break; default: assert(0); break; } } if (gen->castle == -1) { if (!chess_position_is_check(position)) gen->castle = chess_position_castle(position); else { gen->castle = CHESS_CASTLE_STATE_NONE; } } if (color == CHESS_COLOR_WHITE) { if ((gen->castle & CHESS_CASTLE_STATE_WK) && chess_position_piece(position, CHESS_SQUARE_F1) == CHESS_PIECE_NONE && chess_position_piece(position, CHESS_SQUARE_G1) == CHESS_PIECE_NONE && !chess_generate_is_square_attacked(position, CHESS_SQUARE_F1, CHESS_COLOR_BLACK) && !chess_generate_is_square_attacked(position, CHESS_SQUARE_G1, CHESS_COLOR_BLACK)) { gen->castle &= ~CHESS_CASTLE_STATE_WK; return chess_move_make(CHESS_SQUARE_E1, CHESS_SQUARE_G1); } if ((gen->castle & CHESS_CASTLE_STATE_WQ) && chess_position_piece(position, CHESS_SQUARE_B1) == CHESS_PIECE_NONE && chess_position_piece(position, CHESS_SQUARE_C1) == CHESS_PIECE_NONE && chess_position_piece(position, CHESS_SQUARE_D1) == CHESS_PIECE_NONE && !chess_generate_is_square_attacked(position, CHESS_SQUARE_D1, CHESS_COLOR_BLACK) && !chess_generate_is_square_attacked(position, CHESS_SQUARE_C1, CHESS_COLOR_BLACK)) { gen->castle &= ~CHESS_CASTLE_STATE_WQ; return chess_move_make(CHESS_SQUARE_E1, CHESS_SQUARE_C1); } } else { if ((gen->castle & CHESS_CASTLE_STATE_BK) && chess_position_piece(position, CHESS_SQUARE_F8) == CHESS_PIECE_NONE && chess_position_piece(position, CHESS_SQUARE_G8) == CHESS_PIECE_NONE && !chess_generate_is_square_attacked(position, CHESS_SQUARE_F8, CHESS_COLOR_WHITE) && !chess_generate_is_square_attacked(position, CHESS_SQUARE_G8, CHESS_COLOR_WHITE)) { gen->castle &= ~CHESS_CASTLE_STATE_BK; return chess_move_make(CHESS_SQUARE_E8, CHESS_SQUARE_G8); } if ((gen->castle & CHESS_CASTLE_STATE_BQ) && chess_position_piece(position, CHESS_SQUARE_B8) == CHESS_PIECE_NONE && chess_position_piece(position, CHESS_SQUARE_C8) == CHESS_PIECE_NONE && chess_position_piece(position, CHESS_SQUARE_D8) == CHESS_PIECE_NONE && !chess_generate_is_square_attacked(position, CHESS_SQUARE_D8, CHESS_COLOR_WHITE) && !chess_generate_is_square_attacked(position, CHESS_SQUARE_C8, CHESS_COLOR_WHITE)) { gen->castle &= ~CHESS_CASTLE_STATE_BQ; return chess_move_make(CHESS_SQUARE_E8, CHESS_SQUARE_C8); } } return 0; }