]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Update pinned bitboards and friends in do_move()
[stockfish] / src / position.cpp
index e2eeda85ac1e3c5d1f12728d530a840d1039b1a6..0a7ebadda7f3b5614668781f715f6c067cc7ad3f 100644 (file)
@@ -206,6 +206,7 @@ void Position::from_fen(const std::string& fen) {
   castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO;
 
   find_checkers();
+  find_pinned();
 
   st->key = compute_key();
   st->pawnKey = compute_pawn_key();
@@ -319,44 +320,11 @@ void Position::copy(const Position &pos) {
 }
 
 
-/// Position:pinned_pieces() returns a bitboard of all pinned (against the
-/// king) pieces for the given color.
-Bitboard Position::pinned_pieces(Color c) const {
-
-  if (st->pinned[c] != ~EmptyBoardBB)
-      return st->pinned[c];
-
-  Bitboard p1, p2;
-  Square ksq = king_square(c);
-  st->pinned[c] = hidden_checks<ROOK, true>(c, ksq, p1) | hidden_checks<BISHOP, true>(c, ksq, p2);
-  st->pinners[c] = p1 | p2;
-  return st->pinned[c];
-}
-
-Bitboard Position::pinned_pieces(Color c, Bitboard& p) const {
-
-  if (st->pinned[c] == ~EmptyBoardBB)
-      pinned_pieces(c);
-
-  p = st->pinners[c];
-  return st->pinned[c];
-}
-
-Bitboard Position::discovered_check_candidates(Color c) const {
-
-  if (st->dcCandidates[c] != ~EmptyBoardBB)
-      return st->dcCandidates[c];
-
-  Bitboard dummy;
-  Square ksq = king_square(opposite_color(c));
-  st->dcCandidates[c] = hidden_checks<ROOK, false>(c, ksq, dummy) | hidden_checks<BISHOP, false>(c, ksq, dummy);
-  return st->dcCandidates[c];
-}
-
 /// Position:hidden_checks<>() returns a bitboard of all pinned (against the
 /// king) pieces for the given color and for the given pinner type. Or, when
 /// template parameter FindPinned is false, the pinned pieces of opposite color
 /// that are, indeed, the pieces candidate for a discovery check.
+/// Note that checkersBB bitboard must be already updated.
 template<PieceType Piece, bool FindPinned>
 Bitboard Position::hidden_checks(Color c, Square ksq, Bitboard& pinners) const {
 
@@ -466,7 +434,7 @@ bool Position::move_attacks_square(Move m, Square s) const {
 
 
 /// Position::find_checkers() computes the checkersBB bitboard, which
-/// contains a nonzero bit for each checking piece (0, 1 or 2).  It
+/// contains a nonzero bit for each checking piece (0, 1 or 2). It
 /// currently works by calling Position::attacks_to, which is probably
 /// inefficient. Consider rewriting this function to use the last move
 /// played, like in non-bitboard versions of Glaurung.
@@ -478,6 +446,25 @@ void Position::find_checkers() {
 }
 
 
+/// Position:find_pinned() computes the pinned, pinners and dcCandidates
+/// bitboards for both colors. Bitboard checkersBB must be already updated.
+
+void Position::find_pinned() {
+
+  Bitboard p1, p2;
+  Square ksq;
+
+  for (Color c = WHITE; c <= BLACK; c++)
+  {
+      ksq = king_square(c);
+      st->pinned[c] = hidden_checks<ROOK, true>(c, ksq, p1) | hidden_checks<BISHOP, true>(c, ksq, p2);
+      st->pinners[c] = p1 | p2;
+      ksq = king_square(opposite_color(c));
+      st->dcCandidates[c] = hidden_checks<ROOK, false>(c, ksq, p1) | hidden_checks<BISHOP, false>(c, ksq, p2);
+  }
+}
+
+
 /// Position::pl_move_is_legal() tests whether a pseudo-legal move is legal
 
 bool Position::pl_move_is_legal(Move m) const {
@@ -719,10 +706,6 @@ void Position::do_move(Move m, StateInfo& newSt) {
   // case of non-reversible moves is taken care of later.
   st->rule50++;
 
-  // Reset pinned bitboard and its friends
-  for (Color c = WHITE; c <= BLACK; c++)
-      st->pinners[c] = st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB;
-
   if (move_is_castle(m))
       do_castle_move(m);
   else if (move_promotion(m))
@@ -823,6 +806,7 @@ void Position::do_move(Move m, StateInfo& newSt) {
   }
 
   // Finish
+  find_pinned();
   st->key ^= zobSideToMove;
   sideToMove = opposite_color(sideToMove);
   gamePly++;
@@ -1199,7 +1183,7 @@ void Position::undo_move(Move m) {
 
       if (st->capture)
       {
-          assert(capture != KING);
+          assert(st->capture != KING);
 
           // Replace the captured piece
           set_bit(&(byColorBB[them]), to);
@@ -1348,7 +1332,7 @@ void Position::undo_promotion_move(Move m) {
 
   if (st->capture)
   {
-      assert(capture != KING);
+      assert(st->capture != KING);
 
       // Insert captured piece:
       set_bit(&(byColorBB[them]), to);
@@ -1358,7 +1342,7 @@ void Position::undo_promotion_move(Move m) {
 
       // Update material. Because the move is a promotion move, we know
       // that the captured piece cannot be a pawn.
-      assert(capture != PAWN);
+      assert(st->capture != PAWN);
       npMaterial[them] += piece_value_midgame(st->capture);
 
       // Update piece list
@@ -1389,7 +1373,7 @@ void Position::undo_ep_move(Move m) {
   Square to = move_to(m);
   Square capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
 
-  assert(to == ep_square());
+  assert(to == st->previous->epSquare);
   assert(relative_rank(us, to) == RANK_6);
   assert(piece_on(to) == piece_of_color_and_type(us, PAWN));
   assert(piece_on(from) == EMPTY);