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/>.
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 a track of the position keys along the setup moves (from the start position
42 // to the position just before the search starts). This is needed by the repetition
43 // draw detection code.
44 Search::StateStackPtr SetupStates;
47 // position() is called when engine receives the "position" UCI command.
48 // The function sets up the position described in the given FEN string ("fen")
49 // or the starting position ("startpos") and then makes the moves given in the
50 // following move list ("moves").
52 void position(Position& pos, istringstream& is) {
59 if (token == "startpos")
62 is >> token; // Consume "moves" token if any
64 else if (token == "fen")
65 while (is >> token && token != "moves")
70 pos.set(fen, Options["UCI_Chess960"], Threads.main());
71 SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
73 // Parse move list (if any)
74 while (is >> token && (m = UCI::move_from_uci(pos, token)) != MOVE_NONE)
76 SetupStates->push(StateInfo());
77 pos.do_move(m, SetupStates->top());
82 // setoption() is called when engine receives the "setoption" UCI command. The
83 // function updates the UCI option ("name") to the given value ("value").
85 void setoption(istringstream& is) {
87 string token, name, value;
89 is >> token; // Consume "name" token
91 // Read option name (can contain spaces)
92 while (is >> token && token != "value")
93 name += string(" ", !name.empty()) + token;
95 // Read option value (can contain spaces)
97 value += string(" ", !value.empty()) + token;
99 if (Options.count(name))
100 Options[name] = value;
102 sync_cout << "No such option: " << name << sync_endl;
106 // go() is called when engine receives the "go" UCI command. The function sets
107 // the thinking time and other parameters from the input string, and starts
110 void go(const Position& pos, istringstream& is) {
112 Search::LimitsType limits;
117 if (token == "searchmoves")
119 limits.searchmoves.push_back(UCI::move_from_uci(pos, token));
121 else if (token == "wtime") is >> limits.time[WHITE];
122 else if (token == "btime") is >> limits.time[BLACK];
123 else if (token == "winc") is >> limits.inc[WHITE];
124 else if (token == "binc") is >> limits.inc[BLACK];
125 else if (token == "movestogo") is >> limits.movestogo;
126 else if (token == "depth") is >> limits.depth;
127 else if (token == "nodes") is >> limits.nodes;
128 else if (token == "movetime") is >> limits.movetime;
129 else if (token == "mate") is >> limits.mate;
130 else if (token == "infinite") limits.infinite = true;
131 else if (token == "ponder") limits.ponder = true;
134 Threads.start_thinking(pos, limits, SetupStates);
140 /// Wait for a command from the user, parse this text string as an UCI command,
141 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
142 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
143 /// commands, the function also supports a few debug commands.
145 void UCI::loop(int argc, char* argv[]) {
147 Position pos(StartFEN, false, Threads.main()); // The root position
150 for (int i = 1; i < argc; ++i)
151 cmd += std::string(argv[i]) + " ";
154 if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input
157 istringstream is(cmd);
159 is >> skipws >> token;
161 if (token == "quit" || token == "stop" || token == "ponderhit")
163 // The GUI sends 'ponderhit' to tell us to ponder on the same move the
164 // opponent has played. In case Signals.stopOnPonderhit is set we are
165 // waiting for 'ponderhit' to stop the search (for instance because we
166 // already ran out of time), otherwise we should continue searching but
167 // switch from pondering to normal search.
168 if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
170 Search::Signals.stop = true;
171 Threads.main()->notify_one(); // Could be sleeping
174 Search::Limits.ponder = false;
176 else if (token == "perft")
182 ss << Options["Hash"] << " "
183 << Options["Threads"] << " " << depth << " current " << token;
187 else if (token == "key")
188 sync_cout << hex << uppercase << setfill('0')
189 << "position key: " << setw(16) << pos.key()
190 << "\nmaterial key: " << setw(16) << pos.material_key()
191 << "\npawn key: " << setw(16) << pos.pawn_key()
192 << dec << nouppercase << setfill(' ') << sync_endl;
194 else if (token == "uci")
195 sync_cout << "id name " << engine_info(true)
197 << "\nuciok" << sync_endl;
199 else if (token == "ucinewgame") TT.clear();
200 else if (token == "go") go(pos, is);
201 else if (token == "position") position(pos, is);
202 else if (token == "setoption") setoption(is);
203 else if (token == "flip") pos.flip();
204 else if (token == "bench") benchmark(pos, is);
205 else if (token == "d") sync_cout << pos.pretty() << sync_endl;
206 else if (token == "isready") sync_cout << "readyok" << sync_endl;
207 else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
209 sync_cout << "Unknown command: " << cmd << sync_endl;
211 } while (token != "quit" && argc == 1); // Passed args have one-shot behaviour
213 Threads.wait_for_think_finished(); // Cannot quit whilst the search is running