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(const Position& pos, istream& 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);
51 /// Wait for a command from the user, parse this text string as an UCI command,
52 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
53 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
54 /// commands, the function also supports a few debug commands.
56 void uci_loop(const string& args) {
58 Position pos(StartFEN, false, Threads.main_thread()); // The root position
61 while (token != "quit")
66 else if (!getline(cin, cmd)) // Block here waiting for input
69 istringstream is(cmd);
71 is >> skipws >> token;
73 if (token == "quit" || token == "stop")
75 Search::Signals.stop = true;
76 Threads.wait_for_search_finished(); // Cannot quit while threads are running
79 else if (token == "ponderhit")
81 // The opponent has played the expected move. GUI sends "ponderhit" if
82 // we were told to ponder on the same move the opponent has played. We
83 // should continue searching but switching from pondering to normal search.
84 Search::Limits.ponder = false;
86 if (Search::Signals.stopOnPonderhit)
88 Search::Signals.stop = true;
89 Threads.main_thread()->wake_up(); // Could be sleeping
93 else if (token == "go")
96 else if (token == "ucinewgame")
99 else if (token == "isready")
100 cout << "readyok" << endl;
102 else if (token == "position")
103 set_position(pos, is);
105 else if (token == "setoption")
108 else if (token == "d")
111 else if (token == "flip")
114 else if (token == "eval")
115 cout << Eval::trace(pos) << endl;
117 else if (token == "bench")
120 else if (token == "key")
121 cout << "key: " << hex << pos.key()
122 << "\nmaterial key: " << pos.material_key()
123 << "\npawn key: " << pos.pawn_key() << endl;
125 else if (token == "uci")
126 cout << "id name " << engine_info(true)
128 << "\nuciok" << endl;
130 else if (token == "perft" && (is >> token)) // Read depth
134 ss << Options["Hash"] << " "
135 << Options["Threads"] << " " << token << " current perft";
141 cout << "Unknown command: " << cmd << endl;
143 if (!args.empty()) // Command line arguments have one-shot behaviour
145 Threads.wait_for_search_finished();
154 // set_position() is called when engine receives the "position" UCI
155 // command. The function sets up the position described in the given
156 // fen string ("fen") or the starting position ("startpos") and then
157 // makes the moves given in the following move list ("moves").
159 void set_position(Position& pos, istringstream& is) {
166 if (token == "startpos")
169 is >> token; // Consume "moves" token if any
171 else if (token == "fen")
172 while (is >> token && token != "moves")
177 pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
179 // Parse move list (if any)
180 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
182 pos.do_move(m, *SetupState);
184 // Increment pointer to StateRingBuf circular buffer
185 if (++SetupState - StateRingBuf >= 102)
186 SetupState = StateRingBuf;
191 // set_option() is called when engine receives the "setoption" UCI command. The
192 // function updates the UCI option ("name") to the given value ("value").
194 void set_option(istringstream& is) {
196 string token, name, value;
198 is >> token; // Consume "name" token
200 // Read option name (can contain spaces)
201 while (is >> token && token != "value")
202 name += string(" ", !name.empty()) + token;
204 // Read option value (can contain spaces)
206 value += string(" ", !value.empty()) + token;
208 if (Options.count(name))
209 Options[name] = value;
211 cout << "No such option: " << name << endl;
215 // go() is called when engine receives the "go" UCI command. The function sets
216 // the thinking time and other parameters from the input string, and then starts
219 void go(Position& pos, istringstream& is) {
221 Search::LimitsType limits;
222 vector<Move> searchMoves;
227 if (token == "wtime")
228 is >> limits.time[WHITE];
229 else if (token == "btime")
230 is >> limits.time[BLACK];
231 else if (token == "winc")
232 is >> limits.inc[WHITE];
233 else if (token == "binc")
234 is >> limits.inc[BLACK];
235 else if (token == "movestogo")
236 is >> limits.movestogo;
237 else if (token == "depth")
239 else if (token == "nodes")
241 else if (token == "movetime")
242 is >> limits.movetime;
243 else if (token == "infinite")
244 limits.infinite = true;
245 else if (token == "ponder")
246 limits.ponder = true;
247 else if (token == "searchmoves")
249 searchMoves.push_back(move_from_uci(pos, token));
252 Threads.start_searching(pos, limits, searchMoves);