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")
71 Search::Signals.stop = true;
72 Threads.wait_for_search_finished(); // Cannot quit while threads are running
75 else if (token == "ponderhit")
77 // The opponent has played the expected move. GUI sends "ponderhit" if
78 // we were told to ponder on the same move the opponent has played. We
79 // should continue searching but switching from pondering to normal search.
80 Search::Limits.ponder = false;
82 if (Search::Signals.stopOnPonderhit)
84 Search::Signals.stop = true;
85 Threads.wait_for_search_finished(); // Wake up if is sleeping
89 else if (token == "go")
92 else if (token == "ucinewgame")
93 { /* Avoid returning "Unknown command" */ }
95 else if (token == "isready")
96 cout << "readyok" << endl;
98 else if (token == "position")
99 set_position(pos, is);
101 else if (token == "setoption")
104 else if (token == "perft")
107 else if (token == "d")
110 else if (token == "flip")
113 else if (token == "eval")
114 cout << Eval::trace(pos) << endl;
116 else if (token == "key")
117 cout << "key: " << hex << pos.key()
118 << "\nmaterial key: " << pos.material_key()
119 << "\npawn key: " << pos.pawn_key() << endl;
121 else if (token == "uci")
122 cout << "id name " << engine_info(true)
124 << "\nuciok" << endl;
126 cout << "Unknown command: " << cmd << endl;
133 // set_position() is called when engine receives the "position" UCI
134 // command. The function sets up the position described in the given
135 // fen string ("fen") or the starting position ("startpos") and then
136 // makes the moves given in the following move list ("moves").
138 void set_position(Position& pos, istringstream& is) {
145 if (token == "startpos")
148 is >> token; // Consume "moves" token if any
150 else if (token == "fen")
151 while (is >> token && token != "moves")
156 pos.from_fen(fen, Options["UCI_Chess960"]);
158 // Parse move list (if any)
159 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
161 pos.do_move(m, *SetupState);
163 // Increment pointer to StateRingBuf circular buffer
164 if (++SetupState - StateRingBuf >= 102)
165 SetupState = StateRingBuf;
170 // set_option() is called when engine receives the "setoption" UCI command. The
171 // function updates the UCI option ("name") to the given value ("value").
173 void set_option(istringstream& is) {
175 string token, name, value;
177 is >> token; // Consume "name" token
179 // Read option name (can contain spaces)
180 while (is >> token && token != "value")
181 name += string(" ", !name.empty()) + token;
183 // Read option value (can contain spaces)
185 value += string(" ", !value.empty()) + token;
187 if (Options.count(name))
188 Options[name] = value;
190 cout << "No such option: " << name << endl;
194 // go() is called when engine receives the "go" UCI command. The function sets
195 // the thinking time and other parameters from the input string, and then starts
198 void go(Position& pos, istringstream& is) {
200 Search::LimitsType limits;
201 std::set<Move> searchMoves;
206 if (token == "wtime")
207 is >> limits.times[WHITE];
208 else if (token == "btime")
209 is >> limits.times[BLACK];
210 else if (token == "winc")
211 is >> limits.incs[WHITE];
212 else if (token == "binc")
213 is >> limits.incs[BLACK];
214 else if (token == "movestogo")
215 is >> limits.movestogo;
216 else if (token == "depth")
218 else if (token == "nodes")
220 else if (token == "movetime")
221 is >> limits.movetime;
222 else if (token == "infinite")
223 limits.infinite = true;
224 else if (token == "ponder")
225 limits.ponder = true;
226 else if (token == "searchmoves")
228 searchMoves.insert(move_from_uci(pos, token));
231 Threads.start_searching(pos, limits, searchMoves);
235 // perft() is called when engine receives the "perft" command. The function
236 // calls perft() with the required search depth then prints counted leaf nodes
239 void perft(Position& pos, istringstream& is) {
246 Time time = Time::current_time();
248 int64_t n = Search::perft(pos, depth * ONE_PLY);
250 int e = time.elapsed();
252 std::cout << "\nNodes " << n
253 << "\nTime (ms) " << e
254 << "\nNodes/second " << int(n / (e / 1000.0)) << std::endl;