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/>.
38 #include "ucioption.h"
43 //// Local definitions:
48 // FEN string for the initial position
49 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
51 // UCIInputParser is a class for parsing UCI input. The class
52 // is actually a string stream built on a given input string.
53 typedef istringstream UCIInputParser;
56 bool handle_command(Position& pos, const string& command);
57 void set_option(UCIInputParser& uip);
58 void set_position(Position& pos, UCIInputParser& uip);
59 bool go(Position& pos, UCIInputParser& uip);
60 void perft(Position& pos, UCIInputParser& uip);
68 /// uci_main_loop() is the only global function in this file. It is
69 /// called immediately after the program has finished initializing.
70 /// The program remains in this loop until it receives the "quit" UCI
71 /// command. It waits for a command from the user, and passes this
72 /// command to handle_command and also intercepts EOF from stdin,
73 /// by translating EOF to the "quit" command. This ensures that Stockfish
74 /// exits gracefully if the GUI dies unexpectedly.
76 void uci_main_loop() {
78 Position pos(StartPositionFEN, 0); // The root position
82 // Wait for a command from stdin
83 if (!getline(cin, command))
86 } while (handle_command(pos, command));
96 // handle_command() takes a text string as input, uses a
97 // UCIInputParser object to parse this text string as a UCI command,
98 // and calls the appropriate functions. In addition to the UCI
99 // commands, the function also supports a few debug commands.
101 bool handle_command(Position& pos, const string& command) {
103 UCIInputParser uip(command);
106 if (!(uip >> token)) // operator>>() skips any whitespace
117 cout << "id name " << engine_name()
118 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
120 cout << "uciok" << endl;
122 else if (token == "ucinewgame")
124 Options["New Game"].set_value("true");
125 pos.from_fen(StartPositionFEN);
127 else if (token == "isready")
128 cout << "readyok" << endl;
129 else if (token == "position")
130 set_position(pos, uip);
131 else if (token == "setoption")
134 // The remaining commands are for debugging purposes only.
135 // Perhaps they should be removed later in order to reduce the
136 // size of the program binary.
137 else if (token == "d")
139 else if (token == "flip")
141 Position p(pos, pos.thread());
144 else if (token == "eval")
147 cout << "Incremental mg: " << mg_value(pos.value())
148 << "\nIncremental eg: " << eg_value(pos.value())
149 << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
151 else if (token == "key")
152 cout << "key: " << hex << pos.get_key()
153 << "\nmaterial key: " << pos.get_material_key()
154 << "\npawn key: " << pos.get_pawn_key() << endl;
155 else if (token == "perft")
158 cout << "Unknown command: " << command << endl;
164 // set_position() is called when Stockfish receives the "position" UCI
165 // command. The input parameter is a UCIInputParser. It is assumed
166 // that this parser has consumed the first token of the UCI command
167 // ("position"), and is ready to read the second token ("startpos"
168 // or "fen", if the input is well-formed).
170 void set_position(Position& pos, UCIInputParser& uip) {
174 if (!(uip >> token)) // operator>>() skips any whitespace
177 if (token == "startpos")
178 pos.from_fen(StartPositionFEN);
179 else if (token == "fen")
182 while (uip >> token && token != "moves")
192 if (token != "moves")
195 if (token == "moves")
201 move = move_from_string(pos, token);
202 pos.do_move(move, st);
203 if (pos.rule_50_counter() == 0)
204 pos.reset_game_ply();
206 pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
208 // Our StateInfo st is about going out of scope so copy
209 // its content inside pos before it disappears.
216 // set_option() is called when Stockfish receives the "setoption" UCI
217 // command. The input parameter is a UCIInputParser. It is assumed
218 // that this parser has consumed the first token of the UCI command
219 // ("setoption"), and is ready to read the second token ("name", if
220 // the input is well-formed).
222 void set_option(UCIInputParser& uip) {
224 string token, name, value;
226 if (!(uip >> token)) // operator>>() skips any whitespace
229 if (token != "name" || !(uip >> name))
232 while (uip >> token && token != "value")
233 name += (" " + token);
235 if (Options.find(name) == Options.end())
237 cout << "No such option: " << name << endl;
241 if (token != "value" || !(uip >> value))
243 Options[name].set_value("true");
248 value += (" " + token);
250 Options[name].set_value(value);
254 // go() is called when Stockfish receives the "go" UCI command. The
255 // input parameter is a UCIInputParser. It is assumed that this
256 // parser has consumed the first token of the UCI command ("go"),
257 // and is ready to read the second token. The function sets the
258 // thinking time and other parameters from the input string, and
259 // calls think() (defined in search.cpp) with the appropriate
260 // parameters. Returns false if a quit command is received while
261 // thinking, returns true otherwise.
263 bool go(Position& pos, UCIInputParser& uip) {
267 int time[2] = {0, 0}, inc[2] = {0, 0};
268 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
269 bool infinite = false, ponder = false;
270 Move searchMoves[MOVES_MAX];
272 searchMoves[0] = MOVE_NONE;
276 if (token == "infinite")
278 else if (token == "ponder")
280 else if (token == "wtime")
282 else if (token == "btime")
284 else if (token == "winc")
286 else if (token == "binc")
288 else if (token == "movestogo")
290 else if (token == "depth")
292 else if (token == "nodes")
294 else if (token == "movetime")
296 else if (token == "searchmoves")
300 searchMoves[numOfMoves++] = move_from_string(pos, token);
302 searchMoves[numOfMoves] = MOVE_NONE;
308 return think(pos, infinite, ponder, time, inc, movesToGo,
309 depth, nodes, moveTime, searchMoves);
312 void perft(Position& pos, UCIInputParser& uip) {
320 tm = get_system_time();
322 n = perft(pos, depth * ONE_PLY);
324 tm = get_system_time() - tm;
325 std::cout << "\nNodes " << n
326 << "\nTime (ms) " << tm
327 << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;