1717 along with this program. If not, see <http://www.gnu.org/licenses/>.
1818*/
1919
20- #include < iomanip>
2120#include < iostream>
2221#include < sstream>
2322#include < string>
@@ -39,9 +38,9 @@ namespace {
3938 // FEN string of the initial position, normal chess
4039 const char * StartFEN = " rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" ;
4140
42- // Keep a track of the position keys along the setup moves (from the start position
43- // to the position just before the search starts). This is needed by the repetition
44- // draw detection code .
41+ // Stack to keep track of the position states along the setup moves (from the
42+ // start position to the position just before the search starts). Needed by
43+ // ' draw by repetition' detection .
4544 Search::StateStackPtr SetupStates;
4645
4746
@@ -105,7 +104,7 @@ namespace {
105104
106105
107106 // go() is called when engine receives the "go" UCI command. The function sets
108- // the thinking time and other parameters from the input string, and starts
107+ // the thinking time and other parameters from the input string, then starts
109108 // the search.
110109
111110 void go (const Position& pos, istringstream& is) {
@@ -114,7 +113,6 @@ namespace {
114113 string token;
115114
116115 while (is >> token)
117- {
118116 if (token == " searchmoves" )
119117 while (is >> token)
120118 limits.searchmoves .push_back (UCI::to_move (pos, token));
@@ -130,18 +128,18 @@ namespace {
130128 else if (token == " mate" ) is >> limits.mate ;
131129 else if (token == " infinite" ) limits.infinite = true ;
132130 else if (token == " ponder" ) limits.ponder = true ;
133- }
134131
135132 Threads.start_thinking (pos, limits, SetupStates);
136133 }
137134
138135} // namespace
139136
140137
141- // / Wait for a command from the user, parse this text string as an UCI command,
142- // / and call the appropriate functions. Also intercepts EOF from stdin to ensure
143- // / that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
144- // / commands, the function also supports a few debug commands.
138+ // / UCI::loop() waits for a command from stdin, parses it and calls the appropriate
139+ // / function. Also intercepts EOF from stdin to ensure gracefully exiting if the
140+ // / GUI dies unexpectedly. When called with some command line arguments, e.g. to
141+ // / run 'bench', once the command is executed the function returns immediately.
142+ // / In addition to the UCI ones, also some additional debug commands are supported.
145143
146144void UCI::loop (int argc, char * argv[]) {
147145
@@ -152,61 +150,56 @@ void UCI::loop(int argc, char* argv[]) {
152150 cmd += std::string (argv[i]) + " " ;
153151
154152 do {
155- if (argc == 1 && !getline (cin, cmd)) // Block here waiting for input
153+ if (argc == 1 && !getline (cin, cmd)) // Block here waiting for input or EOF
156154 cmd = " quit" ;
157155
158156 istringstream is (cmd);
159157
160158 token.clear (); // getline() could return empty or blank line
161159 is >> skipws >> token;
162160
163- if (token == " quit" || token == " stop" || token == " ponderhit" )
161+ // The GUI sends 'ponderhit' to tell us to ponder on the same move the
162+ // opponent has played. In case Signals.stopOnPonderhit is set we are
163+ // waiting for 'ponderhit' to stop the search (for instance because we
164+ // already ran out of time), otherwise we should continue searching but
165+ // switching from pondering to normal search.
166+ if ( token == " quit"
167+ || token == " stop"
168+ || (token == " ponderhit" && Search::Signals.stopOnPonderhit ))
164169 {
165- // The GUI sends 'ponderhit' to tell us to ponder on the same move the
166- // opponent has played. In case Signals.stopOnPonderhit is set we are
167- // waiting for 'ponderhit' to stop the search (for instance because we
168- // already ran out of time), otherwise we should continue searching but
169- // switch from pondering to normal search.
170- if (token != " ponderhit" || Search::Signals.stopOnPonderhit )
171- {
172- Search::Signals.stop = true ;
173- Threads.main ()->notify_one (); // Could be sleeping
174- }
175- else
176- Search::Limits.ponder = false ;
170+ Search::Signals.stop = true ;
171+ Threads.main ()->notify_one (); // Could be sleeping
177172 }
178- else if (token == " perft" )
179- {
180- int depth;
181- stringstream ss;
182-
183- is >> depth;
184- ss << Options[" Hash" ] << " "
185- << Options[" Threads" ] << " " << depth << " current " << token;
186-
187- benchmark (pos, ss);
188- }
189- else if (token == " key" )
190- sync_cout << hex << uppercase << setfill (' 0' )
191- << " position key: " << setw (16 ) << pos.key ()
192- << " \n material key: " << setw (16 ) << pos.material_key ()
193- << " \n pawn key: " << setw (16 ) << pos.pawn_key ()
194- << dec << nouppercase << setfill (' ' ) << sync_endl;
173+ else if (token == " ponderhit" )
174+ Search::Limits.ponder = false ; // Switch to normal search
195175
196176 else if (token == " uci" )
197177 sync_cout << " id name " << engine_info (true )
198178 << " \n " << Options
199179 << " \n uciok" << sync_endl;
200180
181+ else if (token == " isready" ) sync_cout << " readyok" << sync_endl;
201182 else if (token == " ucinewgame" ) TT.clear ();
202183 else if (token == " go" ) go (pos, is);
203184 else if (token == " position" ) position (pos, is);
204185 else if (token == " setoption" ) setoption (is);
186+
187+ // Additional custom non-UCI commands, useful for debugging
205188 else if (token == " flip" ) pos.flip ();
206189 else if (token == " bench" ) benchmark (pos, is);
207190 else if (token == " d" ) sync_cout << pos << sync_endl;
208- else if (token == " isready" ) sync_cout << " readyok" << sync_endl;
209191 else if (token == " eval" ) sync_cout << Eval::trace (pos) << sync_endl;
192+ else if (token == " perft" )
193+ {
194+ int depth;
195+ stringstream ss;
196+
197+ is >> depth;
198+ ss << Options[" Hash" ] << " "
199+ << Options[" Threads" ] << " " << depth << " current perft" ;
200+
201+ benchmark (pos, ss);
202+ }
210203 else
211204 sync_cout << " Unknown command: " << cmd << sync_endl;
212205
@@ -216,14 +209,14 @@ void UCI::loop(int argc, char* argv[]) {
216209}
217210
218211
219- // / Convert a Value to a string suitable for use with the UCI protocol
220- // / specifications :
212+ // / UCI::value() converts a Value to a string suitable for use with the UCI
213+ // / protocol specification :
221214// /
222- // / cp <x> The score from the engine's point of view in centipawns.
223- // / mate <y> Mate in y moves, not plies. If the engine is getting mated
224- // / use negative values for y.
215+ // / cp <x> The score from the engine's point of view in centipawns.
216+ // / mate <y> Mate in y moves, not plies. If the engine is getting mated
217+ // / use negative values for y.
225218
226- string UCI::value (Value v, Value alpha, Value beta ) {
219+ string UCI::value (Value v) {
227220
228221 stringstream ss;
229222
@@ -232,25 +225,23 @@ string UCI::value(Value v, Value alpha, Value beta) {
232225 else
233226 ss << " mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2 ;
234227
235- ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : " " );
236-
237228 return ss.str ();
238229}
239230
240231
241- // / Convert a Square to a string in algebraic notation (g1, a7, etc.)
232+ // / UCI::square() converts a Square to a string in algebraic notation (g1, a7, etc.)
242233
243234std::string UCI::square (Square s) {
244235
245- char sq[] = { char (' a' + file_of (s)), char (' 1' + rank_of (s)), 0 };
236+ char sq[] = { char (' a' + file_of (s)), char (' 1' + rank_of (s)), 0 }; // NULL terminated
246237 return sq;
247238}
248239
249240
250- // / Convert a Move to a string in pure coordinate notation (g1f3, a7a8q). The
251- // / only special case is castling moves , where we print in the e1g1 notation in
252- // / normal chess mode, and in e1h1 notation in chess960 mode. Internally
253- // / castling moves are always encoded as " king captures rook" .
241+ // / UCI::move() converts a Move to a string in coordinate notation (g1f3, a7a8q).
242+ // / The only special case is castling, where we print in the e1g1 notation in
243+ // / normal chess mode, and in e1h1 notation in chess960 mode. Internally all
244+ // / castling moves are always encoded as ' king captures rook' .
254245
255246string UCI::move (Move m, bool chess960) {
256247
@@ -275,8 +266,8 @@ string UCI::move(Move m, bool chess960) {
275266}
276267
277268
278- // / Convert a string representing a move in pure coordinate notation to the
279- // / corresponding legal Move, if any.
269+ // / UCI::to_move() converts a string representing a move in coordinate notation
270+ // / (g1f3, a7a8q) to the corresponding legal Move, if any.
280271
281272Move UCI::to_move (const Position& pos, string& str) {
282273
0 commit comments