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/>.
39 #include "ucioption.h"
44 //// Local definitions:
49 // FEN string for the initial position
50 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
52 // UCIInputParser is a class for parsing UCI input. The class
53 // is actually a string stream built on a given input string.
54 typedef istringstream UCIInputParser;
57 bool handle_command(Position& pos, const string& command);
58 void set_option(UCIInputParser& uip);
59 void set_position(Position& pos, UCIInputParser& uip);
60 bool go(Position& pos, UCIInputParser& uip);
61 void perft(Position& pos, UCIInputParser& uip);
69 /// uci_main_loop() is the only global function in this file. It is
70 /// called immediately after the program has finished initializing.
71 /// The program remains in this loop until it receives the "quit" UCI
72 /// command. It waits for a command from the user, and passes this
73 /// command to handle_command and also intercepts EOF from stdin,
74 /// by translating EOF to the "quit" command. This ensures that Stockfish
75 /// exits gracefully if the GUI dies unexpectedly.
77 void uci_main_loop() {
79 Position pos(StartPositionFEN, 0); // The root position
83 // Wait for a command from stdin
84 if (!getline(cin, command))
87 } while (handle_command(pos, command));
97 // handle_command() takes a text string as input, uses a
98 // UCIInputParser object to parse this text string as a UCI command,
99 // and calls the appropriate functions. In addition to the UCI
100 // commands, the function also supports a few debug commands.
102 bool handle_command(Position& pos, const string& command) {
104 UCIInputParser uip(command);
107 if (!(uip >> token)) // operator>>() skips any whitespace
118 cout << "id name " << engine_name()
119 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
121 cout << "uciok" << endl;
123 else if (token == "ucinewgame")
125 push_button("New Game");
126 pos.from_fen(StartPositionFEN);
128 else if (token == "isready")
129 cout << "readyok" << endl;
130 else if (token == "position")
131 set_position(pos, uip);
132 else if (token == "setoption")
135 // The remaining commands are for debugging purposes only.
136 // Perhaps they should be removed later in order to reduce the
137 // size of the program binary.
138 else if (token == "d")
140 else if (token == "flip")
142 Position p(pos, pos.thread());
145 else if (token == "eval")
148 cout << "Incremental mg: " << mg_value(pos.value())
149 << "\nIncremental eg: " << eg_value(pos.value())
150 << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
152 else if (token == "key")
153 cout << "key: " << hex << pos.get_key()
154 << "\nmaterial key: " << pos.get_material_key()
155 << "\npawn key: " << pos.get_pawn_key() << endl;
156 else if (token == "perft")
159 cout << "Unknown command: " << command << endl;
165 // set_position() is called when Stockfish receives the "position" UCI
166 // command. The input parameter is a UCIInputParser. It is assumed
167 // that this parser has consumed the first token of the UCI command
168 // ("position"), and is ready to read the second token ("startpos"
169 // or "fen", if the input is well-formed).
171 void set_position(Position& pos, UCIInputParser& uip) {
175 if (!(uip >> token)) // operator>>() skips any whitespace
178 if (token == "startpos")
179 pos.from_fen(StartPositionFEN);
180 else if (token == "fen")
183 while (uip >> token && token != "moves")
193 if (token != "moves")
196 if (token == "moves")
202 move = move_from_string(pos, token);
203 pos.do_move(move, st);
204 if (pos.rule_50_counter() == 0)
205 pos.reset_game_ply();
207 pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
209 // Our StateInfo st is about going out of scope so copy
210 // its content inside pos before it disappears.
217 // set_option() is called when Stockfish receives the "setoption" UCI
218 // command. The input parameter is a UCIInputParser. It is assumed
219 // that this parser has consumed the first token of the UCI command
220 // ("setoption"), and is ready to read the second token ("name", if
221 // the input is well-formed).
223 void set_option(UCIInputParser& uip) {
225 string token, name, value;
227 if (!(uip >> token)) // operator>>() skips any whitespace
230 if (token == "name" && uip >> name)
232 while (uip >> token && token != "value")
233 name += (" " + token);
235 if (token == "value" && uip >> value)
238 value += (" " + token);
240 set_option_value(name, value);
247 // go() is called when Stockfish receives the "go" UCI command. The
248 // input parameter is a UCIInputParser. It is assumed that this
249 // parser has consumed the first token of the UCI command ("go"),
250 // and is ready to read the second token. The function sets the
251 // thinking time and other parameters from the input string, and
252 // calls think() (defined in search.cpp) with the appropriate
253 // parameters. Returns false if a quit command is received while
254 // thinking, returns true otherwise.
256 bool go(Position& pos, UCIInputParser& uip) {
260 int time[2] = {0, 0}, inc[2] = {0, 0};
261 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
262 bool infinite = false, ponder = false;
263 Move searchMoves[500];
265 searchMoves[0] = MOVE_NONE;
269 if (token == "infinite")
271 else if (token == "ponder")
273 else if (token == "wtime")
275 else if (token == "btime")
277 else if (token == "winc")
279 else if (token == "binc")
281 else if (token == "movestogo")
283 else if (token == "depth")
285 else if (token == "nodes")
287 else if (token == "movetime")
289 else if (token == "searchmoves")
293 searchMoves[numOfMoves++] = move_from_string(pos, token);
295 searchMoves[numOfMoves] = MOVE_NONE;
301 return think(pos, infinite, ponder, time, inc, movesToGo,
302 depth, nodes, moveTime, searchMoves);
305 void perft(Position& pos, UCIInputParser& uip) {
313 tm = get_system_time();
315 n = perft(pos, depth * ONE_PLY);
317 tm = get_system_time() - tm;
318 std::cout << "\nNodes " << n
319 << "\nTime (ms) " << tm
320 << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl;