2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "ucioption.h"
33 extern void benchmark(istringstream& is);
37 // FEN string of the initial position, normal chess
38 const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
40 // Keep track of position keys along the setup moves (from start position to the
41 // position just before to start searching). This is needed by draw detection
42 // where, due to 50 moves rule, we need to check at most 100 plies back.
43 StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
45 void set_option(istringstream& up);
46 void set_position(Position& pos, istringstream& up);
47 void go(Position& pos, istringstream& up);
48 void perft(Position& pos, istringstream& up);
52 /// Wait for a command from the user, parse this text string as an UCI command,
53 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
54 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
55 /// commands, the function also supports a few debug commands.
57 void uci_loop(const string& args) {
59 Position pos(StartFEN, false, Threads.main_thread()); // The root position
62 Search::RootPosition = pos;
64 while (token != "quit")
69 else if (!getline(cin, cmd)) // Block here waiting for input
72 istringstream is(cmd);
74 is >> skipws >> token;
76 if (token == "quit" || token == "stop")
78 Search::Signals.stop = true;
79 Threads.wait_for_search_finished(); // Cannot quit while threads are running
82 else if (token == "ponderhit")
84 // The opponent has played the expected move. GUI sends "ponderhit" if
85 // we were told to ponder on the same move the opponent has played. We
86 // should continue searching but switching from pondering to normal search.
87 Search::Limits.ponder = false;
89 if (Search::Signals.stopOnPonderhit)
91 Search::Signals.stop = true;
92 Threads.main_thread()->wake_up(); // Could be sleeping
96 else if (token == "go")
99 else if (token == "ucinewgame")
100 { /* Avoid returning "Unknown command" */ }
102 else if (token == "isready")
103 cout << "readyok" << endl;
105 else if (token == "position")
106 set_position(pos, is);
108 else if (token == "setoption")
111 else if (token == "perft")
114 else if (token == "d")
117 else if (token == "flip")
120 else if (token == "eval")
121 cout << Eval::trace(pos) << endl;
123 else if (token == "bench")
126 else if (token == "key")
127 cout << "key: " << hex << pos.key()
128 << "\nmaterial key: " << pos.material_key()
129 << "\npawn key: " << pos.pawn_key() << endl;
131 else if (token == "uci")
132 cout << "id name " << engine_info(true)
134 << "\nuciok" << endl;
136 cout << "Unknown command: " << cmd << endl;
138 if (!args.empty()) // Command line arguments have one-shot behaviour
140 Threads.wait_for_search_finished();
149 // set_position() is called when engine receives the "position" UCI
150 // command. The function sets up the position described in the given
151 // fen string ("fen") or the starting position ("startpos") and then
152 // makes the moves given in the following move list ("moves").
154 void set_position(Position& pos, istringstream& is) {
161 if (token == "startpos")
164 is >> token; // Consume "moves" token if any
166 else if (token == "fen")
167 while (is >> token && token != "moves")
172 pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
174 // Parse move list (if any)
175 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
177 pos.do_move(m, *SetupState);
179 // Increment pointer to StateRingBuf circular buffer
180 if (++SetupState - StateRingBuf >= 102)
181 SetupState = StateRingBuf;
186 // set_option() is called when engine receives the "setoption" UCI command. The
187 // function updates the UCI option ("name") to the given value ("value").
189 void set_option(istringstream& is) {
191 string token, name, value;
193 is >> token; // Consume "name" token
195 // Read option name (can contain spaces)
196 while (is >> token && token != "value")
197 name += string(" ", !name.empty()) + token;
199 // Read option value (can contain spaces)
201 value += string(" ", !value.empty()) + token;
203 if (Options.count(name))
204 Options[name] = value;
206 cout << "No such option: " << name << endl;
210 // go() is called when engine receives the "go" UCI command. The function sets
211 // the thinking time and other parameters from the input string, and then starts
214 void go(Position& pos, istringstream& is) {
216 Search::LimitsType limits;
217 vector<Move> searchMoves;
222 if (token == "wtime")
223 is >> limits.time[WHITE];
224 else if (token == "btime")
225 is >> limits.time[BLACK];
226 else if (token == "winc")
227 is >> limits.inc[WHITE];
228 else if (token == "binc")
229 is >> limits.inc[BLACK];
230 else if (token == "movestogo")
231 is >> limits.movestogo;
232 else if (token == "depth")
234 else if (token == "nodes")
236 else if (token == "movetime")
237 is >> limits.movetime;
238 else if (token == "infinite")
239 limits.infinite = true;
240 else if (token == "ponder")
241 limits.ponder = true;
242 else if (token == "searchmoves")
244 searchMoves.push_back(move_from_uci(pos, token));
247 Threads.start_searching(pos, limits, searchMoves);
251 // perft() is called when engine receives the "perft" command. The function
252 // calls perft() with the required search depth then prints counted leaf nodes
255 void perft(Position& pos, istringstream& is) {
262 Time time = Time::current_time();
264 int64_t n = Search::perft(pos, depth * ONE_PLY);
266 int e = time.elapsed();
268 cout << "\nNodes " << n
269 << "\nTime (ms) " << e
270 << "\nNodes/second " << int(n / (e / 1000.0)) << endl;