Compare commits
1
Commits
c45450255a
...
695d2dd30d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
695d2dd30d |
No files matched your search
+10
-2
@@ -128,10 +128,18 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) {
|
|||||||
alpha = std::max(alpha, stand_pat);
|
alpha = std::max(alpha, stand_pat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Out of check only captures and queen promotions are searched, so only
|
||||||
|
// those are generated; a position without any of them is still checked
|
||||||
|
// for stalemate.
|
||||||
MoveList list;
|
MoveList list;
|
||||||
|
if (in_check) {
|
||||||
generate_legal(pos, list);
|
generate_legal(pos, list);
|
||||||
if (list.empty()) return in_check ? -MATE + ply : 0;
|
if (list.empty()) return -MATE + ply;
|
||||||
if (in_check && ply >= QS_MAX_PLY) return static_eval(pos);
|
if (ply >= QS_MAX_PLY) return static_eval(pos);
|
||||||
|
} else {
|
||||||
|
generate_noisy(pos, list);
|
||||||
|
if (list.empty()) return has_legal_move(pos) ? best : 0;
|
||||||
|
}
|
||||||
|
|
||||||
Move moves[256];
|
Move moves[256];
|
||||||
int scores[256];
|
int scores[256];
|
||||||
|
|||||||
+89
-18
@@ -21,10 +21,25 @@ void add_pawn_move(MoveList& list, int from, int to, bool capture, bool promotio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
// Our pieces pinned to our king by an enemy slider.
|
||||||
|
u64 pinned_pieces(const Position& pos, int us, int ksq, u64 occ) {
|
||||||
|
const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN];
|
||||||
|
const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN];
|
||||||
|
u64 pinned = 0;
|
||||||
|
u64 snipers = ((attacks::rook(ksq, 0) & orth_sliders) | (attacks::bishop(ksq, 0) & diag_sliders)) & pos.colors[us ^ 1];
|
||||||
|
while (snipers) {
|
||||||
|
const int s = pop_lsb(snipers);
|
||||||
|
const u64 blockers = attacks::between[ksq][s] & occ;
|
||||||
|
if (blockers && !(blockers & (blockers - 1)) && (blockers & pos.colors[us])) pinned |= blockers;
|
||||||
|
}
|
||||||
|
return pinned;
|
||||||
|
}
|
||||||
|
|
||||||
// Fully legal generation using check and pin masks.
|
// Fully legal generation using check and pin masks. With NOISY only captures
|
||||||
void generate_legal(const Position& pos, MoveList& list) {
|
// (en passant included) and queen promotions, in the same order as the full
|
||||||
|
// generation lists them.
|
||||||
|
template <bool NOISY>
|
||||||
|
void generate(const Position& pos, MoveList& list) {
|
||||||
list.clear();
|
list.clear();
|
||||||
|
|
||||||
const int us = pos.stm;
|
const int us = pos.stm;
|
||||||
@@ -40,7 +55,7 @@ void generate_legal(const Position& pos, MoveList& list) {
|
|||||||
// King moves: the king itself must not block a slider ray on its destination.
|
// King moves: the king itself must not block a slider ray on its destination.
|
||||||
{
|
{
|
||||||
const u64 occ_no_king = occ ^ bb(ksq);
|
const u64 occ_no_king = occ ^ bb(ksq);
|
||||||
u64 targets = attacks::king[ksq] & ~ours;
|
u64 targets = attacks::king[ksq] & (NOISY ? theirs : ~ours);
|
||||||
while (targets) {
|
while (targets) {
|
||||||
const int to = pop_lsb(targets);
|
const int to = pop_lsb(targets);
|
||||||
if (!pos.square_attacked(to, them, occ_no_king))
|
if (!pos.square_attacked(to, them, occ_no_king))
|
||||||
@@ -51,30 +66,23 @@ void generate_legal(const Position& pos, MoveList& list) {
|
|||||||
if (num_checkers > 1) return;
|
if (num_checkers > 1) return;
|
||||||
|
|
||||||
const u64 check_mask = num_checkers ? (attacks::between[ksq][lsb(checkers)] | checkers) : ~0ULL;
|
const u64 check_mask = num_checkers ? (attacks::between[ksq][lsb(checkers)] | checkers) : ~0ULL;
|
||||||
|
// Squares a piece may move to: anything but our own pieces, or only theirs.
|
||||||
|
const u64 target_mask = check_mask & (NOISY ? theirs : ~ours);
|
||||||
const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN];
|
const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN];
|
||||||
const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN];
|
const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN];
|
||||||
|
const u64 pinned = pinned_pieces(pos, us, ksq, occ);
|
||||||
u64 pinned = 0;
|
|
||||||
{
|
|
||||||
u64 snipers = ((attacks::rook(ksq, 0) & orth_sliders) | (attacks::bishop(ksq, 0) & diag_sliders)) & theirs;
|
|
||||||
while (snipers) {
|
|
||||||
const int s = pop_lsb(snipers);
|
|
||||||
const u64 blockers = attacks::between[ksq][s] & occ;
|
|
||||||
if (blockers && !(blockers & (blockers - 1)) && (blockers & ours)) pinned |= blockers;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A pinned knight can never move.
|
// A pinned knight can never move.
|
||||||
u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned;
|
u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned;
|
||||||
while (knights) {
|
while (knights) {
|
||||||
const int from = pop_lsb(knights);
|
const int from = pop_lsb(knights);
|
||||||
add_targets(list, from, attacks::knight[from] & ~ours & check_mask, theirs);
|
add_targets(list, from, attacks::knight[from] & target_mask, theirs);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 diag = diag_sliders & ours;
|
u64 diag = diag_sliders & ours;
|
||||||
while (diag) {
|
while (diag) {
|
||||||
const int from = pop_lsb(diag);
|
const int from = pop_lsb(diag);
|
||||||
u64 targets = attacks::bishop(from, occ) & ~ours & check_mask;
|
u64 targets = attacks::bishop(from, occ) & target_mask;
|
||||||
if (pinned & bb(from)) targets &= attacks::line[ksq][from];
|
if (pinned & bb(from)) targets &= attacks::line[ksq][from];
|
||||||
add_targets(list, from, targets, theirs);
|
add_targets(list, from, targets, theirs);
|
||||||
}
|
}
|
||||||
@@ -82,7 +90,7 @@ void generate_legal(const Position& pos, MoveList& list) {
|
|||||||
u64 orth = orth_sliders & ours;
|
u64 orth = orth_sliders & ours;
|
||||||
while (orth) {
|
while (orth) {
|
||||||
const int from = pop_lsb(orth);
|
const int from = pop_lsb(orth);
|
||||||
u64 targets = attacks::rook(from, occ) & ~ours & check_mask;
|
u64 targets = attacks::rook(from, occ) & target_mask;
|
||||||
if (pinned & bb(from)) targets &= attacks::line[ksq][from];
|
if (pinned & bb(from)) targets &= attacks::line[ksq][from];
|
||||||
add_targets(list, from, targets, theirs);
|
add_targets(list, from, targets, theirs);
|
||||||
}
|
}
|
||||||
@@ -99,11 +107,15 @@ void generate_legal(const Position& pos, MoveList& list) {
|
|||||||
|
|
||||||
const int to = from + up;
|
const int to = from + up;
|
||||||
if (!(occ & bb(to))) {
|
if (!(occ & bb(to))) {
|
||||||
|
if constexpr (NOISY) {
|
||||||
|
if ((allowed & bb(to)) && (promo_rank & bb(to))) list.add(Move(from, to, Move::PROMO + 3));
|
||||||
|
} else {
|
||||||
if (allowed & bb(to)) add_pawn_move(list, from, to, false, promo_rank & bb(to));
|
if (allowed & bb(to)) add_pawn_move(list, from, to, false, promo_rank & bb(to));
|
||||||
const int to2 = to + up;
|
const int to2 = to + up;
|
||||||
if ((start_rank & bb(from)) && !(occ & bb(to2)) && (allowed & bb(to2)))
|
if ((start_rank & bb(from)) && !(occ & bb(to2)) && (allowed & bb(to2)))
|
||||||
list.add(Move(from, to2, Move::DOUBLE_PUSH));
|
list.add(Move(from, to2, Move::DOUBLE_PUSH));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
u64 captures = attacks::pawn[us][from] & theirs & allowed;
|
u64 captures = attacks::pawn[us][from] & theirs & allowed;
|
||||||
while (captures) {
|
while (captures) {
|
||||||
@@ -124,7 +136,7 @@ void generate_legal(const Position& pos, MoveList& list) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num_checkers) return;
|
if (NOISY || num_checkers) return;
|
||||||
|
|
||||||
const int rank_base = us == WHITE ? 0 : 56;
|
const int rank_base = us == WHITE ? 0 : 56;
|
||||||
const int oo = us == WHITE ? WHITE_OO : BLACK_OO;
|
const int oo = us == WHITE ? WHITE_OO : BLACK_OO;
|
||||||
@@ -142,4 +154,63 @@ void generate_legal(const Position& pos, MoveList& list) {
|
|||||||
list.add(Move(ksq, rank_base + 2, Move::QUEEN_CASTLE));
|
list.add(Move(ksq, rank_base + 2, Move::QUEEN_CASTLE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void generate_legal(const Position& pos, MoveList& list) { generate<false>(pos, list); }
|
||||||
|
|
||||||
|
void generate_noisy(const Position& pos, MoveList& list) { generate<true>(pos, list); }
|
||||||
|
|
||||||
|
bool has_legal_move(const Position& pos) {
|
||||||
|
const int us = pos.stm;
|
||||||
|
const int them = us ^ 1;
|
||||||
|
const u64 occ = pos.occupancy();
|
||||||
|
const u64 ours = pos.colors[us];
|
||||||
|
const int ksq = pos.king_square(us);
|
||||||
|
|
||||||
|
// In check or with an en passant capture around, just generate everything.
|
||||||
|
if (pos.in_check() || pos.ep != NO_SQUARE) {
|
||||||
|
MoveList list;
|
||||||
|
generate<false>(pos, list);
|
||||||
|
return !list.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not in check, so castling only adds moves when the king can already step
|
||||||
|
// to the square next to it; the king, the pieces and the pawns decide.
|
||||||
|
const u64 pinned = pinned_pieces(pos, us, ksq, occ);
|
||||||
|
|
||||||
|
u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned;
|
||||||
|
while (knights)
|
||||||
|
if (attacks::knight[pop_lsb(knights)] & ~ours) return true;
|
||||||
|
|
||||||
|
u64 targets = attacks::king[ksq] & ~ours;
|
||||||
|
while (targets)
|
||||||
|
if (!pos.square_attacked(pop_lsb(targets), them, occ ^ bb(ksq))) return true;
|
||||||
|
|
||||||
|
u64 sliders = (pos.pieces[BISHOP] | pos.pieces[QUEEN]) & ours;
|
||||||
|
while (sliders) {
|
||||||
|
const int from = pop_lsb(sliders);
|
||||||
|
u64 t = attacks::bishop(from, occ) & ~ours;
|
||||||
|
if (pinned & bb(from)) t &= attacks::line[ksq][from];
|
||||||
|
if (t) return true;
|
||||||
|
}
|
||||||
|
sliders = (pos.pieces[ROOK] | pos.pieces[QUEEN]) & ours;
|
||||||
|
while (sliders) {
|
||||||
|
const int from = pop_lsb(sliders);
|
||||||
|
u64 t = attacks::rook(from, occ) & ~ours;
|
||||||
|
if (pinned & bb(from)) t &= attacks::line[ksq][from];
|
||||||
|
if (t) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int up = us == WHITE ? 8 : -8;
|
||||||
|
u64 pawns = pos.pieces_of(us, PAWN);
|
||||||
|
while (pawns) {
|
||||||
|
const int from = pop_lsb(pawns);
|
||||||
|
u64 t = attacks::pawn[us][from] & pos.colors[them];
|
||||||
|
if (!(occ & bb(from + up))) t |= bb(from + up);
|
||||||
|
if (pinned & bb(from)) t &= attacks::line[ksq][from];
|
||||||
|
if (t) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace rigby
|
} // namespace rigby
|
||||||
@@ -54,5 +54,10 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
void generate_legal(const Position& pos, MoveList& list);
|
void generate_legal(const Position& pos, MoveList& list);
|
||||||
|
// Legal captures (en passant included) and queen promotions, in the order
|
||||||
|
// generate_legal lists them.
|
||||||
|
void generate_noisy(const Position& pos, MoveList& list);
|
||||||
|
// Same as a non-empty generate_legal, but stops at the first move it finds.
|
||||||
|
bool has_legal_move(const Position& pos);
|
||||||
|
|
||||||
} // namespace rigby
|
} // namespace rigby
|
||||||
Reference in new issue
Block a user