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-2010 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/>.
32 #include "ucioption.h"
38 // FEN string for the initial position
39 const char* StarFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
41 // Keep track of position keys along the setup moves (from start position to the
42 // position just before to start searching). This is needed by draw detection
43 // where, due to 50 moves rule, we need to check at most 100 plies back.
44 StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
46 void set_option(istringstream& up);
47 void set_position(Position& pos, istringstream& up);
48 void go(Position& pos, istringstream& up);
49 void perft(Position& pos, istringstream& up);
53 /// Wait for a command from the user, parse this text string as an UCI command,
54 /// and calls the appropriate functions. Also intercepts EOF from stdin to
55 /// ensure that we exit gracefully if the GUI dies unexpectedly. In addition to
56 /// the UCI commands, the function also supports a few debug commands.
60 Position pos(StarFEN, false, 0); // The root position
64 while (!quit && getline(cin, cmd))
66 istringstream is(cmd);
68 is >> skipws >> token;
70 if (cmd == "quit" || cmd == "stop")
72 quit = (token == "quit");
73 Search::Signals.stop = true;
74 Threads[0].wake_up(); // In case is waiting for stop or ponderhit
75 Threads.wait_end_of_search(); // Block here until search finishes
78 else if (cmd == "ponderhit")
80 // The opponent has played the expected move. GUI sends "ponderhit" if
81 // we were told to ponder on the same move the opponent has played. We
82 // should continue searching but switching from pondering to normal search.
83 Search::Limits.ponder = false; // FIXME racing
85 if (Search::Signals.stopOnPonderhit)
86 Search::Signals.stop = true;
88 Threads[0].wake_up(); // In case is waiting for stop or ponderhit
91 else if (token == "go")
94 else if (token == "ucinewgame")
95 pos.from_fen(StarFEN, false);
97 else if (token == "isready")
98 cout << "readyok" << endl;
100 else if (token == "position")
101 set_position(pos, is);
103 else if (token == "setoption")
106 else if (token == "perft")
109 else if (token == "d")
112 else if (token == "flip")
115 else if (token == "eval")
117 read_evaluation_uci_options(pos.side_to_move());
118 cout << trace_evaluate(pos) << endl;
121 else if (token == "key")
122 cout << "key: " << hex << pos.get_key()
123 << "\nmaterial key: " << pos.get_material_key()
124 << "\npawn key: " << pos.get_pawn_key() << endl;
126 else if (token == "uci")
127 cout << "id name " << engine_name()
128 << "\nid author " << engine_authors()
129 << "\n" << Options.print_all()
130 << "\nuciok" << endl;
132 cout << "Unknown command: " << cmd << endl;
139 // set_position() is called when engine receives the "position" UCI
140 // command. The function sets up the position described in the given
141 // fen string ("fen") or the starting position ("startpos") and then
142 // makes the moves given in the following move list ("moves").
144 void set_position(Position& pos, istringstream& is) {
151 if (token == "startpos")
154 is >> token; // Consume "moves" token if any
156 else if (token == "fen")
157 while (is >> token && token != "moves")
162 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
164 // Parse move list (if any)
165 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
167 pos.do_move(m, *SetupState);
169 // Increment pointer to StateRingBuf circular buffer
170 if (++SetupState - StateRingBuf >= 102)
171 SetupState = StateRingBuf;
176 // set_option() is called when engine receives the "setoption" UCI
177 // command. The function updates the corresponding UCI option ("name")
178 // to the given value ("value").
180 void set_option(istringstream& is) {
182 string token, name, value;
184 is >> token; // Consume "name" token
186 // Read option name (can contain spaces)
187 while (is >> token && token != "value")
188 name += string(" ", !name.empty()) + token;
190 // Read option value (can contain spaces)
192 value += string(" ", !value.empty()) + token;
194 if (Options.find(name) != Options.end())
195 Options[name].set_value(value.empty() ? "true" : value); // UCI buttons don't have "value"
197 cout << "No such option: " << name << endl;
201 // go() is called when engine receives the "go" UCI command. The
202 // function sets the thinking time and other parameters from the input
203 // string, and then calls think(). Returns false if a quit command
204 // is received while thinking, true otherwise.
206 void go(Position& pos, istringstream& is) {
209 int time[] = { 0, 0 }, inc[] = { 0, 0 };
211 memset(&Search::Limits, 0, sizeof(Search::Limits));
212 Search::RootMoves.clear();
213 Search::RootPosition = &pos;
217 if (token == "infinite")
218 Search::Limits.infinite = true;
219 else if (token == "ponder")
220 Search::Limits.ponder = true;
221 else if (token == "wtime")
223 else if (token == "btime")
225 else if (token == "winc")
227 else if (token == "binc")
229 else if (token == "movestogo")
230 is >> Search::Limits.movesToGo;
231 else if (token == "depth")
232 is >> Search::Limits.maxDepth;
233 else if (token == "nodes")
234 is >> Search::Limits.maxNodes;
235 else if (token == "movetime")
236 is >> Search::Limits.maxTime;
237 else if (token == "searchmoves")
239 Search::RootMoves.push_back(move_from_uci(pos, token));
242 Search::RootMoves.push_back(MOVE_NONE);
243 Search::Limits.time = time[pos.side_to_move()];
244 Search::Limits.increment = inc[pos.side_to_move()];
246 Threads.start_thinking();
250 // perft() is called when engine receives the "perft" command.
251 // The function calls perft() passing the required search depth
252 // then prints counted leaf nodes and elapsed time.
254 void perft(Position& pos, istringstream& is) {
262 time = get_system_time();
264 n = Search::perft(pos, depth * ONE_PLY);
266 time = get_system_time() - time;
268 std::cout << "\nNodes " << n
269 << "\nTime (ms) " << time
270 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;