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/>.
32 #include "ucioption.h"
38 // FEN string for the initial position
39 const char* StarFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
41 // Keep track of position keys along the setup moves (from start position to the
42 // position just before to start searching). This is needed by draw detection
43 // where, due to 50 moves rule, we need to check at most 100 plies back.
44 StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
46 void set_option(istringstream& up);
47 void set_position(Position& pos, istringstream& up);
48 bool go(Position& pos, istringstream& up);
49 void perft(Position& pos, istringstream& up);
53 /// Wait for a command from the user, parse this text string as an UCI command,
54 /// and calls the appropriate functions. Also intercepts EOF from stdin to
55 /// ensure that we exit gracefully if the GUI dies unexpectedly. In addition to
56 /// the UCI commands, the function also supports a few debug commands.
60 Position pos(StarFEN, false, 0); // The root position
68 istringstream is(cmd);
70 is >> skipws >> token;
75 else if (token == "go")
78 else if (token == "ucinewgame")
79 pos.from_fen(StarFEN, false);
81 else if (token == "isready")
82 cout << "readyok" << endl;
84 else if (token == "position")
85 set_position(pos, is);
87 else if (token == "setoption")
90 else if (token == "perft")
93 else if (token == "d")
96 else if (token == "flip")
99 else if (token == "eval")
101 read_evaluation_uci_options(pos.side_to_move());
102 cout << trace_evaluate(pos) << endl;
105 else if (token == "key")
106 cout << "key: " << hex << pos.get_key()
107 << "\nmaterial key: " << pos.get_material_key()
108 << "\npawn key: " << pos.get_pawn_key() << endl;
110 else if (token == "uci")
111 cout << "id name " << engine_name()
112 << "\nid author " << engine_authors()
113 << "\n" << Options.print_all()
114 << "\nuciok" << endl;
116 cout << "Unknown command: " << cmd << endl;
123 // set_position() is called when engine receives the "position" UCI
124 // command. The function sets up the position described in the given
125 // fen string ("fen") or the starting position ("startpos") and then
126 // makes the moves given in the following move list ("moves").
128 void set_position(Position& pos, istringstream& is) {
135 if (token == "startpos")
138 is >> token; // Consume "moves" token if any
140 else if (token == "fen")
141 while (is >> token && token != "moves")
146 pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
148 // Parse move list (if any)
149 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
151 pos.do_move(m, *SetupState);
153 // Increment pointer to StateRingBuf circular buffer
154 if (++SetupState - StateRingBuf >= 102)
155 SetupState = StateRingBuf;
160 // set_option() is called when engine receives the "setoption" UCI
161 // command. The function updates the corresponding UCI option ("name")
162 // to the given value ("value").
164 void set_option(istringstream& is) {
166 string token, name, value;
168 is >> token; // Consume "name" token
170 // Read option name (can contain spaces)
171 while (is >> token && token != "value")
172 name += string(" ", !name.empty()) + token;
174 // Read option value (can contain spaces)
176 value += string(" ", !value.empty()) + token;
178 if (Options.find(name) != Options.end())
179 Options[name].set_value(value.empty() ? "true" : value); // UCI buttons don't have "value"
181 cout << "No such option: " << name << endl;
185 // go() is called when engine receives the "go" UCI command. The
186 // function sets the thinking time and other parameters from the input
187 // string, and then calls think(). Returns false if a quit command
188 // is received while thinking, true otherwise.
190 bool go(Position& pos, istringstream& is) {
194 std::vector<Move> searchMoves;
195 int time[] = { 0, 0 }, inc[] = { 0, 0 };
199 if (token == "infinite")
200 limits.infinite = true;
201 else if (token == "ponder")
202 limits.ponder = true;
203 else if (token == "wtime")
205 else if (token == "btime")
207 else if (token == "winc")
209 else if (token == "binc")
211 else if (token == "movestogo")
212 is >> limits.movesToGo;
213 else if (token == "depth")
214 is >> limits.maxDepth;
215 else if (token == "nodes")
216 is >> limits.maxNodes;
217 else if (token == "movetime")
218 is >> limits.maxTime;
219 else if (token == "searchmoves")
221 searchMoves.push_back(move_from_uci(pos, token));
224 searchMoves.push_back(MOVE_NONE);
225 limits.time = time[pos.side_to_move()];
226 limits.increment = inc[pos.side_to_move()];
228 return think(pos, limits, &searchMoves[0]);
232 // perft() is called when engine receives the "perft" command.
233 // The function calls perft() passing the required search depth
234 // then prints counted leaf nodes and elapsed time.
236 void perft(Position& pos, istringstream& is) {
244 time = get_system_time();
246 n = perft(pos, depth * ONE_PLY);
248 time = get_system_time() - time;
250 std::cout << "\nNodes " << n
251 << "\nTime (ms) " << time
252 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;