]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Fully correct stealmate detection
[stockfish] / src / evaluate.cpp
index be53a73ee886633bc7775c3453856a50531e78e6..34a87771aea96a120e14f5858ffebd510f73d60f 100644 (file)
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <algorithm>
 #include <cassert>
 #include <iomanip>
 #include <sstream>
-#include <algorithm>
 
 #include "bitcount.h"
 #include "evaluate.h"
@@ -787,11 +787,18 @@ namespace {
              sf = ScaleFactor(50 * sf / SCALE_FACTOR_NORMAL);
     }
 
+    // Stealmate detection
+    Color stm = pos.side_to_move();
+    if (   (ei.attackedBy[stm][ALL_PIECES] == ei.attackedBy[stm][KING])
+        && (!(ei.attackedBy[stm][KING] & ~ei.attackedBy[~stm][ALL_PIECES]))
+        && !MoveList<LEGAL>(pos).size())
+        sf = SCALE_FACTOR_DRAW;
+
     // Interpolate between a middlegame and a (scaled by 'sf') endgame score
     Value v =  mg_value(score) * int(ei.mi->game_phase())
              + eg_value(score) * int(PHASE_MIDGAME - ei.mi->game_phase()) * sf / SCALE_FACTOR_NORMAL;
 
-    v /= PHASE_MIDGAME;
+    v /= int(PHASE_MIDGAME);
 
     // In case of tracing add all single evaluation contributions for both white and black
     if (Trace)