]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Detect stalemate in KXK endgames
[stockfish] / src / endgame.cpp
index 00c707267575d816c5c0d12750768b9fa9610038..d7cb2b3c8c58541a54d0e662aff2d25110a1e07f 100644 (file)
@@ -22,7 +22,6 @@
 
 #include "bitcount.h"
 #include "endgame.h"
-#include "pawns.h"
 
 using std::string;
 
@@ -83,6 +82,21 @@ namespace {
   template<typename M>
   void delete_endgame(const typename M::value_type& p) { delete p.second; }
 
+  // Fast stalemate detection with lone king
+  bool is_kxk_stalemate(const Position &pos, const Color c) {
+    if ( pos.side_to_move() == c &&
+        !pos.in_check()) {
+      const Square from = pos.king_square(c);
+      Bitboard b = pos.attacks_from<KING>(from);
+      while (b) {
+        // Assume there are no pinned pieces, as it is a lone king
+        if (pos.pl_move_is_legal(make_move(from, pop_1st_bit(&b)), 0))
+          return false;
+      }
+      return true;
+    }
+    return false;
+  }
 } // namespace
 
 
@@ -133,6 +147,10 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
   assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO);
 
+  if (is_kxk_stalemate(pos, weakerSide)) {
+    return VALUE_DRAW;
+  }
+  
   Square winnerKSq = pos.king_square(strongerSide);
   Square loserKSq = pos.king_square(weakerSide);
 
@@ -143,9 +161,9 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
 
   if (   pos.piece_count(strongerSide, QUEEN)
       || pos.piece_count(strongerSide, ROOK)
-      || pos.piece_count(strongerSide, BISHOP) > 1)
-      // TODO: check for two equal-colored bishops!
-      result += VALUE_KNOWN_WIN;
+      || pos.both_color_bishops(strongerSide)) {
+    result += VALUE_KNOWN_WIN;
+  }
 
   return strongerSide == pos.side_to_move() ? result : -result;
 }
@@ -416,7 +434,7 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
           && abs(file_of(kingSq) - pawnFile) <= 1)
       {
           // The bishop has the wrong color, and the defending king is on the
-          // file of the pawn(s) or the neighboring file. Find the rank of the
+          // file of the pawn(s) or the adjacent file. Find the rank of the
           // frontmost pawn.
           Rank rank;
           if (strongerSide == WHITE)
@@ -768,7 +786,7 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
         return SCALE_FACTOR_NONE;
 
   case 1:
-    // Pawns on neighboring files. Draw if defender firmly controls the square
+    // Pawns on adjacent files. Draw if defender firmly controls the square
     // in front of the frontmost pawn's path, and the square diagonally behind
     // this square on the file of the other pawn.
     if (   ksq == blockSq1