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/>.
30 #include "ucioption.h"
34 extern void benchmark(const Position& pos, istream& is);
38 // FEN string of the initial position, normal chess
39 const char* StartFEN = "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). Needed by repetition draw detection.
43 Search::StateStackPtr SetupStates;
45 void set_option(istringstream& up);
46 void set_position(Position& pos, istringstream& up);
47 void go(const Position& pos, istringstream& up);
51 /// Wait for a command from the user, parse this text string as an UCI command,
52 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
53 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
54 /// commands, the function also supports a few debug commands.
56 void UCI::loop(const string& args) {
58 Position pos(StartFEN, false, Threads.main_thread()); // The root position
59 string token, cmd = args;
62 if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
65 istringstream is(cmd);
67 is >> skipws >> token;
69 if (token == "quit" || token == "stop" || token == "ponderhit")
71 // GUI sends 'ponderhit' to tell us to ponder on the same move the
72 // opponent has played. In case Signals.stopOnPonderhit is set we are
73 // waiting for 'ponderhit' to stop the search (for instance because we
74 // already ran out of time), otherwise we should continue searching but
75 // switching from pondering to normal search.
76 if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
78 Search::Signals.stop = true;
79 Threads.main_thread()->notify_one(); // Could be sleeping
82 Search::Limits.ponder = false;
84 else if (token == "perft" && (is >> token)) // Read perft depth
88 ss << Options["Hash"] << " "
89 << Options["Threads"] << " " << token << " current perft";
93 else if (token == "key") sync_cout << "position key: " << hex << pos.key()
94 << "\nmaterial key: " << pos.material_key()
95 << "\npawn key: " << pos.pawn_key()
98 else if (token == "uci") sync_cout << "id name " << engine_info(true)
100 << "\nuciok" << sync_endl;
102 else if (token == "ucinewgame") TT.clear();
103 else if (token == "go") go(pos, is);
104 else if (token == "position") set_position(pos, is);
105 else if (token == "setoption") set_option(is);
106 else if (token == "flip") pos.flip();
107 else if (token == "bench") benchmark(pos, is);
108 else if (token == "d") sync_cout << pos.pretty() << sync_endl;
109 else if (token == "isready") sync_cout << "readyok" << sync_endl;
110 else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
112 sync_cout << "Unknown command: " << cmd << sync_endl;
114 } while (token != "quit" && args.empty()); // Args have one-shot behaviour
116 Threads.wait_for_think_finished(); // Cannot quit while search is running
122 // set_position() is called when engine receives the "position" UCI command.
123 // The function sets up the position described in the given fen string ("fen")
124 // or the starting position ("startpos") and then makes the moves given in the
125 // following move list ("moves").
127 void set_position(Position& pos, istringstream& is) {
134 if (token == "startpos")
137 is >> token; // Consume "moves" token if any
139 else if (token == "fen")
140 while (is >> token && token != "moves")
145 pos.set(fen, Options["UCI_Chess960"], Threads.main_thread());
146 SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
148 // Parse move list (if any)
149 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
151 SetupStates->push(StateInfo());
152 pos.do_move(m, SetupStates->top());
157 // set_option() is called when engine receives the "setoption" UCI command. The
158 // function updates the UCI option ("name") to the given value ("value").
160 void set_option(istringstream& is) {
162 string token, name, value;
164 is >> token; // Consume "name" token
166 // Read option name (can contain spaces)
167 while (is >> token && token != "value")
168 name += string(" ", !name.empty()) + token;
170 // Read option value (can contain spaces)
172 value += string(" ", !value.empty()) + token;
174 if (Options.count(name))
175 Options[name] = value;
177 sync_cout << "No such option: " << name << sync_endl;
181 // go() is called when engine receives the "go" UCI command. The function sets
182 // the thinking time and other parameters from the input string, and starts
185 void go(const Position& pos, istringstream& is) {
187 Search::LimitsType limits;
188 vector<Move> searchMoves;
193 if (token == "searchmoves")
195 searchMoves.push_back(move_from_uci(pos, token));
197 else if (token == "wtime") is >> limits.time[WHITE];
198 else if (token == "btime") is >> limits.time[BLACK];
199 else if (token == "winc") is >> limits.inc[WHITE];
200 else if (token == "binc") is >> limits.inc[BLACK];
201 else if (token == "movestogo") is >> limits.movestogo;
202 else if (token == "depth") is >> limits.depth;
203 else if (token == "nodes") is >> limits.nodes;
204 else if (token == "movetime") is >> limits.movetime;
205 else if (token == "mate") is >> limits.mate;
206 else if (token == "infinite") limits.infinite = true;
207 else if (token == "ponder") limits.ponder = true;
210 Threads.start_thinking(pos, limits, searchMoves, SetupStates);