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"
44 // FEN string for the initial position
45 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
47 // UCIParser is a class for parsing UCI input. The class
48 // is actually a string stream built on a given input string.
49 typedef istringstream UCIParser;
52 void set_option(UCIParser& up);
53 void set_position(Position& pos, UCIParser& up);
54 bool go(Position& pos, UCIParser& up);
55 void perft(Position& pos, UCIParser& up);
59 /// execute_uci_command() takes a string as input, uses a UCIParser
60 /// object to parse this text string as a UCI command, and calls
61 /// the appropriate functions. In addition to the UCI commands,
62 /// the function also supports a few debug commands.
64 bool execute_uci_command(const string& cmd) {
66 static Position pos(StartPositionFEN, false, 0); // The root position
70 if (!(up >> token)) // operator>>() skips any whitespace
81 cout << "id name " << engine_name()
82 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
84 cout << "uciok" << endl;
86 else if (token == "ucinewgame")
87 pos.from_fen(StartPositionFEN, false);
89 else if (token == "isready")
90 cout << "readyok" << endl;
92 else if (token == "position")
93 set_position(pos, up);
95 else if (token == "setoption")
98 // The remaining commands are for debugging purposes only
99 else if (token == "d")
102 else if (token == "flip")
104 Position p(pos, pos.thread());
107 else if (token == "eval")
110 cout << "Incremental mg: " << mg_value(pos.value())
111 << "\nIncremental eg: " << eg_value(pos.value())
112 << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
114 else if (token == "key")
115 cout << "key: " << hex << pos.get_key()
116 << "\nmaterial key: " << pos.get_material_key()
117 << "\npawn key: " << pos.get_pawn_key() << endl;
119 else if (token == "perft")
123 cout << "Unknown command: " << cmd << endl;
135 // set_position() is called when Stockfish receives the "position" UCI
136 // command. The input parameter is a UCIParser. It is assumed
137 // that this parser has consumed the first token of the UCI command
138 // ("position"), and is ready to read the second token ("startpos"
139 // or "fen", if the input is well-formed).
141 void set_position(Position& pos, UCIParser& up) {
145 if (!(up >> token) || (token != "startpos" && token != "fen"))
148 if (token == "startpos")
150 pos.from_fen(StartPositionFEN, false);
151 up >> token; // Consume "moves" token
155 while (up >> token && token != "moves")
156 fen += token + string(" ");
158 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
161 // Parse move list (if any)
163 pos.do_setup_move(move_from_uci(pos, token));
167 // set_option() is called when Stockfish receives the "setoption" UCI
168 // command. The input parameter is a UCIParser. It is assumed
169 // that this parser has consumed the first token of the UCI command
170 // ("setoption"), and is ready to read the second token ("name", if
171 // the input is well-formed).
173 void set_option(UCIParser& up) {
175 string token, name, value;
177 if (!(up >> token) || token != "name") // operator>>() skips any whitespace
183 // Handle names with included spaces
184 while (up >> token && token != "value")
185 name += (" " + token);
187 if (Options.find(name) == Options.end())
189 cout << "No such option: " << name << endl;
194 if (token != "value")
196 Options[name].set_value("true");
203 // Handle values with included spaces
205 value += (" " + token);
207 Options[name].set_value(value);
211 // go() is called when Stockfish receives the "go" UCI command. The
212 // input parameter is a UCIParser. It is assumed that this
213 // parser has consumed the first token of the UCI command ("go"),
214 // and is ready to read the second token. The function sets the
215 // thinking time and other parameters from the input string, and
216 // calls think() (defined in search.cpp) with the appropriate
217 // parameters. Returns false if a quit command is received while
218 // thinking, returns true otherwise.
220 bool go(Position& pos, UCIParser& up) {
224 int time[2] = {0, 0}, inc[2] = {0, 0};
225 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
226 bool infinite = false, ponder = false;
227 Move searchMoves[MOVES_MAX];
229 searchMoves[0] = MOVE_NONE;
233 if (token == "infinite")
235 else if (token == "ponder")
237 else if (token == "wtime")
239 else if (token == "btime")
241 else if (token == "winc")
243 else if (token == "binc")
245 else if (token == "movestogo")
247 else if (token == "depth")
249 else if (token == "nodes")
251 else if (token == "movetime")
253 else if (token == "searchmoves")
257 searchMoves[numOfMoves++] = move_from_uci(pos, token);
259 searchMoves[numOfMoves] = MOVE_NONE;
265 return think(pos, infinite, ponder, time, inc, movesToGo,
266 depth, nodes, moveTime, searchMoves);
269 void perft(Position& pos, UCIParser& up) {
277 tm = get_system_time();
279 n = perft(pos, depth * ONE_PLY);
281 tm = get_system_time() - tm;
282 std::cout << "\nNodes " << n
283 << "\nTime (ms) " << tm
284 << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;