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"
45 // FEN string for the initial position
46 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
48 // UCIParser is a class for parsing UCI input. The class
49 // is actually a string stream built on a given input string.
50 typedef istringstream UCIParser;
53 void set_option(UCIParser& up);
54 void set_position(Position& pos, UCIParser& up);
55 bool go(Position& pos, UCIParser& up);
56 void perft(Position& pos, UCIParser& up);
60 /// execute_uci_command() takes a string as input, uses a UCIParser
61 /// object to parse this text string as a UCI command, and calls
62 /// the appropriate functions. In addition to the UCI commands,
63 /// the function also supports a few debug commands.
65 bool execute_uci_command(const string& cmd) {
67 static Position pos(StartPositionFEN, false, 0); // The root position
71 if (!(up >> token)) // operator>>() skips any whitespace
82 cout << "id name " << engine_name()
83 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
85 cout << "uciok" << endl;
87 else if (token == "ucinewgame")
88 pos.from_fen(StartPositionFEN, false);
90 else if (token == "isready")
91 cout << "readyok" << endl;
93 else if (token == "position")
94 set_position(pos, up);
96 else if (token == "setoption")
99 // The remaining commands are for debugging purposes only
100 else if (token == "d")
103 else if (token == "flip")
105 Position p(pos, pos.thread());
108 else if (token == "eval")
111 cout << "Incremental mg: " << mg_value(pos.value())
112 << "\nIncremental eg: " << eg_value(pos.value())
113 << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
115 else if (token == "key")
116 cout << "key: " << hex << pos.get_key()
117 << "\nmaterial key: " << pos.get_material_key()
118 << "\npawn key: " << pos.get_pawn_key() << endl;
120 else if (token == "perft")
124 cout << "Unknown command: " << cmd << endl;
136 // set_position() is called when Stockfish receives the "position" UCI
137 // command. The input parameter is a UCIParser. It is assumed
138 // that this parser has consumed the first token of the UCI command
139 // ("position"), and is ready to read the second token ("startpos"
140 // or "fen", if the input is well-formed).
142 void set_position(Position& pos, UCIParser& up) {
146 if (!(up >> token) || (token != "startpos" && token != "fen"))
149 if (token == "startpos")
151 pos.from_fen(StartPositionFEN, false);
158 while (up >> token && token != "moves")
163 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
166 if (token != "moves")
169 // Parse optional move list
174 move = move_from_uci(pos, token);
175 pos.do_setup_move(move, st);
177 // Our StateInfo st is about going out of scope so copy
178 // its content inside pos before it disappears.
183 // set_option() is called when Stockfish receives the "setoption" UCI
184 // command. The input parameter is a UCIParser. It is assumed
185 // that this parser has consumed the first token of the UCI command
186 // ("setoption"), and is ready to read the second token ("name", if
187 // the input is well-formed).
189 void set_option(UCIParser& up) {
191 string token, name, value;
193 if (!(up >> token) || token != "name") // operator>>() skips any whitespace
199 // Handle names with included spaces
200 while (up >> token && token != "value")
201 name += (" " + token);
203 if (Options.find(name) == Options.end())
205 cout << "No such option: " << name << endl;
210 if (token != "value")
212 Options[name].set_value("true");
219 // Handle values with included spaces
221 value += (" " + token);
223 Options[name].set_value(value);
227 // go() is called when Stockfish receives the "go" UCI command. The
228 // input parameter is a UCIParser. It is assumed that this
229 // parser has consumed the first token of the UCI command ("go"),
230 // and is ready to read the second token. The function sets the
231 // thinking time and other parameters from the input string, and
232 // calls think() (defined in search.cpp) with the appropriate
233 // parameters. Returns false if a quit command is received while
234 // thinking, returns true otherwise.
236 bool go(Position& pos, UCIParser& up) {
240 int time[2] = {0, 0}, inc[2] = {0, 0};
241 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
242 bool infinite = false, ponder = false;
243 Move searchMoves[MOVES_MAX];
245 searchMoves[0] = MOVE_NONE;
249 if (token == "infinite")
251 else if (token == "ponder")
253 else if (token == "wtime")
255 else if (token == "btime")
257 else if (token == "winc")
259 else if (token == "binc")
261 else if (token == "movestogo")
263 else if (token == "depth")
265 else if (token == "nodes")
267 else if (token == "movetime")
269 else if (token == "searchmoves")
273 searchMoves[numOfMoves++] = move_from_uci(pos, token);
275 searchMoves[numOfMoves] = MOVE_NONE;
281 return think(pos, infinite, ponder, time, inc, movesToGo,
282 depth, nodes, moveTime, searchMoves);
285 void perft(Position& pos, UCIParser& up) {
293 tm = get_system_time();
295 n = perft(pos, depth * ONE_PLY);
297 tm = get_system_time() - tm;
298 std::cout << "\nNodes " << n
299 << "\nTime (ms) " << tm
300 << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;