X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=bc8e92a1d77a29980bb9c274e5cbf434dbdfa7c5;hp=cd91b07fe773e5b9ba766d4055a36867c72b5c5f;hb=2731bbaf6b4bed23abaae8de5c1fa9f373e30e57;hpb=c73706243672bf36b0fef58e817f843cb341d8ca diff --git a/src/position.cpp b/src/position.cpp index cd91b07f..bc8e92a1 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -81,25 +81,6 @@ PieceType min_attacker(const Bitboard*, Square, Bitboard, Bitboard&, Bitbo } // namespace -/// CheckInfo constructor - -CheckInfo::CheckInfo(const Position& pos) { - - Color them = ~pos.side_to_move(); - ksq = pos.square(them); - - pinned = pos.pinned_pieces(pos.side_to_move()); - dcCandidates = pos.discovered_check_candidates(); - - checkSquares[PAWN] = pos.attacks_from(ksq, them); - checkSquares[KNIGHT] = pos.attacks_from(ksq); - checkSquares[BISHOP] = pos.attacks_from(ksq); - checkSquares[ROOK] = pos.attacks_from(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) { @@ -311,6 +292,24 @@ void Position::set_castling_right(Color c, Square rfrom) { } +/// 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(WHITE)); + si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square(BLACK)); + + Square ksq = square(~sideToMove); + + si->checkSquares[PAWN] = attacks_from(ksq, ~sideToMove); + si->checkSquares[KNIGHT] = attacks_from(ksq); + si->checkSquares[BISHOP] = attacks_from(ksq); + si->checkSquares[ROOK] = attacks_from(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 @@ -321,9 +320,10 @@ void Position::set_state(StateInfo* si) const { si->key = si->pawnKey = si->materialKey = 0; si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO; si->psq = SCORE_ZERO; - si->checkersBB = attackers_to(square(sideToMove)) & pieces(~sideToMove); + set_check_info(si); + for (Bitboard b = pieces(); b; ) { Square s = pop_lsb(&b); @@ -420,28 +420,27 @@ Phase Position::game_phase() const { } -/// 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(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; } @@ -463,10 +462,9 @@ Bitboard Position::attackers_to(Square s, Bitboard occupied) const { /// 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); @@ -501,7 +499,7 @@ bool Position::legal(Move m, Bitboard pinned) const { // 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(us)); } @@ -580,22 +578,21 @@ bool Position::pseudo_legal(const Move m) const { /// 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(~sideToMove))) return true; switch (type_of(m)) @@ -604,7 +601,7 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const { 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(~sideToMove); // En passant capture with check? We have already handled the case // of direct checks and ordinary discovered check, so the only case we @@ -615,8 +612,8 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const { 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(ci.ksq, b) & pieces(sideToMove, QUEEN, BISHOP)); + return (attacks_bb< ROOK>(square(~sideToMove), b) & pieces(sideToMove, QUEEN, ROOK)) + | (attacks_bb(square(~sideToMove), b) & pieces(sideToMove, QUEEN, BISHOP)); } case CASTLING: { @@ -625,8 +622,8 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const { 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(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & ci.ksq); + return (PseudoAttacks[ROOK][rto] & square(~sideToMove)) + && (attacks_bb(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & square(~sideToMove)); } default: assert(false); @@ -802,6 +799,9 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { sideToMove = ~sideToMove; + // Update king attacks used for fast check detection + set_check_info(st); + assert(pos_is_ok()); } @@ -915,6 +915,8 @@ void Position::do_null_move(StateInfo& newSt) { sideToMove = ~sideToMove; + set_check_info(st); + assert(pos_is_ok()); }