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;
46 void setoption(istringstream& up);
47 void position(Position& pos, istringstream& up);
48 void go(const Position& pos, istringstream& up);
52 /// Wait for a command from the user, parse this text string as an UCI command,
53 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
54 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
55 /// commands, the function also supports a few debug commands.
57 void UCI::loop(const string& args) {
59 Position pos(StartFEN, false, Threads.main_thread()); // The root position
60 string token, cmd = args;
63 if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
66 istringstream is(cmd);
68 is >> skipws >> token;
70 if (token == "quit" || token == "stop" || token == "ponderhit")
72 // GUI sends 'ponderhit' to tell us to ponder on the same move the
73 // opponent has played. In case Signals.stopOnPonderhit is set we are
74 // waiting for 'ponderhit' to stop the search (for instance because we
75 // already ran out of time), otherwise we should continue searching but
76 // switching from pondering to normal search.
77 if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
79 Search::Signals.stop = true;
80 Threads.main_thread()->notify_one(); // Could be sleeping
83 Search::Limits.ponder = false;
85 else if (token == "perft" && (is >> token)) // Read perft depth
89 ss << Options["Hash"] << " "
90 << Options["Threads"] << " " << token << " current perft";
94 else if (token == "key")
95 sync_cout << hex << uppercase << setfill('0')
96 << "position key: " << setw(16) << pos.key()
97 << "\nmaterial key: " << setw(16) << pos.material_key()
98 << "\npawn key: " << setw(16) << pos.pawn_key()
101 else if (token == "uci")
102 sync_cout << "id name " << engine_info(true)
104 << "\nuciok" << sync_endl;
106 else if (token == "ucinewgame") TT.clear();
107 else if (token == "go") go(pos, is);
108 else if (token == "position") position(pos, is);
109 else if (token == "setoption") setoption(is);
110 else if (token == "flip") pos.flip();
111 else if (token == "bench") benchmark(pos, is);
112 else if (token == "d") sync_cout << pos.pretty() << sync_endl;
113 else if (token == "isready") sync_cout << "readyok" << sync_endl;
114 else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
116 sync_cout << "Unknown command: " << cmd << sync_endl;
118 } while (token != "quit" && args.empty()); // Args have one-shot behaviour
120 Threads.wait_for_think_finished(); // Cannot quit while search is running
126 // position() is called when engine receives the "position" UCI command.
127 // The function sets up the position described in the given fen string ("fen")
128 // or the starting position ("startpos") and then makes the moves given in the
129 // following move list ("moves").
131 void position(Position& pos, istringstream& is) {
138 if (token == "startpos")
141 is >> token; // Consume "moves" token if any
143 else if (token == "fen")
144 while (is >> token && token != "moves")
149 pos.set(fen, Options["UCI_Chess960"], Threads.main_thread());
150 SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
152 // Parse move list (if any)
153 while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
155 SetupStates->push(StateInfo());
156 pos.do_move(m, SetupStates->top());
161 // setoption() is called when engine receives the "setoption" UCI command. The
162 // function updates the UCI option ("name") to the given value ("value").
164 void setoption(istringstream& is) {
166 string token, name, value;
168 is >> token; // Consume "name" token
170 // Read option name (can contain spaces)
171 while (is >> token && token != "value")
172 name += string(" ", !name.empty()) + token;
174 // Read option value (can contain spaces)
176 value += string(" ", !value.empty()) + token;
178 if (Options.count(name))
179 Options[name] = value;
181 sync_cout << "No such option: " << name << sync_endl;
185 // go() is called when engine receives the "go" UCI command. The function sets
186 // the thinking time and other parameters from the input string, and starts
189 void go(const Position& pos, istringstream& is) {
191 Search::LimitsType limits;
192 vector<Move> searchMoves;
197 if (token == "searchmoves")
199 searchMoves.push_back(move_from_uci(pos, token));
201 else if (token == "wtime") is >> limits.time[WHITE];
202 else if (token == "btime") is >> limits.time[BLACK];
203 else if (token == "winc") is >> limits.inc[WHITE];
204 else if (token == "binc") is >> limits.inc[BLACK];
205 else if (token == "movestogo") is >> limits.movestogo;
206 else if (token == "depth") is >> limits.depth;
207 else if (token == "nodes") is >> limits.nodes;
208 else if (token == "movetime") is >> limits.movetime;
209 else if (token == "mate") is >> limits.mate;
210 else if (token == "infinite") limits.infinite = true;
211 else if (token == "ponder") limits.ponder = true;
214 Threads.start_thinking(pos, limits, searchMoves, SetupStates);