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-2013 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"
35 extern void benchmark(const Position& pos, istream& is);
39 // FEN string of the initial position, normal chess
40 const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
42 // Keep track of position keys along the setup moves (from start position to the
43 // position just before to start searching). Needed by repetition draw detection.
44 Search::StateStackPtr SetupStates;
45 Search::MovesVectPtr SetupMoves;
47 void set_option(istringstream& up);
48 void set_position(Position& pos, istringstream& up);
49 void go(const Position& pos, istringstream& up);
53 /// Wait for a command from the user, parse this text string as an UCI command,
54 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
55 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
56 /// commands, the function also supports a few debug commands.
58 void UCI::loop(const string& args) {
60 Position pos(StartFEN, false, Threads.main_thread()); // The root position
61 string token, cmd = args;
64 if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
67 istringstream is(cmd);
69 is >> skipws >> token;
71 if (token == "quit" || token == "stop" || token == "ponderhit")
73 // GUI sends 'ponderhit' to tell us to ponder on the same move the
74 // opponent has played. In case Signals.stopOnPonderhit is set we are
75 // waiting for 'ponderhit' to stop the search (for instance because we
76 // already ran out of time), otherwise we should continue searching but
77 // switching from pondering to normal search.
78 if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
80 Search::Signals.stop = true;
81 Threads.main_thread()->notify_one(); // Could be sleeping
84 Search::Limits.ponder = false;
86 else if (token == "perft" && (is >> token)) // Read perft depth
90 ss << Options["Hash"] << " "
91 << Options["Threads"] << " " << token << " current perft";
95 else if (token == "key")
96 sync_cout << hex << uppercase << setfill('0')
97 << "position key: " << setw(16) << pos.key()
98 << "\nmaterial key: " << setw(16) << pos.material_key()
99 << "\npawn key: " << setw(16) << pos.pawn_key()
102 else if (token == "uci")
103 sync_cout << "id name " << engine_info(true)
105 << "\nuciok" << sync_endl;
107 else if (token == "ucinewgame") TT.clear();
108 else if (token == "go") go(pos, is);
109 else if (token == "position") set_position(pos, is);
110 else if (token == "setoption") set_option(is);
111 else if (token == "flip") pos.flip();
112 else if (token == "bench") benchmark(pos, is);
113 else if (token == "d") sync_cout << pos.pretty() << sync_endl;
114 else if (token == "isready") sync_cout << "readyok" << sync_endl;
115 else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
117 sync_cout << "Unknown command: " << cmd << sync_endl;
119 } while (token != "quit" && args.empty()); // Args have one-shot behaviour
121 Threads.wait_for_think_finished(); // Cannot quit while search is running
127 // set_position() is called when engine receives the "position" UCI command.
128 // The function sets up the position described in the given fen string ("fen")
129 // or the starting position ("startpos") and then makes the moves given in the
130 // following move list ("moves").
132 void set_position(Position& pos, istringstream& is) {
139 if (token == "startpos")
142 is >> token; // Consume "moves" token if any
144 else if (token == "fen")
145 while (is >> token && token != "moves")
150 pos.set(fen, Options["UCI_Chess960"], Threads.main_thread());
151 SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
152 SetupMoves = Search::MovesVectPtr(new std::vector<Move>());
153 SetupMoves->reserve(200); // Try to avoid reallocations
155 // Parse move list (if any)
156 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
158 SetupMoves->push_back(m);
159 SetupStates->push(StateInfo());
160 pos.do_move(m, SetupStates->top());
165 // set_option() is called when engine receives the "setoption" UCI command. The
166 // function updates the UCI option ("name") to the given value ("value").
168 void set_option(istringstream& is) {
170 string token, name, value;
172 is >> token; // Consume "name" token
174 // Read option name (can contain spaces)
175 while (is >> token && token != "value")
176 name += string(" ", !name.empty()) + token;
178 // Read option value (can contain spaces)
180 value += string(" ", !value.empty()) + token;
182 if (Options.count(name))
183 Options[name] = value;
185 sync_cout << "No such option: " << name << sync_endl;
189 // go() is called when engine receives the "go" UCI command. The function sets
190 // the thinking time and other parameters from the input string, and starts
193 void go(const Position& pos, istringstream& is) {
195 Search::LimitsType limits;
196 vector<Move> searchMoves;
201 if (token == "searchmoves")
203 searchMoves.push_back(move_from_uci(pos, token));
205 else if (token == "wtime") is >> limits.time[WHITE];
206 else if (token == "btime") is >> limits.time[BLACK];
207 else if (token == "winc") is >> limits.inc[WHITE];
208 else if (token == "binc") is >> limits.inc[BLACK];
209 else if (token == "movestogo") is >> limits.movestogo;
210 else if (token == "depth") is >> limits.depth;
211 else if (token == "nodes") is >> limits.nodes;
212 else if (token == "movetime") is >> limits.movetime;
213 else if (token == "mate") is >> limits.mate;
214 else if (token == "infinite") limits.infinite = true;
215 else if (token == "ponder") limits.ponder = true;
218 Threads.start_thinking(pos, limits, searchMoves, SetupStates, SetupMoves);