]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Fix KXK endgame
[stockfish] / src / endgame.cpp
index c50bb08ae500e506e04c98cec6abbb65e0f21c15..4ee515ade71cab5c856f3f6c547d1fd959a8820f 100644 (file)
@@ -82,8 +82,7 @@ namespace {
 
   // Get the material key of Position out of the given endgame key code
   // like "KBPKN". The trick here is to first forge an ad-hoc FEN string
-  // and then let a Position object do the work for us. Note that the
-  // FEN string could correspond to an illegal position.
+  // and then let a Position object do the work for us.
   Key key(const string& code, Color c) {
 
     assert(code.length() > 0 && code.length() < 8);
@@ -94,8 +93,8 @@ namespace {
 
     std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
 
-    string fen =  sides[0] + char('0' + int(8 - code.length()))
-                + sides[1] + "/8/8/8/8/8/8/8 w - - 0 10";
+    string fen =  sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/"
+                + sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10";
 
     return Position(fen, false, NULL).material_key();
   }
@@ -167,6 +166,7 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
 
   if (   pos.count<QUEEN>(strongSide)
       || pos.count<ROOK>(strongSide)
+      ||(pos.count<BISHOP>(strongSide) && pos.count<KNIGHT>(strongSide))
       || pos.bishop_pair(strongSide))
       result += VALUE_KNOWN_WIN;
 
@@ -241,18 +241,18 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
   Square rsq  = relative_square(strongSide, pos.list<ROOK>(strongSide)[0]);
   Square psq  = relative_square(strongSide, pos.list<PAWN>(weakSide)[0]);
 
-  Square queeningSq = file_of(psq) | RANK_1;
+  Square queeningSq = make_square(file_of(psq), RANK_1);
   Value result;
 
   // If the stronger side's king is in front of the pawn, it's a win
   if (wksq < psq && file_of(wksq) == file_of(psq))
-      result = RookValueEg - Value(square_distance(wksq, psq));
+      result = RookValueEg - square_distance(wksq, psq);
 
   // If the weaker side's king is too far from the pawn and the rook,
   // it's a win.
   else if (   square_distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide)
            && square_distance(bksq, rsq) >= 3)
-      result = RookValueEg - Value(square_distance(wksq, psq));
+      result = RookValueEg - square_distance(wksq, psq);
 
   // If the pawn is far advanced and supported by the defending king,
   // the position is drawish
@@ -260,13 +260,12 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
            && square_distance(bksq, psq) == 1
            && rank_of(wksq) >= RANK_4
            && square_distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide))
-      result = Value(80 - square_distance(wksq, psq) * 8);
+      result = Value(80) - 8 * square_distance(wksq, psq);
 
   else
-      result =  Value(200)
-              - Value(square_distance(wksq, psq + DELTA_S) * 8)
-              + Value(square_distance(bksq, psq + DELTA_S) * 8)
-              + Value(square_distance(psq, queeningSq) * 8);
+      result =  Value(200) - 8 * (  square_distance(wksq, psq + DELTA_S)
+                                  - square_distance(bksq, psq + DELTA_S)
+                                  - square_distance(psq, queeningSq));
 
   return strongSide == pos.side_to_move() ? result : -result;
 }
@@ -365,52 +364,27 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
   // be detected even when the weaker side has some pawns.
 
   Bitboard pawns = pos.pieces(strongSide, PAWN);
-  File pawnFile = file_of(pos.list<PAWN>(strongSide)[0]);
+  File pawnsFile = file_of(lsb(pawns));
 
   // All pawns are on a single rook file ?
-  if (    (pawnFile == FILE_A || pawnFile == FILE_H)
-      && !(pawns & ~file_bb(pawnFile)))
+  if (    (pawnsFile == FILE_A || pawnsFile == FILE_H)
+      && !(pawns & ~file_bb(pawnsFile)))
   {
       Square bishopSq = pos.list<BISHOP>(strongSide)[0];
-      Square queeningSq = relative_square(strongSide, pawnFile | RANK_8);
+      Square queeningSq = relative_square(strongSide, make_square(pawnsFile, RANK_8));
       Square kingSq = pos.king_square(weakSide);
 
+      // If the bishop has the wrong color, and the defending king is on the file
+      // of the pawn(s) or the neighboring file, then it's potentially a draw.
       if (   opposite_colors(queeningSq, bishopSq)
-          && square_distance(queeningSq, kingSq) <= 1)
-          return SCALE_FACTOR_DRAW;
-  }
-
-  // If all the pawns are on the same B or G file, then it's potentially a draw
-  if (    (pawnFile == FILE_B || pawnFile == FILE_G)
-      && !(pos.pieces(PAWN) & ~file_bb(pawnFile))
-      && pos.non_pawn_material(weakSide) == 0
-      && pos.count<PAWN>(weakSide) >= 1)
-  {
-      // Get weakSide pawn that is closest to the home rank
-      Square weakPawnSq = backmost_sq(weakSide, pos.pieces(weakSide, PAWN));
-
-      Square strongKingSq = pos.king_square(strongSide);
-      Square weakKingSq = pos.king_square(weakSide);
-      Square bishopSq = pos.list<BISHOP>(strongSide)[0];
-
-      // There's 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(strongSide, weakPawnSq) == RANK_7
-          && (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
-          && (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(strongSide) == 1))
+          && file_distance(kingSq, lsb(pawns)) <= 1)
       {
-          int strongKingDist = square_distance(weakPawnSq, strongKingSq);
-          int weakKingDist = square_distance(weakPawnSq, weakKingSq);
-
-          // It's a draw if the weak king is on its 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(strongSide, weakKingSq) >= RANK_7
-              && weakKingDist <= 2
-              && weakKingDist <= strongKingDist)
+          // If the defending king has distance <= 1 to the promotion square or
+          // is placed somewhere in front of the frontmost pawn, it's a draw.
+          Rank rank = relative_rank(strongSide, (frontmost_sq(strongSide, pawns)));
+
+          if (   square_distance(kingSq, queeningSq) <= 1
+              || relative_rank(strongSide, kingSq) >= rank)
               return SCALE_FACTOR_DRAW;
       }
   }
@@ -464,7 +438,7 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
 
   File f = file_of(wpsq);
   Rank r = rank_of(wpsq);
-  Square queeningSq = f | RANK_8;
+  Square queeningSq = make_square(f, RANK_8);
   int tempo = (pos.side_to_move() == strongSide);
 
   // If the pawn is not too far advanced and the defending king defends the
@@ -722,12 +696,12 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
   if (relative_rank(strongSide, psq1) > relative_rank(strongSide, psq2))
   {
       blockSq1 = psq1 + pawn_push(strongSide);
-      blockSq2 = file_of(psq2) | rank_of(psq1);
+      blockSq2 = make_square(file_of(psq2), rank_of(psq1));
   }
   else
   {
       blockSq1 = psq2 + pawn_push(strongSide);
-      blockSq2 = file_of(psq1) | rank_of(psq2);
+      blockSq2 = make_square(file_of(psq1), rank_of(psq2));
   }
 
   switch (file_distance(psq1, psq2))