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/>.
37 #include "ucioption.h"
42 //// Local definitions:
47 // FEN string for the initial position
48 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
50 // UCIInputParser is a class for parsing UCI input. The class
51 // is actually a string stream built on a given input string.
52 typedef istringstream UCIInputParser;
55 bool handle_command(Position& pos, const string& command);
56 void set_option(UCIInputParser& uip);
57 void set_position(Position& pos, UCIInputParser& uip);
58 bool go(Position& pos, UCIInputParser& uip);
59 void perft(Position& pos, UCIInputParser& uip);
67 /// uci_main_loop() is the only global function in this file. It is
68 /// called immediately after the program has finished initializing.
69 /// The program remains in this loop until it receives the "quit" UCI
70 /// command. It waits for a command from the user, and passes this
71 /// command to handle_command and also intercepts EOF from stdin,
72 /// by translating EOF to the "quit" command. This ensures that Stockfish
73 /// exits gracefully if the GUI dies unexpectedly.
75 void uci_main_loop() {
77 Position pos(StartPositionFEN, 0); // The root position
81 // Wait for a command from stdin
82 if (!getline(cin, command))
85 } while (handle_command(pos, command));
95 // handle_command() takes a text string as input, uses a
96 // UCIInputParser object to parse this text string as a UCI command,
97 // and calls the appropriate functions. In addition to the UCI
98 // commands, the function also supports a few debug commands.
100 bool handle_command(Position& pos, const string& command) {
102 UCIInputParser uip(command);
105 if (!(uip >> token)) // operator>>() skips any whitespace
116 cout << "id name " << engine_name()
117 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
119 cout << "uciok" << endl;
121 else if (token == "ucinewgame")
123 Options["New Game"].set_value("true");
124 pos.from_fen(StartPositionFEN);
126 else if (token == "isready")
127 cout << "readyok" << endl;
128 else if (token == "position")
129 set_position(pos, uip);
130 else if (token == "setoption")
133 // The remaining commands are for debugging purposes only.
134 // Perhaps they should be removed later in order to reduce the
135 // size of the program binary.
136 else if (token == "d")
138 else if (token == "flip")
140 Position p(pos, pos.thread());
143 else if (token == "eval")
146 cout << "Incremental mg: " << mg_value(pos.value())
147 << "\nIncremental eg: " << eg_value(pos.value())
148 << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
150 else if (token == "key")
151 cout << "key: " << hex << pos.get_key()
152 << "\nmaterial key: " << pos.get_material_key()
153 << "\npawn key: " << pos.get_pawn_key() << endl;
154 else if (token == "perft")
157 cout << "Unknown command: " << command << endl;
163 // set_position() is called when Stockfish receives the "position" UCI
164 // command. The input parameter is a UCIInputParser. It is assumed
165 // that this parser has consumed the first token of the UCI command
166 // ("position"), and is ready to read the second token ("startpos"
167 // or "fen", if the input is well-formed).
169 void set_position(Position& pos, UCIInputParser& uip) {
173 if (!(uip >> token)) // operator>>() skips any whitespace
176 if (token == "startpos")
177 pos.from_fen(StartPositionFEN);
178 else if (token == "fen")
181 while (uip >> token && token != "moves")
191 if (token != "moves")
194 if (token == "moves")
200 move = move_from_string(pos, token);
201 pos.do_move(move, st);
202 if (pos.rule_50_counter() == 0)
203 pos.reset_game_ply();
205 pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
207 // Our StateInfo st is about going out of scope so copy
208 // its content inside pos before it disappears.
215 // set_option() is called when Stockfish receives the "setoption" UCI
216 // command. The input parameter is a UCIInputParser. It is assumed
217 // that this parser has consumed the first token of the UCI command
218 // ("setoption"), and is ready to read the second token ("name", if
219 // the input is well-formed).
221 void set_option(UCIInputParser& uip) {
223 string token, name, value;
225 if (!(uip >> token)) // operator>>() skips any whitespace
228 if (token != "name" || !(uip >> name))
231 while (uip >> token && token != "value")
232 name += (" " + token);
234 if (Options.find(name) == Options.end())
236 cout << "No such option: " << name << endl;
240 if (token != "value" || !(uip >> value))
242 Options[name].set_value("true");
247 value += (" " + token);
249 Options[name].set_value(value);
253 // go() is called when Stockfish receives the "go" UCI command. The
254 // input parameter is a UCIInputParser. It is assumed that this
255 // parser has consumed the first token of the UCI command ("go"),
256 // and is ready to read the second token. The function sets the
257 // thinking time and other parameters from the input string, and
258 // calls think() (defined in search.cpp) with the appropriate
259 // parameters. Returns false if a quit command is received while
260 // thinking, returns true otherwise.
262 bool go(Position& pos, UCIInputParser& uip) {
266 int time[2] = {0, 0}, inc[2] = {0, 0};
267 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
268 bool infinite = false, ponder = false;
269 Move searchMoves[MOVES_MAX];
271 searchMoves[0] = MOVE_NONE;
275 if (token == "infinite")
277 else if (token == "ponder")
279 else if (token == "wtime")
281 else if (token == "btime")
283 else if (token == "winc")
285 else if (token == "binc")
287 else if (token == "movestogo")
289 else if (token == "depth")
291 else if (token == "nodes")
293 else if (token == "movetime")
295 else if (token == "searchmoves")
299 searchMoves[numOfMoves++] = move_from_string(pos, token);
301 searchMoves[numOfMoves] = MOVE_NONE;
307 return think(pos, infinite, ponder, time, inc, movesToGo,
308 depth, nodes, moveTime, searchMoves);
311 void perft(Position& pos, UCIInputParser& uip) {
319 tm = get_system_time();
321 n = perft(pos, depth * ONE_PLY);
323 tm = get_system_time() - tm;
324 std::cout << "\nNodes " << n
325 << "\nTime (ms) " << tm
326 << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;