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
77 else if (cmd == "ponderhit")
79 // The opponent has played the expected move. GUI sends "ponderhit" if
80 // we were told to ponder on the same move the opponent has played. We
81 // should continue searching but switching from pondering to normal search.
82 Search::Limits.ponder = false; // FIXME racing
84 if (Search::Signals.stopOnPonderhit)
85 Search::Signals.stop = true;
87 Threads[0].wake_up(); // In case is waiting for stop or ponderhit
90 else if (token == "go")
93 else if (token == "ucinewgame")
94 pos.from_fen(StarFEN, false);
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 == "perft")
108 else if (token == "d")
111 else if (token == "flip")
114 else if (token == "eval")
116 read_evaluation_uci_options(pos.side_to_move());
117 cout << trace_evaluate(pos) << endl;
120 else if (token == "key")
121 cout << "key: " << hex << pos.get_key()
122 << "\nmaterial key: " << pos.get_material_key()
123 << "\npawn key: " << pos.get_pawn_key() << endl;
125 else if (token == "uci")
126 cout << "id name " << engine_name()
127 << "\nid author " << engine_authors()
128 << "\n" << Options.print_all()
129 << "\nuciok" << endl;
131 cout << "Unknown command: " << cmd << endl;
138 // set_position() is called when engine receives the "position" UCI
139 // command. The function sets up the position described in the given
140 // fen string ("fen") or the starting position ("startpos") and then
141 // makes the moves given in the following move list ("moves").
143 void set_position(Position& pos, istringstream& is) {
150 if (token == "startpos")
153 is >> token; // Consume "moves" token if any
155 else if (token == "fen")
156 while (is >> token && token != "moves")
161 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
163 // Parse move list (if any)
164 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
166 pos.do_move(m, *SetupState);
168 // Increment pointer to StateRingBuf circular buffer
169 if (++SetupState - StateRingBuf >= 102)
170 SetupState = StateRingBuf;
175 // set_option() is called when engine receives the "setoption" UCI
176 // command. The function updates the corresponding UCI option ("name")
177 // to the given value ("value").
179 void set_option(istringstream& is) {
181 string token, name, value;
183 is >> token; // Consume "name" token
185 // Read option name (can contain spaces)
186 while (is >> token && token != "value")
187 name += string(" ", !name.empty()) + token;
189 // Read option value (can contain spaces)
191 value += string(" ", !value.empty()) + token;
193 if (Options.find(name) != Options.end())
194 Options[name].set_value(value.empty() ? "true" : value); // UCI buttons don't have "value"
196 cout << "No such option: " << name << endl;
200 // go() is called when engine receives the "go" UCI command. The
201 // function sets the thinking time and other parameters from the input
202 // string, and then calls think(). Returns false if a quit command
203 // is received while thinking, true otherwise.
205 void go(Position& pos, istringstream& is) {
208 int time[] = { 0, 0 }, inc[] = { 0, 0 };
210 memset(&Search::Limits, 0, sizeof(Search::Limits));
211 Search::RootMoves.clear();
212 Search::RootPosition = &pos;
216 if (token == "infinite")
217 Search::Limits.infinite = true;
218 else if (token == "ponder")
219 Search::Limits.ponder = true;
220 else if (token == "wtime")
222 else if (token == "btime")
224 else if (token == "winc")
226 else if (token == "binc")
228 else if (token == "movestogo")
229 is >> Search::Limits.movesToGo;
230 else if (token == "depth")
231 is >> Search::Limits.maxDepth;
232 else if (token == "nodes")
233 is >> Search::Limits.maxNodes;
234 else if (token == "movetime")
235 is >> Search::Limits.maxTime;
236 else if (token == "searchmoves")
238 Search::RootMoves.push_back(move_from_uci(pos, token));
241 Search::RootMoves.push_back(MOVE_NONE);
242 Search::Limits.time = time[pos.side_to_move()];
243 Search::Limits.increment = inc[pos.side_to_move()];
245 Threads.start_thinking();
249 // perft() is called when engine receives the "perft" command.
250 // The function calls perft() passing the required search depth
251 // then prints counted leaf nodes and elapsed time.
253 void perft(Position& pos, istringstream& is) {
261 time = get_system_time();
263 n = Search::perft(pos, depth * ONE_PLY);
265 time = get_system_time() - time;
267 std::cout << "\nNodes " << n
268 << "\nTime (ms) " << time
269 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;