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.
55 typedef istringstream UCIInputParser;
57 // The root position. This is set up when the user (or in practice, the GUI)
58 // sends the "position" UCI command. The root position is sent to the think()
59 // function when the program receives the "go" command.
60 Position RootPosition(StartPositionFEN, 0);
63 bool handle_command(const string& command);
64 void set_option(UCIInputParser& uip);
65 void set_position(UCIInputParser& uip);
66 bool go(UCIInputParser& uip);
67 void perft(UCIInputParser& uip);
75 /// uci_main_loop() is the only global function in this file. It is
76 /// called immediately after the program has finished initializing.
77 /// The program remains in this loop until it receives the "quit" UCI
78 /// command. It waits for a command from the user, and passes this
79 /// command to handle_command and also intercepts EOF from stdin,
80 /// by translating EOF to the "quit" command. This ensures that Stockfish
81 /// exits gracefully if the GUI dies unexpectedly.
83 void uci_main_loop() {
88 // Wait for a command from stdin
89 if (!getline(cin, command))
92 } while (handle_command(command));
102 // handle_command() takes a text string as input, uses a
103 // UCIInputParser object to parse this text string as a UCI command,
104 // and calls the appropriate functions. In addition to the UCI
105 // commands, the function also supports a few debug commands.
107 bool handle_command(const string& command) {
109 UCIInputParser uip(command);
112 if (!(uip >> token)) // operator>>() skips any whitespace
123 cout << "id name " << engine_name()
124 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
126 cout << "uciok" << endl;
128 else if (token == "ucinewgame")
130 push_button("New Game");
131 RootPosition.from_fen(StartPositionFEN);
133 else if (token == "isready")
134 cout << "readyok" << endl;
135 else if (token == "position")
137 else if (token == "setoption")
140 // The remaining commands are for debugging purposes only.
141 // Perhaps they should be removed later in order to reduce the
142 // size of the program binary.
143 else if (token == "d")
144 RootPosition.print();
145 else if (token == "flip")
147 Position p(RootPosition, RootPosition.thread());
148 RootPosition.flipped_copy(p);
150 else if (token == "eval")
153 cout << "Incremental mg: " << mg_value(RootPosition.value())
154 << "\nIncremental eg: " << eg_value(RootPosition.value())
155 << "\nFull eval: " << evaluate(RootPosition, evalMargin) << endl;
157 else if (token == "key")
158 cout << "key: " << hex << RootPosition.get_key()
159 << "\nmaterial key: " << RootPosition.get_material_key()
160 << "\npawn key: " << RootPosition.get_pawn_key() << endl;
161 else if (token == "perft")
164 cout << "Unknown command: " << command << endl;
170 // set_position() is called when Stockfish receives the "position" UCI
171 // command. The input parameter is a UCIInputParser. It is assumed
172 // that this parser has consumed the first token of the UCI command
173 // ("position"), and is ready to read the second token ("startpos"
174 // or "fen", if the input is well-formed).
176 void set_position(UCIInputParser& uip) {
180 if (!(uip >> token)) // operator>>() skips any whitespace
183 if (token == "startpos")
184 RootPosition.from_fen(StartPositionFEN);
185 else if (token == "fen")
188 while (uip >> token && token != "moves")
193 RootPosition.from_fen(fen);
198 if (token != "moves")
201 if (token == "moves")
207 move = move_from_string(RootPosition, token);
208 RootPosition.do_move(move, st);
209 if (RootPosition.rule_50_counter() == 0)
210 RootPosition.reset_game_ply();
212 RootPosition.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
214 // Our StateInfo st is about going out of scope so copy
215 // its content inside RootPosition before it disappears.
216 RootPosition.detach();
222 // set_option() is called when Stockfish receives the "setoption" UCI
223 // command. The input parameter is a UCIInputParser. It is assumed
224 // that this parser has consumed the first token of the UCI command
225 // ("setoption"), and is ready to read the second token ("name", if
226 // the input is well-formed).
228 void set_option(UCIInputParser& uip) {
230 string token, name, value;
232 if (!(uip >> token)) // operator>>() skips any whitespace
235 if (token == "name" && uip >> name)
237 while (uip >> token && token != "value")
238 name += (" " + token);
240 if (token == "value" && uip >> value)
243 value += (" " + token);
245 set_option_value(name, value);
252 // go() is called when Stockfish receives the "go" UCI command. The
253 // input parameter is a UCIInputParser. It is assumed that this
254 // parser has consumed the first token of the UCI command ("go"),
255 // and is ready to read the second token. The function sets the
256 // thinking time and other parameters from the input string, and
257 // calls think() (defined in search.cpp) with the appropriate
258 // parameters. Returns false if a quit command is received while
259 // thinking, returns true otherwise.
261 bool go(UCIInputParser& uip) {
265 int time[2] = {0, 0}, inc[2] = {0, 0};
266 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
267 bool infinite = false, ponder = false;
268 Move searchMoves[500];
270 searchMoves[0] = MOVE_NONE;
274 if (token == "infinite")
276 else if (token == "ponder")
278 else if (token == "wtime")
280 else if (token == "btime")
282 else if (token == "winc")
284 else if (token == "binc")
286 else if (token == "movestogo")
288 else if (token == "depth")
290 else if (token == "nodes")
292 else if (token == "movetime")
294 else if (token == "searchmoves")
298 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
300 searchMoves[numOfMoves] = MOVE_NONE;
304 assert(RootPosition.is_ok());
306 return think(RootPosition, infinite, ponder, time, inc, movesToGo,
307 depth, nodes, moveTime, searchMoves);
310 void perft(UCIInputParser& uip) {
318 tm = get_system_time();
320 n = perft(RootPosition, depth * ONE_PLY);
322 tm = get_system_time() - tm;
323 std::cout << "\nNodes " << n
324 << "\nTime (ms) " << tm
325 << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl;