Key enpassant[FILE_NB];
Key castling[CASTLING_RIGHT_NB];
Key side;
- Key exclusion;
}
-Key Position::exclusion_key() const { return st->key ^ Zobrist::exclusion; }
-
namespace {
const string PieceToChar(" PNBRQK pnbrqk");
} // namespace
-/// CheckInfo constructor
-
-CheckInfo::CheckInfo(const Position& pos) {
-
- Color them = ~pos.side_to_move();
- ksq = pos.square<KING>(them);
-
- pinned = pos.pinned_pieces(pos.side_to_move());
- dcCandidates = pos.discovered_check_candidates();
-
- checkSquares[PAWN] = pos.attacks_from<PAWN>(ksq, them);
- checkSquares[KNIGHT] = pos.attacks_from<KNIGHT>(ksq);
- checkSquares[BISHOP] = pos.attacks_from<BISHOP>(ksq);
- checkSquares[ROOK] = pos.attacks_from<ROOK>(ksq);
- checkSquares[QUEEN] = checkSquares[BISHOP] | checkSquares[ROOK];
- checkSquares[KING] = 0;
-}
-
-
/// operator<<(Position) returns an ASCII representation of the position
std::ostream& operator<<(std::ostream& os, const Position& pos) {
}
Zobrist::side = rng.rand<Key>();
- Zobrist::exclusion = rng.rand<Key>();
}
}
+/// Position::set_check_info() sets king attacks to detect if a move gives check
+
+void Position::set_check_info(StateInfo* si) const {
+
+ si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square<KING>(WHITE));
+ si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square<KING>(BLACK));
+
+ Square ksq = square<KING>(~sideToMove);
+
+ si->checkSquares[PAWN] = attacks_from<PAWN>(ksq, ~sideToMove);
+ si->checkSquares[KNIGHT] = attacks_from<KNIGHT>(ksq);
+ si->checkSquares[BISHOP] = attacks_from<BISHOP>(ksq);
+ si->checkSquares[ROOK] = attacks_from<ROOK>(ksq);
+ si->checkSquares[QUEEN] = si->checkSquares[BISHOP] | si->checkSquares[ROOK];
+ si->checkSquares[KING] = 0;
+}
+
+
/// Position::set_state() computes the hash keys of the position, and other
/// data that once computed is updated incrementally as moves are made.
/// The function is only used when a new position is set up, and to verify
si->key = si->pawnKey = si->materialKey = 0;
si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO;
si->psq = SCORE_ZERO;
-
si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);
+ set_check_info(si);
+
for (Bitboard b = pieces(); b; )
{
Square s = pop_lsb(&b);
}
-/// Position::check_blockers() returns a bitboard of all the pieces with color
-/// 'c' that are blocking check on the king with color 'kingColor'. A piece
-/// blocks a check if removing that piece from the board would result in a
-/// position where the king is in check. A check blocking piece can be either a
-/// pinned or a discovered check piece, according if its color 'c' is the same
-/// or the opposite of 'kingColor'.
+/// Position::slider_blockers() returns a bitboard of all the pieces (both colors) that
+/// are blocking attacks on the square 's' from 'sliders'. A piece blocks a slider
+/// if removing that piece from the board would result in a position where square 's'
+/// is attacked. For example, a king-attack blocking piece can be either a pinned or
+/// a discovered check piece, according if its color is the opposite or the same of
+/// the color of the slider.
-Bitboard Position::check_blockers(Color c, Color kingColor) const {
+Bitboard Position::slider_blockers(Bitboard sliders, Square s) const {
Bitboard b, pinners, result = 0;
- Square ksq = square<KING>(kingColor);
- // Pinners are sliders that give check when a pinned piece is removed
- pinners = ( (pieces( ROOK, QUEEN) & PseudoAttacks[ROOK ][ksq])
- | (pieces(BISHOP, QUEEN) & PseudoAttacks[BISHOP][ksq])) & pieces(~kingColor);
+ // Pinners are sliders that attack 's' when a pinned piece is removed
+ pinners = ( (PseudoAttacks[ROOK ][s] & pieces(QUEEN, ROOK))
+ | (PseudoAttacks[BISHOP][s] & pieces(QUEEN, BISHOP))) & sliders;
while (pinners)
{
- b = between_bb(ksq, pop_lsb(&pinners)) & pieces();
+ b = between_bb(s, pop_lsb(&pinners)) & pieces();
if (!more_than_one(b))
- result |= b & pieces(c);
+ result |= b;
}
return result;
}
/// Position::legal() tests whether a pseudo-legal move is legal
-bool Position::legal(Move m, Bitboard pinned) const {
+bool Position::legal(Move m) const {
assert(is_ok(m));
- assert(pinned == pinned_pieces(sideToMove));
Color us = sideToMove;
Square from = from_sq(m);
// A non-king move is legal if and only if it is not pinned or it
// is moving along the ray towards or away from the king.
- return !(pinned & from)
+ return !(pinned_pieces(us) & from)
|| aligned(from, to_sq(m), square<KING>(us));
}
/// Position::gives_check() tests whether a pseudo-legal move gives a check
-bool Position::gives_check(Move m, const CheckInfo& ci) const {
+bool Position::gives_check(Move m) const {
assert(is_ok(m));
- assert(ci.dcCandidates == discovered_check_candidates());
assert(color_of(moved_piece(m)) == sideToMove);
Square from = from_sq(m);
Square to = to_sq(m);
// Is there a direct check?
- if (ci.checkSquares[type_of(piece_on(from))] & to)
+ if (st->checkSquares[type_of(piece_on(from))] & to)
return true;
// Is there a discovered check?
- if ( (ci.dcCandidates & from)
- && !aligned(from, to, ci.ksq))
+ if ( (discovered_check_candidates() & from)
+ && !aligned(from, to, square<KING>(~sideToMove)))
return true;
switch (type_of(m))
return false;
case PROMOTION:
- return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & ci.ksq;
+ return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & square<KING>(~sideToMove);
// En passant capture with check? We have already handled the case
// of direct checks and ordinary discovered check, so the only case we
Square capsq = make_square(file_of(to), rank_of(from));
Bitboard b = (pieces() ^ from ^ capsq) | to;
- return (attacks_bb< ROOK>(ci.ksq, b) & pieces(sideToMove, QUEEN, ROOK))
- | (attacks_bb<BISHOP>(ci.ksq, b) & pieces(sideToMove, QUEEN, BISHOP));
+ return (attacks_bb< ROOK>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, ROOK))
+ | (attacks_bb<BISHOP>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, BISHOP));
}
case CASTLING:
{
Square kto = relative_square(sideToMove, rfrom > kfrom ? SQ_G1 : SQ_C1);
Square rto = relative_square(sideToMove, rfrom > kfrom ? SQ_F1 : SQ_D1);
- return (PseudoAttacks[ROOK][rto] & ci.ksq)
- && (attacks_bb<ROOK>(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & ci.ksq);
+ return (PseudoAttacks[ROOK][rto] & square<KING>(~sideToMove))
+ && (attacks_bb<ROOK>(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & square<KING>(~sideToMove));
}
default:
assert(false);
sideToMove = ~sideToMove;
+ // Update king attacks used for fast check detection
+ set_check_info(st);
+
assert(pos_is_ok());
}
sideToMove = ~sideToMove;
+ set_check_info(st);
+
assert(pos_is_ok());
}