]> git.sesse.net Git - stockfish/blob - src/uci.cpp
caf1b5c2fed08d67084b09c584ba1c7f4c7716c1
[stockfish] / src / uci.cpp
1 /*
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
5
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.
10
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.
15
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/>.
18 */
19
20 #include <iomanip>
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24
25 #include "evaluate.h"
26 #include "notation.h"
27 #include "position.h"
28 #include "search.h"
29 #include "thread.h"
30 #include "ucioption.h"
31
32 using namespace std;
33
34 extern void benchmark(const Position& pos, istream& is);
35
36 namespace {
37
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";
40
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;
44
45   void setoption(istringstream& up);
46   void position(Position& pos, istringstream& up);
47   void go(const Position& pos, istringstream& up);
48 }
49
50
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.
55
56 void UCI::loop(const string& args) {
57
58   Position pos(StartFEN, false, Threads.main()); // The root position
59   string token, cmd = args;
60
61   do {
62       if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
63           cmd = "quit";
64
65       istringstream is(cmd);
66
67       is >> skipws >> token;
68
69       if (token == "quit" || token == "stop" || token == "ponderhit")
70       {
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)
77           {
78               Search::Signals.stop = true;
79               Threads.main()->notify_one(); // Could be sleeping
80           }
81           else
82               Search::Limits.ponder = false;
83       }
84       else if (token == "perft" && (is >> token)) // Read perft depth
85       {
86           stringstream ss;
87
88           ss << Options["Hash"]    << " "
89              << Options["Threads"] << " " << token << " current perft";
90
91           benchmark(pos, ss);
92       }
93       else if (token == "key")
94           sync_cout << hex << uppercase << setfill('0')
95                     << "position key: "   << setw(16) << pos.key()
96                     << "\nmaterial key: " << setw(16) << pos.material_key()
97                     << "\npawn key:     " << setw(16) << pos.pawn_key()
98                     << dec << sync_endl;
99
100       else if (token == "uci")
101           sync_cout << "id name " << engine_info(true)
102                     << "\n"       << Options
103                     << "\nuciok"  << sync_endl;
104
105       else if (token == "eval")
106       {
107           Search::RootColor = pos.side_to_move(); // Ensure it is set
108           sync_cout << Eval::trace(pos) << sync_endl;
109       }
110       else if (token == "ucinewgame") { /* Avoid returning "Unknown command" */ }
111       else if (token == "go")         go(pos, is);
112       else if (token == "position")   position(pos, is);
113       else if (token == "setoption")  setoption(is);
114       else if (token == "flip")       pos.flip();
115       else if (token == "bench")      benchmark(pos, is);
116       else if (token == "d")          sync_cout << pos.pretty() << sync_endl;
117       else if (token == "isready")    sync_cout << "readyok" << sync_endl;
118       else
119           sync_cout << "Unknown command: " << cmd << sync_endl;
120
121   } while (token != "quit" && args.empty()); // Args have one-shot behaviour
122
123   Threads.wait_for_think_finished(); // Cannot quit while search is running
124 }
125
126
127 namespace {
128
129   // position() is called when engine receives the "position" UCI command.
130   // The function sets up the position described in the given fen string ("fen")
131   // or the starting position ("startpos") and then makes the moves given in the
132   // following move list ("moves").
133
134   void position(Position& pos, istringstream& is) {
135
136     Move m;
137     string token, fen;
138
139     is >> token;
140
141     if (token == "startpos")
142     {
143         fen = StartFEN;
144         is >> token; // Consume "moves" token if any
145     }
146     else if (token == "fen")
147         while (is >> token && token != "moves")
148             fen += token + " ";
149     else
150         return;
151
152     pos.set(fen, Options["UCI_Chess960"], Threads.main());
153     SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
154
155     // Parse move list (if any)
156     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
157     {
158         SetupStates->push(StateInfo());
159         pos.do_move(m, SetupStates->top());
160     }
161   }
162
163
164   // setoption() is called when engine receives the "setoption" UCI command. The
165   // function updates the UCI option ("name") to the given value ("value").
166
167   void setoption(istringstream& is) {
168
169     string token, name, value;
170
171     is >> token; // Consume "name" token
172
173     // Read option name (can contain spaces)
174     while (is >> token && token != "value")
175         name += string(" ", !name.empty()) + token;
176
177     // Read option value (can contain spaces)
178     while (is >> token)
179         value += string(" ", !value.empty()) + token;
180
181     if (Options.count(name))
182         Options[name] = value;
183     else
184         sync_cout << "No such option: " << name << sync_endl;
185   }
186
187
188   // go() is called when engine receives the "go" UCI command. The function sets
189   // the thinking time and other parameters from the input string, and starts
190   // the search.
191
192   void go(const Position& pos, istringstream& is) {
193
194     Search::LimitsType limits;
195     vector<Move> searchMoves;
196     string token;
197
198     while (is >> token)
199     {
200         if (token == "searchmoves")
201             while (is >> token)
202                 searchMoves.push_back(move_from_uci(pos, token));
203
204         else if (token == "wtime")     is >> limits.time[WHITE];
205         else if (token == "btime")     is >> limits.time[BLACK];
206         else if (token == "winc")      is >> limits.inc[WHITE];
207         else if (token == "binc")      is >> limits.inc[BLACK];
208         else if (token == "movestogo") is >> limits.movestogo;
209         else if (token == "depth")     is >> limits.depth;
210         else if (token == "nodes")     is >> limits.nodes;
211         else if (token == "movetime")  is >> limits.movetime;
212         else if (token == "mate")      is >> limits.mate;
213         else if (token == "infinite")  limits.infinite = true;
214         else if (token == "ponder")    limits.ponder = true;
215     }
216
217     Threads.start_thinking(pos, limits, searchMoves, SetupStates);
218   }
219 }