]> git.sesse.net Git - stockfish/commitdiff
Handle UCI command "mate in x moves"
authorMarco Costalba <mcostalba@gmail.com>
Sun, 30 Dec 2012 10:40:20 +0000 (11:40 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 30 Dec 2012 13:43:23 +0000 (14:43 +0100)
Following a user request I added the handling of UCI:

go mate x

Currently we just return from a PV node if x moves have been
done. Probably not the best approach. I have looked at Fruit/Toga
sources and there is even simpler: engine falls back on a fixed
depth search.

No functional change.

src/search.cpp
src/search.h
src/uci.cpp

index 3ba0dbc905b9f6a75c4da3be45dca9b23a2d17f2..dcd4a5ed71c9a0da313343415a471205509238a0 100644 (file)
@@ -194,7 +194,7 @@ void Search::think() {
       goto finalize;
   }
 
-  if (Options["OwnBook"] && !Limits.infinite)
+  if (Options["OwnBook"] && !Limits.infinite && !Limits.mate)
   {
       Move bookMove = book.probe(RootPos, Options["Book File"], Options["Best Book Move"]);
 
@@ -410,6 +410,12 @@ namespace {
         if (depth > 2 && BestMoveChanges)
             bestMoveNeverChanged = false;
 
+        // Do we have found a "mate in x"?
+        if (   Limits.mate
+            && bestValue >= VALUE_MATE_IN_MAX_PLY
+            && VALUE_MATE - bestValue <= 2 * Limits.mate)
+            Signals.stop = true;
+
         // Do we have time for the next iteration? Can we stop searching now?
         if (Limits.use_time_management() && !Signals.stopOnPonderhit)
         {
@@ -602,6 +608,11 @@ namespace {
                  ss->staticEval, ss->evalMargin);
     }
 
+    // Handling of UCI command 'mate in x moves'. We simply return if after
+    // 'x' moves we still have not checkmated the opponent.
+    if (PvNode && !RootNode && !inCheck && Limits.mate && ss->ply > 2 * Limits.mate)
+        return eval;
+
     // Update gain for the parent non-capture move given the static position
     // evaluation before and after the move.
     if (   (move = (ss-1)->currentMove) != MOVE_NULL
index b7475577475246fa6892e309bde68afe6bd19097..600d7a75198500dfd4582f48e76c239ebb5b2a50 100644 (file)
@@ -80,9 +80,9 @@ struct RootMove {
 struct LimitsType {
 
   LimitsType() { memset(this, 0, sizeof(LimitsType)); }
-  bool use_time_management() const { return !(movetime | depth | nodes | infinite); }
+  bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); }
 
-  int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, infinite, ponder;
+  int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, mate, infinite, ponder;
 };
 
 
index b80390c9e61ca12ca178682968689e21054f09d6..65633c30b0ee041088569924f66a7b21466a54ea 100644 (file)
@@ -215,6 +215,7 @@ namespace {
         else if (token == "depth")     is >> limits.depth;
         else if (token == "nodes")     is >> limits.nodes;
         else if (token == "movetime")  is >> limits.movetime;
+        else if (token == "mate")      is >> limits.mate;
         else if (token == "infinite")  limits.infinite = true;
         else if (token == "ponder")    limits.ponder = true;
     }