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 // UCIInputParser is a class for parsing UCI input. The class
50 // is actually a string stream built on a given input string.
52 typedef istringstream UCIInputParser;
54 // The root position. This is set up when the user (or in practice, the GUI)
55 // sends the "position" UCI command. The root position is sent to the think()
56 // function when the program receives the "go" command.
57 Position RootPosition(0);
60 bool handle_command(const string& command);
61 void set_option(UCIInputParser& uip);
62 void set_position(UCIInputParser& uip);
63 bool go(UCIInputParser& uip);
64 void perft(UCIInputParser& uip);
72 /// uci_main_loop() is the only global function in this file. It is
73 /// called immediately after the program has finished initializing.
74 /// The program remains in this loop until it receives the "quit" UCI
75 /// command. It waits for a command from the user, and passes this
76 /// command to handle_command and also intercepts EOF from stdin,
77 /// by translating EOF to the "quit" command. This ensures that Stockfish
78 /// exits gracefully if the GUI dies unexpectedly.
80 void uci_main_loop() {
82 RootPosition.from_fen(StartPositionFEN);
86 // Wait for a command from stdin
87 if (!getline(cin, command))
90 } while (handle_command(command));
100 // handle_command() takes a text string as input, uses a
101 // UCIInputParser object to parse this text string as a UCI command,
102 // and calls the appropriate functions. In addition to the UCI
103 // commands, the function also supports a few debug commands.
105 bool handle_command(const string& command) {
107 UCIInputParser uip(command);
110 if (!(uip >> token)) // operator>>() skips any whitespace
121 cout << "id name " << engine_name()
122 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
124 cout << "uciok" << endl;
126 else if (token == "ucinewgame")
128 push_button("New Game");
129 RootPosition.from_fen(StartPositionFEN);
131 else if (token == "isready")
132 cout << "readyok" << endl;
133 else if (token == "position")
135 else if (token == "setoption")
138 // The remaining commands are for debugging purposes only.
139 // Perhaps they should be removed later in order to reduce the
140 // size of the program binary.
141 else if (token == "d")
142 RootPosition.print();
143 else if (token == "flip")
145 Position p(RootPosition, RootPosition.thread());
146 RootPosition.flipped_copy(p);
148 else if (token == "eval")
151 cout << "Incremental mg: " << mg_value(RootPosition.value())
152 << "\nIncremental eg: " << eg_value(RootPosition.value())
153 << "\nFull eval: " << evaluate(RootPosition, ei) << endl;
155 else if (token == "key")
156 cout << "key: " << hex << RootPosition.get_key()
157 << "\nmaterial key: " << RootPosition.get_material_key()
158 << "\npawn key: " << RootPosition.get_pawn_key() << endl;
159 else if (token == "perft")
162 cout << "Unknown command: " << command << endl;
168 // set_position() is called when Stockfish receives the "position" UCI
169 // command. The input parameter is a UCIInputParser. It is assumed
170 // that this parser has consumed the first token of the UCI command
171 // ("position"), and is ready to read the second token ("startpos"
172 // or "fen", if the input is well-formed).
174 void set_position(UCIInputParser& uip) {
178 if (!(uip >> token)) // operator>>() skips any whitespace
181 if (token == "startpos")
182 RootPosition.from_fen(StartPositionFEN);
183 else if (token == "fen")
186 while (uip >> token && token != "moves")
191 RootPosition.from_fen(fen);
196 if (token != "moves")
199 if (token == "moves")
205 move = move_from_string(RootPosition, token);
206 RootPosition.do_move(move, st);
207 if (RootPosition.rule_50_counter() == 0)
208 RootPosition.reset_game_ply();
210 RootPosition.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
212 // Our StateInfo st is about going out of scope so copy
213 // its content inside RootPosition before it disappears.
214 RootPosition.detach();
220 // set_option() is called when Stockfish receives the "setoption" UCI
221 // command. The input parameter is a UCIInputParser. It is assumed
222 // that this parser has consumed the first token of the UCI command
223 // ("setoption"), and is ready to read the second token ("name", if
224 // the input is well-formed).
226 void set_option(UCIInputParser& uip) {
228 string token, name, value;
230 if (!(uip >> token)) // operator>>() skips any whitespace
233 if (token == "name" && uip >> name)
235 while (uip >> token && token != "value")
236 name += (" " + token);
238 if (token == "value" && uip >> value)
241 value += (" " + token);
243 set_option_value(name, value);
250 // go() is called when Stockfish receives the "go" UCI command. The
251 // input parameter is a UCIInputParser. It is assumed that this
252 // parser has consumed the first token of the UCI command ("go"),
253 // and is ready to read the second token. The function sets the
254 // thinking time and other parameters from the input string, and
255 // calls think() (defined in search.cpp) with the appropriate
256 // parameters. Returns false if a quit command is received while
257 // thinking, returns true otherwise.
259 bool go(UCIInputParser& uip) {
263 int time[2] = {0, 0}, inc[2] = {0, 0};
264 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
265 bool infinite = false, ponder = false;
266 Move searchMoves[500];
268 searchMoves[0] = MOVE_NONE;
272 if (token == "infinite")
274 else if (token == "ponder")
276 else if (token == "wtime")
278 else if (token == "btime")
280 else if (token == "winc")
282 else if (token == "binc")
284 else if (token == "movestogo")
286 else if (token == "depth")
288 else if (token == "nodes")
290 else if (token == "movetime")
292 else if (token == "searchmoves")
296 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
298 searchMoves[numOfMoves] = MOVE_NONE;
302 assert(RootPosition.is_ok());
304 return think(RootPosition, infinite, ponder, time, inc, movesToGo,
305 depth, nodes, moveTime, searchMoves);
308 void perft(UCIInputParser& uip) {
312 Position pos(RootPosition, RootPosition.thread());
317 tm = get_system_time();
319 n = perft(pos, depth * OnePly);
321 tm = get_system_time() - tm;
322 std::cout << "\nNodes " << n
323 << "\nTime (ms) " << tm
324 << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl;