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/>.
31 #include "ucioption.h"
37 // FEN string for the initial position
38 const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
40 // Keep track of position keys along the setup moves (from start position to the
41 // position just before to start searching). This is needed by draw detection
42 // where, due to 50 moves rule, we need to check at most 100 plies back.
43 StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
45 void set_option(istringstream& up);
46 void set_position(Position& pos, istringstream& up);
47 bool go(Position& pos, istringstream& up);
48 void perft(Position& pos, istringstream& up);
52 /// execute_uci_command() takes a string as input, parses this text string as
53 /// an UCI command, and calls the appropriate functions. In addition to the
54 /// UCI commands, the function also supports a few debug commands.
56 bool execute_uci_command(const string& cmd) {
58 static Position pos(StartPositionFEN, false, 0); // The root position
60 istringstream is(cmd);
63 is >> skipws >> token;
71 if (token == "ucinewgame")
72 pos.from_fen(StartPositionFEN, false);
74 else if (token == "isready")
75 cout << "readyok" << endl;
77 else if (token == "position")
78 set_position(pos, is);
80 else if (token == "setoption")
83 else if (token == "perft")
86 else if (token == "d")
89 else if (token == "flip")
92 else if (token == "eval")
94 read_evaluation_uci_options(pos.side_to_move());
95 cout << trace_evaluate(pos) << endl;
98 else if (token == "key")
99 cout << "key: " << hex << pos.get_key()
100 << "\nmaterial key: " << pos.get_material_key()
101 << "\npawn key: " << pos.get_pawn_key() << endl;
103 else if (token == "uci")
104 cout << "id name " << engine_name()
105 << "\nid author " << engine_authors()
106 << "\n" << Options.print_all()
107 << "\nuciok" << endl;
109 cout << "Unknown command: " << cmd << endl;
117 // set_position() is called when engine receives the "position" UCI
118 // command. The function sets up the position described in the given
119 // fen string ("fen") or the starting position ("startpos") and then
120 // makes the moves given in the following move list ("moves").
122 void set_position(Position& pos, istringstream& is) {
129 if (token == "startpos")
131 fen = StartPositionFEN;
132 is >> token; // Consume "moves" token if any
134 else if (token == "fen")
135 while (is >> token && token != "moves")
140 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
142 // Parse move list (if any)
143 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
145 pos.do_move(m, *SetupState);
147 // Increment pointer to StateRingBuf circular buffer
148 if (++SetupState - StateRingBuf >= 102)
149 SetupState = StateRingBuf;
154 // set_option() is called when engine receives the "setoption" UCI
155 // command. The function updates the corresponding UCI option ("name")
156 // to the given value ("value").
158 void set_option(istringstream& is) {
160 string token, name, value;
162 is >> token; // Consume "name" token
164 // Read option name (can contain spaces)
165 while (is >> token && token != "value")
166 name += string(" ", !name.empty()) + token;
168 // Read option value (can contain spaces)
170 value += string(" ", !value.empty()) + token;
172 if (Options.find(name) != Options.end())
173 Options[name].set_value(value.empty() ? "true" : value); // UCI buttons don't have "value"
175 cout << "No such option: " << name << endl;
179 // go() is called when engine receives the "go" UCI command. The
180 // function sets the thinking time and other parameters from the input
181 // string, and then calls think(). Returns false if a quit command
182 // is received while thinking, true otherwise.
184 bool go(Position& pos, istringstream& is) {
188 std::vector<Move> searchMoves;
189 int time[] = { 0, 0 }, inc[] = { 0, 0 };
193 if (token == "infinite")
194 limits.infinite = true;
195 else if (token == "ponder")
196 limits.ponder = true;
197 else if (token == "wtime")
199 else if (token == "btime")
201 else if (token == "winc")
203 else if (token == "binc")
205 else if (token == "movestogo")
206 is >> limits.movesToGo;
207 else if (token == "depth")
208 is >> limits.maxDepth;
209 else if (token == "nodes")
210 is >> limits.maxNodes;
211 else if (token == "movetime")
212 is >> limits.maxTime;
213 else if (token == "searchmoves")
215 searchMoves.push_back(move_from_uci(pos, token));
218 searchMoves.push_back(MOVE_NONE);
219 limits.time = time[pos.side_to_move()];
220 limits.increment = inc[pos.side_to_move()];
222 return think(pos, limits, &searchMoves[0]);
226 // perft() is called when engine receives the "perft" command.
227 // The function calls perft() passing the required search depth
228 // then prints counted leaf nodes and elapsed time.
230 void perft(Position& pos, istringstream& is) {
238 time = get_system_time();
240 n = perft(pos, depth * ONE_PLY);
242 time = get_system_time() - time;
244 std::cout << "\nNodes " << n
245 << "\nTime (ms) " << time
246 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;