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-2012 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/>.
29 #include "ucioption.h"
35 // FEN string of the initial position, normal chess
36 const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
38 // Keep track of position keys along the setup moves (from start position to the
39 // position just before to start searching). This is needed by draw detection
40 // where, due to 50 moves rule, we need to check at most 100 plies back.
41 StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
43 void set_option(istringstream& up);
44 void set_position(Position& pos, istringstream& up);
45 void go(Position& pos, istringstream& up);
46 void perft(Position& pos, istringstream& up);
50 /// Wait for a command from the user, parse this text string as an UCI command,
51 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
52 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
53 /// commands, the function also supports a few debug commands.
57 Position pos(StartFEN, false, 0); // The root position
60 while (token != "quit")
62 if (!getline(cin, cmd)) // Block here waiting for input
65 istringstream is(cmd);
67 is >> skipws >> token;
69 if (token == "quit" || token == "stop")
70 Threads.stop_thinking();
72 else if (token == "ponderhit")
74 // The opponent has played the expected move. GUI sends "ponderhit" if
75 // we were told to ponder on the same move the opponent has played. We
76 // should continue searching but switching from pondering to normal search.
77 Search::Limits.ponder = false;
79 if (Search::Signals.stopOnPonderhit)
80 Threads.stop_thinking();
83 else if (token == "go")
86 else if (token == "ucinewgame")
87 pos.from_fen(StartFEN, false);
89 else if (token == "isready")
90 cout << "readyok" << endl;
92 else if (token == "position")
93 set_position(pos, is);
95 else if (token == "setoption")
98 else if (token == "perft")
101 else if (token == "d")
104 else if (token == "flip")
107 else if (token == "eval")
109 read_evaluation_uci_options(pos.side_to_move());
110 cout << trace_evaluate(pos) << endl;
113 else if (token == "key")
114 cout << "key: " << hex << pos.key()
115 << "\nmaterial key: " << pos.material_key()
116 << "\npawn key: " << pos.pawn_key() << endl;
118 else if (token == "uci")
119 cout << "id name " << engine_info(true)
121 << "\nuciok" << endl;
123 cout << "Unknown command: " << cmd << endl;
130 // set_position() is called when engine receives the "position" UCI
131 // command. The function sets up the position described in the given
132 // fen string ("fen") or the starting position ("startpos") and then
133 // makes the moves given in the following move list ("moves").
135 void set_position(Position& pos, istringstream& is) {
142 if (token == "startpos")
145 is >> token; // Consume "moves" token if any
147 else if (token == "fen")
148 while (is >> token && token != "moves")
153 pos.from_fen(fen, Options["UCI_Chess960"]);
155 // Parse move list (if any)
156 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
158 pos.do_move(m, *SetupState);
160 // Increment pointer to StateRingBuf circular buffer
161 if (++SetupState - StateRingBuf >= 102)
162 SetupState = StateRingBuf;
167 // set_option() is called when engine receives the "setoption" UCI command. The
168 // function updates the UCI option ("name") to the given value ("value").
170 void set_option(istringstream& is) {
172 string token, name, value;
174 is >> token; // Consume "name" token
176 // Read option name (can contain spaces)
177 while (is >> token && token != "value")
178 name += string(" ", !name.empty()) + token;
180 // Read option value (can contain spaces)
182 value += string(" ", !value.empty()) + token;
184 if (!Options.count(name))
185 cout << "No such option: " << name << endl;
187 else if (value.empty()) // UCI buttons don't have a value
188 Options[name] = true;
191 Options[name] = value;
195 // go() is called when engine receives the "go" UCI command. The function sets
196 // the thinking time and other parameters from the input string, and then starts
197 // the main searching thread.
199 void go(Position& pos, istringstream& is) {
202 Search::LimitsType limits;
203 std::set<Move> searchMoves;
204 int time[] = { 0, 0 }, inc[] = { 0, 0 };
208 if (token == "infinite")
209 limits.infinite = true;
210 else if (token == "ponder")
211 limits.ponder = true;
212 else if (token == "wtime")
214 else if (token == "btime")
216 else if (token == "winc")
218 else if (token == "binc")
220 else if (token == "movestogo")
221 is >> limits.movesToGo;
222 else if (token == "depth")
223 is >> limits.maxDepth;
224 else if (token == "nodes")
225 is >> limits.maxNodes;
226 else if (token == "movetime")
227 is >> limits.maxTime;
228 else if (token == "searchmoves")
230 searchMoves.insert(move_from_uci(pos, token));
233 limits.time = time[pos.side_to_move()];
234 limits.increment = inc[pos.side_to_move()];
236 Threads.start_thinking(pos, limits, searchMoves, true);
240 // perft() is called when engine receives the "perft" command. The function
241 // calls perft() with the required search depth then prints counted leaf nodes
244 void perft(Position& pos, istringstream& is) {
251 time = system_time();
253 int64_t n = Search::perft(pos, depth * ONE_PLY);
255 time = system_time() - time;
257 std::cout << "\nNodes " << n
258 << "\nTime (ms) " << time
259 << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;