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 >> token; // operator>>() skips any whitespace
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) {
125 up >> token; // operator>>() skips any whitespace
127 if (token == "startpos")
129 pos.from_fen(StartPositionFEN, false);
130 up >> token; // Consume "moves" token if any
132 else if (token == "fen")
134 while (up >> token && token != "moves")
137 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
141 // Parse move list (if any)
143 pos.do_setup_move(move_from_uci(pos, token));
147 // set_option() is called when engine receives the "setoption" UCI
148 // command. The function updates the corresponding UCI option ("name")
149 // to the given value ("value").
151 void set_option(UCIParser& up) {
154 string value = "true"; // UCI buttons don't have a "value" field
156 up >> token; // Consume "name" token
157 up >> name; // Read option name
159 // Handle names with included spaces
160 while (up >> token && token != "value")
163 up >> value; // Read option value
165 // Handle values with included spaces
167 value += " " + token;
169 if (Options.find(name) != Options.end())
170 Options[name].set_value(value);
172 cout << "No such option: " << name << endl;
176 // go() is called when engine receives the "go" UCI command. The
177 // function sets the thinking time and other parameters from the input
178 // string, and then calls think(). Returns false if a quit command
179 // is received while thinking, true otherwise.
181 bool go(Position& pos, UCIParser& up) {
185 Move searchMoves[MAX_MOVES], *cur = searchMoves;
186 int time[] = { 0, 0 }, inc[] = { 0, 0 };
190 if (token == "infinite")
191 limits.infinite = true;
192 else if (token == "ponder")
193 limits.ponder = true;
194 else if (token == "wtime")
196 else if (token == "btime")
198 else if (token == "winc")
200 else if (token == "binc")
202 else if (token == "movestogo")
203 up >> limits.movesToGo;
204 else if (token == "depth")
205 up >> limits.maxDepth;
206 else if (token == "nodes")
207 up >> limits.maxNodes;
208 else if (token == "movetime")
209 up >> limits.maxTime;
210 else if (token == "searchmoves")
212 *cur++ = move_from_uci(pos, token);
216 limits.time = time[pos.side_to_move()];
217 limits.increment = inc[pos.side_to_move()];
221 return think(pos, limits, searchMoves);
225 // perft() is called when engine receives the "perft" command.
226 // The function calls perft() passing the required search depth
227 // then prints counted leaf nodes and elapsed time.
229 void perft(Position& pos, UCIParser& up) {
237 time = get_system_time();
239 n = perft(pos, depth * ONE_PLY);
241 time = get_system_time() - time;
243 std::cout << "\nNodes " << n
244 << "\nTime (ms) " << time
245 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;