]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Add some knowledge for KRPKB endgame
[stockfish] / src / endgame.cpp
index e45f9ed3fe34ddfb357882ffccddc8f89f75ff76..45a04815c42bd4dddf5fa4d23642f0f1193435be 100644 (file)
@@ -57,7 +57,7 @@ namespace {
 
   // Tables used to drive a piece towards or away from another piece
   const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
-  const int PushAway [8] = { 0, 10, 14, 20, 30, 42, 58, 80 };
+  const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
 
   // Get the material key of a Position out of the given endgame key code
   // like "KBPKN". The trick here is to first forge an ad-hoc fen string
@@ -102,6 +102,7 @@ Endgames::Endgames() {
   add<KNPK>("KNPK");
   add<KNPKB>("KNPKB");
   add<KRPKR>("KRPKR");
+  add<KRPKB>("KRPKB");
   add<KBPKB>("KBPKB");
   add<KBPKN>("KBPKN");
   add<KBPPKB>("KBPPKB");
@@ -365,6 +366,11 @@ Value Endgame<KQKR>::operator()(const Position& pos) const {
   return strongerSide == pos.side_to_move() ? result : -result;
 }
 
+
+/// KBB vs KN. This is almost always a win. We try to push enemy king to a corner
+/// and away from his knight. For a reference of this difficult endgame see:
+/// en.wikipedia.org/wiki/Chess_endgame#Effect_of_tablebases_on_endgame_theory
+
 template<>
 Value Endgame<KBBKN>::operator()(const Position& pos) const {
 
@@ -374,14 +380,14 @@ Value Endgame<KBBKN>::operator()(const Position& pos) const {
   assert(pos.count<KNIGHT>(weakerSide  ) == 1);
   assert(!pos.pieces(PAWN));
 
-  Square wksq = pos.king_square(strongerSide);
-  Square bksq = pos.king_square(weakerSide);
-  Square nsq = pos.list<KNIGHT>(weakerSide)[0];
+  Square winnerKSq = pos.king_square(strongerSide);
+  Square loserKSq = pos.king_square(weakerSide);
+  Square knightSq = pos.list<KNIGHT>(weakerSide)[0];
 
-  Value result =  BishopValueEg
-                + PushClose[square_distance(wksq, bksq)]
-                + square_distance(bksq, nsq) * 32
-                + (8 - popcount<Max15>(pos.attacks_from<KNIGHT>(nsq))) * 8;
+  Value result =  VALUE_KNOWN_WIN
+                + PushToCorners[loserKSq]
+                + PushClose[square_distance(winnerKSq, loserKSq)]
+                + PushAway[square_distance(loserKSq, knightSq)];
 
   return strongerSide == pos.side_to_move() ? result : -result;
 }
@@ -446,12 +452,26 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
       Square weakerKingSq = pos.king_square(weakerSide);
       Square bishopSq = pos.list<BISHOP>(strongerSide)[0];
 
-      // Draw if weaker pawn is on rank 7, bishop can't attack the pawn, and
-      // weaker king can stop opposing opponent's king from penetrating.
+      // Potential for a draw if our pawn is blocked on the 7th rank
+      // the bishop cannot attack it or they only have one pawn left
       if (   relative_rank(strongerSide, weakerPawnSq) == RANK_7
-          && opposite_colors(bishopSq, weakerPawnSq)
-          && square_distance(weakerPawnSq, weakerKingSq) <= square_distance(weakerPawnSq, strongerKingSq))
-          return SCALE_FACTOR_DRAW;
+          && (pos.pieces(strongerSide, PAWN) & (weakerPawnSq + pawn_push(weakerSide)))
+          && (opposite_colors(bishopSq, weakerPawnSq) || pos.count<PAWN>(strongerSide) == 1))
+      {
+          int strongerKingDist = square_distance(weakerPawnSq, strongerKingSq);
+          int weakerKingDist = square_distance(weakerPawnSq, weakerKingSq);
+
+          // Draw if the weak king is on it's back two ranks, within 2
+          // squares of the blocking pawn and the strong king is not
+          // closer. (I think this rule only fails in practically
+          // unreachable positions such as 5k1K/6p1/6P1/8/8/3B4/8/8 w
+          // and positions where qsearch will immediately correct the
+          // problem such as 8/4k1p1/6P1/1K6/3B4/8/8/8 w)
+          if (   relative_rank(strongerSide, weakerKingSq) >= RANK_7
+              && weakerKingDist <= 2
+              && weakerKingDist <= strongerKingDist)
+              return SCALE_FACTOR_DRAW;
+      }
   }
 
   return SCALE_FACTOR_NONE;
@@ -605,6 +625,51 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
   return SCALE_FACTOR_NONE;
 }
 
+template<>
+ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
+
+  assert(pos.non_pawn_material(strongerSide) == RookValueMg);
+  assert(pos.non_pawn_material(weakerSide) == BishopValueMg);
+  assert(pos.count<PAWN>(strongerSide) == 1);
+  assert(pos.count<PAWN>(weakerSide) == 0);
+
+  // Test for a rook pawn
+  if (pos.pieces(PAWN) & (FileABB | FileHBB))
+  {
+      Square ksq = pos.king_square(weakerSide);
+      Square bsq = pos.list<BISHOP>(weakerSide)[0];
+      Square psq = pos.list<PAWN>(strongerSide)[0];
+      Rank rk = relative_rank(strongerSide, psq);
+      Square push = pawn_push(strongerSide);
+
+      // If the pawn is on the 5th rank and the pawn (currently) is on
+      // the same color square as the bishop then there is a chance of
+      // a fortress. Depending on the king position give a moderate
+      // reduction or a stronger one if the defending king is near the
+      // corner but not trapped there.
+      if (rk == RANK_5 && !opposite_colors(bsq, psq))
+      {
+          int d = square_distance(psq + 3 * push, ksq);
+
+          if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongerSide) + 2 * push))
+              return ScaleFactor(24);
+          else
+              return ScaleFactor(48);
+      }
+
+      // When the pawn has moved to the 6th rank we can be fairly sure
+      // it's drawn if the bishop attacks the square in front of the
+      // pawn from a reasonable distance and the defending king is near
+      // the corner
+      if (   rk == RANK_6
+          && square_distance(psq + 2 * push, ksq) <= 1
+          && (PseudoAttacks[BISHOP][bsq] & (psq + push))
+          && file_distance(bsq, psq) >= 2)
+          return ScaleFactor(8);
+  }
+
+  return SCALE_FACTOR_NONE;
+}
 
 /// K, rook and two pawns vs K, rook and one pawn. There is only a single
 /// pattern: If the stronger side has no passed pawns and the defending king
@@ -622,8 +687,7 @@ ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
   Square bksq = pos.king_square(weakerSide);
 
   // Does the stronger side have a passed pawn?
-  if (   pos.pawn_is_passed(strongerSide, wpsq1)
-      || pos.pawn_is_passed(strongerSide, wpsq2))
+  if (pos.pawn_passed(strongerSide, wpsq1) || pos.pawn_passed(strongerSide, wpsq2))
       return SCALE_FACTOR_NONE;
 
   Rank r = std::max(relative_rank(strongerSide, wpsq1), relative_rank(strongerSide, wpsq2));