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-2014 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 a track of the position keys along the setup moves (from the start position
43 // to the position just before the search starts). This is needed by the repetition
44 // draw detection code.
45 Search::StateStackPtr SetupStates;
47 void setoption(istringstream& up);
48 void 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()); // 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 // The 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 // switch from pondering to normal search.
78 if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
80 Search::Signals.stop = true;
81 Threads.main()->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 == "eval")
109 Search::RootColor = pos.side_to_move(); // Ensure it is set
110 sync_cout << Eval::trace(pos) << sync_endl;
112 else if (token == "ucinewgame") TT.clear();
113 else if (token == "go") go(pos, is);
114 else if (token == "position") position(pos, is);
115 else if (token == "setoption") setoption(is);
116 else if (token == "flip") pos.flip();
117 else if (token == "bench") benchmark(pos, is);
118 else if (token == "d") sync_cout << pos.pretty() << sync_endl;
119 else if (token == "isready") sync_cout << "readyok" << sync_endl;
121 sync_cout << "Unknown command: " << cmd << sync_endl;
123 } while (token != "quit" && args.empty()); // Args have one-shot behaviour
125 Threads.wait_for_think_finished(); // Cannot quit whilst the search is running
131 // position() is called when engine receives the "position" UCI command.
132 // The function sets up the position described in the given FEN string ("fen")
133 // or the starting position ("startpos") and then makes the moves given in the
134 // following move list ("moves").
136 void position(Position& pos, istringstream& is) {
143 if (token == "startpos")
146 is >> token; // Consume "moves" token if any
148 else if (token == "fen")
149 while (is >> token && token != "moves")
154 pos.set(fen, Options["UCI_Chess960"], Threads.main());
155 SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
157 // Parse move list (if any)
158 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
160 SetupStates->push(StateInfo());
161 pos.do_move(m, SetupStates->top());
166 // setoption() is called when engine receives the "setoption" UCI command. The
167 // function updates the UCI option ("name") to the given value ("value").
169 void setoption(istringstream& is) {
171 string token, name, value;
173 is >> token; // Consume "name" token
175 // Read option name (can contain spaces)
176 while (is >> token && token != "value")
177 name += string(" ", !name.empty()) + token;
179 // Read option value (can contain spaces)
181 value += string(" ", !value.empty()) + token;
183 if (Options.count(name))
184 Options[name] = value;
186 sync_cout << "No such option: " << name << sync_endl;
190 // go() is called when engine receives the "go" UCI command. The function sets
191 // the thinking time and other parameters from the input string, and starts
194 void go(const Position& pos, istringstream& is) {
196 Search::LimitsType limits;
201 if (token == "searchmoves")
203 limits.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, SetupStates);