]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Use update_checkers<>() also for PAWN
[stockfish] / src / position.cpp
index 58884c37f42cb4ee778c9b0a889303b16d6a7b65..ed7510101de89fbbd38270113019d82b710d8b7a 100644 (file)
@@ -39,6 +39,8 @@
 //// Variables
 ////
 
+extern SearchStack EmptySearchStack;
+
 int Position::castleRightsMask[64];
 
 Key Position::zobrist[2][8][64];
@@ -50,6 +52,7 @@ Key Position::zobSideToMove;
 Value Position::MgPieceSquareTable[16][64];
 Value Position::EgPieceSquareTable[16][64];
 
+static bool RequestPending = false;
 
 ////
 //// Functions
@@ -57,11 +60,11 @@ Value Position::EgPieceSquareTable[16][64];
 
 /// Constructors
 
-Position::Position(const Position &pos) {
+Position::Position(const Positionpos) {
   copy(pos);
 }
 
-Position::Position(const std::string &fen) {
+Position::Position(const std::stringfen) {
   from_fen(fen);
 }
 
@@ -70,7 +73,7 @@ Position::Position(const std::string &fen) {
 /// string. This function is not very robust - make sure that input FENs are
 /// correct (this is assumed to be the responsibility of the GUI).
 
-void Position::from_fen(const std::string &fen) {
+void Position::from_fen(const std::stringfen) {
 
   static const std::string pieceLetters = "KQRBNPkqrbnp";
   static const Piece pieces[] = { WK, WQ, WR, WB, WN, WP, BK, BQ, BR, BB, BN, BP };
@@ -270,12 +273,18 @@ void Position::print(Move m) const {
 
   static const std::string pieceLetters = " PNBRQK  PNBRQK .";
 
+  // Check for reentrancy, as example when called from inside
+  // MovePicker that is used also here in move_to_san()
+  if (RequestPending)
+      return;
+
+  RequestPending = true;
+
   std::cout << std::endl;
   if (m != MOVE_NONE)
   {
-      Position p(*this);
       std::string col = (color_of_piece_on(move_from(m)) == BLACK ? ".." : "");
-      std::cout << "Move is: " << col << move_to_san(p, m) << std::endl;
+      std::cout << "Move is: " << col << move_to_san(*this, m) << std::endl;
   }
   for (Rank rank = RANK_8; rank >= RANK_1; rank--)
   {
@@ -295,6 +304,8 @@ void Position::print(Move m) const {
   std::cout << "+---+---+---+---+---+---+---+---+" << std::endl
             << "Fen is: " << to_fen() << std::endl
             << "Key is: " << key << std::endl;
+
+  RequestPending = false;
 }
 
 
@@ -306,6 +317,25 @@ void Position::copy(const Position &pos) {
 }
 
 
+/// Position::update_checkers() updates checkers info, used in do_move()
+template<PieceType Piece>
+inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square from,
+                                      Square to, Bitboard dcCandidates) {
+
+  if (Piece != KING && bit_is_set(piece_attacks<Piece>(ksq), to))
+      set_bit(pCheckersBB, to);
+
+  if (Piece != QUEEN && bit_is_set(dcCandidates, from))
+  {
+      if (Piece != ROOK)
+          (*pCheckersBB) |= (piece_attacks<ROOK>(ksq) & rooks_and_queens(side_to_move()));
+
+      if (Piece != BISHOP)
+          (*pCheckersBB) |= (piece_attacks<BISHOP>(ksq) & bishops_and_queens(side_to_move()));
+  }
+}
+
+
 /// Position:pinned_pieces() returns a bitboard of all pinned (against the
 /// king) pieces for the given color.
 Bitboard Position::pinned_pieces(Color c) const {
@@ -368,19 +398,6 @@ Bitboard Position::hidden_checks(Color c, Square ksq) const {
 }
 
 
-/// Position::square_is_attacked() checks whether the given side attacks the
-/// given square.
-
-bool Position::square_is_attacked(Square s, Color c) const {
-
-  return   (pawn_attacks(opposite_color(c), s) & pawns(c))
-        || (piece_attacks<KNIGHT>(s) & knights(c))
-        || (piece_attacks<KING>(s)   & kings(c))
-        || (piece_attacks<ROOK>(s)   & rooks_and_queens(c))
-        || (piece_attacks<BISHOP>(s) & bishops_and_queens(c));
-}
-
-
 /// Position::attacks_to() computes a bitboard containing all pieces which
 /// attacks a given square. There are two versions of this function: One
 /// which finds attackers of both colors, and one which only finds the
@@ -396,12 +413,6 @@ Bitboard Position::attacks_to(Square s) const {
         | (piece_attacks<KING>(s)   & pieces_of_type(KING));
 }
 
-Bitboard Position::attacks_to(Square s, Color c) const {
-
-  return attacks_to(s) & pieces_of_color(c);
-}
-
-
 /// Position::piece_attacks_square() tests whether the piece on square f
 /// attacks square t.
 
@@ -664,10 +675,12 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const {
 
 
 /// Position::move_is_capture() tests whether a move from the current
-/// position is a capture.
+/// position is a capture. Move must not be MOVE_NONE.
 
 bool Position::move_is_capture(Move m) const {
 
+  assert(m != MOVE_NONE);
+
   return (   !square_is_empty(move_to(m))
           && (color_of_piece_on(move_to(m)) == opposite_color(side_to_move()))
          )
@@ -795,33 +808,34 @@ void Position::do_move(Move m, UndoInfo& u, Bitboard dcCandidates) {
     if (piece == KING)
         kingSquare[us] = to;
 
-    // If the move was a double pawn push, set the en passant square.
-    // This code is a bit ugly right now, and should be cleaned up later.
-    // FIXME
+    // Reset en passant square
     if (epSquare != SQ_NONE)
     {
         key ^= zobEp[epSquare];
         epSquare = SQ_NONE;
     }
+
+    // If the moving piece was a pawn do some special extra work
     if (piece == PAWN)
     {
+        // Reset rule 50 draw counter
+        rule50 = 0;
+
+        // Update pawn hash key
+        pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
+
+        // Set en passant square, only if moved pawn can be captured
         if (abs(int(to) - int(from)) == 16)
         {
-            if(   (   us == WHITE
-                   && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
-               || (   us == BLACK
-                   && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
+            if (   (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
+                || (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
             {
                 epSquare = Square((int(from) + int(to)) / 2);
                 key ^= zobEp[epSquare];
             }
         }
-        // Reset rule 50 draw counter
-        rule50 = 0;
-
-        // Update pawn hash key
-        pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
     }
+
     // Update piece lists
     pieceList[us][piece][index[from]] = to;
     index[to] = index[from];
@@ -832,54 +846,33 @@ void Position::do_move(Move m, UndoInfo& u, Bitboard dcCandidates) {
     castleRights &= castleRightsMask[to];
     key ^= zobCastle[castleRights];
 
-    // Update checkers bitboard
+    // Update checkers bitboard, piece must be already moved
     checkersBB = EmptyBoardBB;
     Square ksq = king_square(them);
     switch (piece)
     {
     case PAWN:
-        if (bit_is_set(pawn_attacks(them, ksq), to))
-            set_bit(&checkersBB, to);
-
-        if (bit_is_set(dcCandidates, from))
-            checkersBB |= ( (piece_attacks<ROOK>(ksq) & rooks_and_queens(us))
-                           |(piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
+        update_checkers<PAWN>(&checkersBB, ksq, from, to, dcCandidates);
         break;
 
     case KNIGHT:
-        if (bit_is_set(piece_attacks<KNIGHT>(ksq), to))
-            set_bit(&checkersBB, to);
-
-        if (bit_is_set(dcCandidates, from))
-            checkersBB |= ( (piece_attacks<ROOK>(ksq) & rooks_and_queens(us))
-                           |(piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
+        update_checkers<KNIGHT>(&checkersBB, ksq, from, to, dcCandidates);
         break;
 
     case BISHOP:
-        if  (bit_is_set(piece_attacks<BISHOP>(ksq), to))
-            set_bit(&checkersBB, to);
-
-        if (bit_is_set(dcCandidates, from))
-            checkersBB |= (piece_attacks<ROOK>(ksq) & rooks_and_queens(us));
+        update_checkers<BISHOP>(&checkersBB, ksq, from, to, dcCandidates);
         break;
 
     case ROOK:
-        if (bit_is_set(piece_attacks<ROOK>(ksq), to))
-            set_bit(&checkersBB, to);
-
-        if (bit_is_set(dcCandidates, from))
-            checkersBB |= (piece_attacks<BISHOP>(ksq) & bishops_and_queens(us));
+        update_checkers<ROOK>(&checkersBB, ksq, from, to, dcCandidates);
         break;
 
     case QUEEN:
-        if (bit_is_set(piece_attacks<QUEEN>(ksq), to))
-            set_bit(&checkersBB, to);
+        update_checkers<QUEEN>(&checkersBB, ksq, from, to, dcCandidates);
         break;
 
     case KING:
-        if (bit_is_set(dcCandidates, from))
-            checkersBB |= ( (piece_attacks<ROOK>(ksq) & rooks_and_queens(us))
-                           |(piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
+        update_checkers<KING>(&checkersBB, ksq, from, to, dcCandidates);
         break;
 
     default:
@@ -899,7 +892,6 @@ void Position::do_move(Move m, UndoInfo& u, Bitboard dcCandidates) {
   assert(is_ok());
 }
 
-
 /// Position::do_capture_move() is a private method used to update captured
 /// piece info. It is called from the main Position::do_move function.
 
@@ -1569,10 +1561,16 @@ void Position::undo_null_move(const UndoInfo &u) {
 
 
 /// Position::see() is a static exchange evaluator:  It tries to estimate the
-/// material gain or loss resulting from a move.  There are two versions of
-/// this function: One which takes a move as input, and one which takes a
-/// 'from' and a 'to' square.  The function does not yet understand promotions
-/// or en passant captures.
+/// material gain or loss resulting from a move.  There are three versions of
+/// this function: One which takes a destination square as input, one takes a
+/// move, and one which takes a 'from' and a 'to' square.  The function does
+/// not yet understand promotions or en passant captures.
+
+int Position::see(Square to) const {
+
+  assert(square_is_ok(to));
+  return see(SQ_NONE, to);
+}
 
 int Position::see(Move m) const {
 
@@ -1582,18 +1580,22 @@ int Position::see(Move m) const {
 
 int Position::see(Square from, Square to) const {
 
-  // Approximate material values, with pawn = 1
+  // Material values
   static const int seeValues[18] = {
-    0, 1, 3, 3, 5, 10, 100, 0, 0, 1, 3, 3, 5, 10, 100, 0, 0, 0
+    0, PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
+       RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10, 0,
+    0, PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
+       RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10, 0,
+    0, 0
   };
 
   Bitboard attackers, occ, b;
 
-  assert(square_is_ok(from));
+  assert(square_is_ok(from) || from == SQ_NONE);
   assert(square_is_ok(to));
 
   // Initialize colors
-  Color us = color_of_piece_on(from);
+  Color us = (from != SQ_NONE ? color_of_piece_on(from) : opposite_color(color_of_piece_on(to)));
   Color them = opposite_color(us);
 
   // Initialize pieces
@@ -1603,15 +1605,49 @@ int Position::see(Square from, Square to) const {
   // Find all attackers to the destination square, with the moving piece
   // removed, but possibly an X-ray attacker added behind it.
   occ = occupied_squares();
-  clear_bit(&occ, from);
-  attackers =  (rook_attacks_bb(to, occ)   & rooks_and_queens())
-             | (bishop_attacks_bb(to, occ) & bishops_and_queens())
-             | (piece_attacks<KNIGHT>(to)  & knights())
-             | (piece_attacks<KING>(to)    & kings())
-             | (pawn_attacks(WHITE, to)    & pawns(BLACK))
-             | (pawn_attacks(BLACK, to)    & pawns(WHITE));
-
-  // If the opponent has no attackers, we are finished
+
+  // Handle enpassant moves
+  if (ep_square() == to && type_of_piece_on(from) == PAWN)
+  {
+      assert(capture == EMPTY);
+
+      Square capQq = (side_to_move() == WHITE)? (to - DELTA_N) : (to - DELTA_S);
+      capture = piece_on(capQq);
+
+      assert(type_of_piece_on(capQq) == PAWN);
+
+      // Remove the captured pawn
+      clear_bit(&occ, capQq);
+  }
+
+  while (true)
+  {
+      clear_bit(&occ, from);
+      attackers =  (rook_attacks_bb(to, occ)   & rooks_and_queens())
+                 | (bishop_attacks_bb(to, occ) & bishops_and_queens())
+                 | (piece_attacks<KNIGHT>(to)  & knights())
+                 | (piece_attacks<KING>(to)    & kings())
+                 | (pawn_attacks(WHITE, to)    & pawns(BLACK))
+                 | (pawn_attacks(BLACK, to)    & pawns(WHITE));
+
+      if (from != SQ_NONE)
+          break;
+
+      // If we don't have any attacker we are finished
+      if ((attackers & pieces_of_color(us)) == EmptyBoardBB)
+          return 0;
+
+      // Locate the least valuable attacker to the destination square
+      // and use it to initialize from square.
+      PieceType pt;
+      for (pt = PAWN; !(attackers & pieces_of_color_and_type(us, pt)); pt++)
+          assert(pt < KING);
+
+      from = first_1(attackers & pieces_of_color_and_type(us, pt));
+      piece = piece_on(from);
+  }
+
+  // If the opponent has no attackers we are finished
   if ((attackers & pieces_of_color(them)) == EmptyBoardBB)
       return seeValues[capture];
 
@@ -1910,7 +1946,7 @@ Value Position::compute_non_pawn_material(Color c) const {
 /// side to move is checkmated. Note that this function is currently very
 /// slow, and shouldn't be used frequently inside the search.
 
-bool Position::is_mate() {
+bool Position::is_mate() const {
 
   if (is_check())
   {