From ce063f59cd0fea215309c719ee56cbf486a5ea80 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 30 Dec 2012 11:40:20 +0100 Subject: [PATCH] Handle UCI command "mate in x moves" 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 | 13 ++++++++++++- src/search.h | 4 ++-- src/uci.cpp | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 3ba0dbc9..dcd4a5ed 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -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 diff --git a/src/search.h b/src/search.h index b7475577..600d7a75 100644 --- a/src/search.h +++ b/src/search.h @@ -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; }; diff --git a/src/uci.cpp b/src/uci.cpp index b80390c9..65633c30 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -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; } -- 2.39.2