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/>.
31 #include "ucioption.h"
35 extern void benchmark(const Position& pos, istream& is);
39 // FEN string of the initial position, normal chess
40 const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
42 void set_option(istringstream& up);
43 void set_position(Position& pos, istringstream& up);
44 void go(Position& pos, istringstream& up);
48 /// Wait for a command from the user, parse this text string as an UCI command,
49 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
50 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
51 /// commands, the function also supports a few debug commands.
53 void UCI::loop(const string& args) {
55 Position pos(StartFEN, false, Threads.main_thread()); // The root position
58 while (token != "quit")
63 else if (!getline(cin, cmd)) // Block here waiting for input
66 istringstream is(cmd);
68 is >> skipws >> token;
70 if (token == "quit" || token == "stop")
72 Search::Signals.stop = true;
73 Threads.wait_for_search_finished(); // Cannot quit while threads are running
76 else if (token == "ponderhit")
78 // The opponent has played the expected move. GUI sends "ponderhit" if
79 // we were told to ponder on the same move the opponent has played. We
80 // should continue searching but switching from pondering to normal search.
81 Search::Limits.ponder = false;
83 if (Search::Signals.stopOnPonderhit)
85 Search::Signals.stop = true;
86 Threads.main_thread()->wake_up(); // Could be sleeping
90 else if (token == "go")
93 else if (token == "ucinewgame")
96 else if (token == "isready")
97 cout << "readyok" << endl;
99 else if (token == "position")
100 set_position(pos, is);
102 else if (token == "setoption")
105 else if (token == "d")
108 else if (token == "flip")
111 else if (token == "eval")
112 cout << Eval::trace(pos) << endl;
114 else if (token == "bench")
117 else if (token == "key")
118 cout << "key: " << hex << pos.key()
119 << "\nmaterial key: " << pos.material_key()
120 << "\npawn key: " << pos.pawn_key() << endl;
122 else if (token == "uci")
123 cout << "id name " << engine_info(true)
125 << "\nuciok" << endl;
127 else if (token == "perft" && (is >> token)) // Read depth
131 ss << Options["Hash"] << " "
132 << Options["Threads"] << " " << token << " current perft";
138 cout << "Unknown command: " << cmd << endl;
140 if (!args.empty()) // Command line arguments have one-shot behaviour
142 Threads.wait_for_search_finished();
151 // set_position() is called when engine receives the "position" UCI command.
152 // The function sets up the position described in the given fen string ("fen")
153 // or the starting position ("startpos") and then makes the moves given in the
154 // following move list ("moves").
156 void set_position(Position& pos, istringstream& is) {
158 // Keep track of position keys along the setup moves (from start position to the
159 // position just before to start searching). Needed by repetition draw detection.
160 static std::deque<StateInfo> st;
167 if (token == "startpos")
170 is >> token; // Consume "moves" token if any
172 else if (token == "fen")
173 while (is >> token && token != "moves")
178 pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
181 // Parse move list (if any)
182 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
184 st.push_back(StateInfo());
185 pos.do_move(m, st.back());
190 // set_option() is called when engine receives the "setoption" UCI command. The
191 // function updates the UCI option ("name") to the given value ("value").
193 void set_option(istringstream& is) {
195 string token, name, value;
197 is >> token; // Consume "name" token
199 // Read option name (can contain spaces)
200 while (is >> token && token != "value")
201 name += string(" ", !name.empty()) + token;
203 // Read option value (can contain spaces)
205 value += string(" ", !value.empty()) + token;
207 if (Options.count(name))
208 Options[name] = value;
210 cout << "No such option: " << name << endl;
214 // go() is called when engine receives the "go" UCI command. The function sets
215 // the thinking time and other parameters from the input string, and then starts
218 void go(Position& pos, istringstream& is) {
220 Search::LimitsType limits;
221 vector<Move> searchMoves;
226 if (token == "wtime")
227 is >> limits.time[WHITE];
228 else if (token == "btime")
229 is >> limits.time[BLACK];
230 else if (token == "winc")
231 is >> limits.inc[WHITE];
232 else if (token == "binc")
233 is >> limits.inc[BLACK];
234 else if (token == "movestogo")
235 is >> limits.movestogo;
236 else if (token == "depth")
238 else if (token == "nodes")
240 else if (token == "movetime")
241 is >> limits.movetime;
242 else if (token == "infinite")
243 limits.infinite = true;
244 else if (token == "ponder")
245 limits.ponder = true;
246 else if (token == "searchmoves")
248 searchMoves.push_back(move_from_uci(pos, token));
251 Threads.start_searching(pos, limits, searchMoves);