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/>.
30 #include "ucioption.h"
36 // FEN string for the initial position
37 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
39 // UCIParser is a class for parsing UCI input. The class
40 // is actually a string stream built on a given input string.
41 typedef istringstream UCIParser;
43 void set_option(UCIParser& up);
44 void set_position(Position& pos, UCIParser& up);
45 bool go(Position& pos, UCIParser& up);
46 void perft(Position& pos, UCIParser& up);
50 /// execute_uci_command() takes a string as input, uses a UCIParser
51 /// object to parse this text string as a UCI command, and calls
52 /// the appropriate functions. In addition to the UCI commands,
53 /// the function also supports a few debug commands.
55 bool execute_uci_command(const string& cmd) {
57 static Position pos(StartPositionFEN, false, 0); // The root position
62 up >> skipws >> token;
70 if (token == "ucinewgame")
71 pos.from_fen(StartPositionFEN, false);
73 else if (token == "isready")
74 cout << "readyok" << endl;
76 else if (token == "position")
77 set_position(pos, up);
79 else if (token == "setoption")
82 else if (token == "perft")
85 else if (token == "d")
88 else if (token == "flip")
91 else if (token == "eval")
93 read_evaluation_uci_options(pos.side_to_move());
94 cout << trace_evaluate(pos) << endl;
97 else if (token == "key")
98 cout << "key: " << hex << pos.get_key()
99 << "\nmaterial key: " << pos.get_material_key()
100 << "\npawn key: " << pos.get_pawn_key() << endl;
102 else if (token == "uci")
103 cout << "id name " << engine_name()
104 << "\nid author " << engine_authors()
105 << "\n" << Options.print_all()
106 << "\nuciok" << endl;
108 cout << "Unknown command: " << cmd << endl;
116 // set_position() is called when engine receives the "position" UCI
117 // command. The function sets up the position described in the given
118 // fen string ("fen") or the starting position ("startpos") and then
119 // makes the moves given in the following move list ("moves").
121 void set_position(Position& pos, UCIParser& up) {
128 if (token == "startpos")
130 pos.from_fen(StartPositionFEN, false);
131 up >> token; // Consume "moves" token if any
133 else if (token == "fen")
135 while (up >> token && token != "moves")
138 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
142 // Parse move list (if any)
143 while (up >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
144 pos.do_setup_move(m);
148 // set_option() is called when engine receives the "setoption" UCI
149 // command. The function updates the corresponding UCI option ("name")
150 // to the given value ("value").
152 void set_option(UCIParser& up) {
154 string token, name, value;
156 up >> token; // Consume "name" token
158 // Read option name (can contain spaces)
159 while (up >> token && token != "value")
160 name += string(" ", !name.empty()) + token;
162 // Read option value (can contain spaces)
164 value += string(" ", !value.empty()) + token;
166 if (Options.find(name) != Options.end())
167 Options[name].set_value(value.empty() ? "true" : value); // UCI buttons don't have "value"
169 cout << "No such option: " << name << endl;
173 // go() is called when engine receives the "go" UCI command. The
174 // function sets the thinking time and other parameters from the input
175 // string, and then calls think(). Returns false if a quit command
176 // is received while thinking, true otherwise.
178 bool go(Position& pos, UCIParser& up) {
182 Move searchMoves[MAX_MOVES], *cur = searchMoves;
183 int time[] = { 0, 0 }, inc[] = { 0, 0 };
187 if (token == "infinite")
188 limits.infinite = true;
189 else if (token == "ponder")
190 limits.ponder = true;
191 else if (token == "wtime")
193 else if (token == "btime")
195 else if (token == "winc")
197 else if (token == "binc")
199 else if (token == "movestogo")
200 up >> limits.movesToGo;
201 else if (token == "depth")
202 up >> limits.maxDepth;
203 else if (token == "nodes")
204 up >> limits.maxNodes;
205 else if (token == "movetime")
206 up >> limits.maxTime;
207 else if (token == "searchmoves")
209 *cur++ = move_from_uci(pos, token);
213 limits.time = time[pos.side_to_move()];
214 limits.increment = inc[pos.side_to_move()];
216 return think(pos, limits, searchMoves);
220 // perft() is called when engine receives the "perft" command.
221 // The function calls perft() passing the required search depth
222 // then prints counted leaf nodes and elapsed time.
224 void perft(Position& pos, UCIParser& up) {
232 time = get_system_time();
234 n = perft(pos, depth * ONE_PLY);
236 time = get_system_time() - time;
238 std::cout << "\nNodes " << n
239 << "\nTime (ms) " << time
240 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;