]> git.sesse.net Git - stockfish/blob - src/uci.cpp
96aae854311245558d678b163ce25107ccce6aa9
[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-2012 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 <iostream>
21 #include <sstream>
22 #include <string>
23
24 #include "evaluate.h"
25 #include "misc.h"
26 #include "position.h"
27 #include "search.h"
28 #include "thread.h"
29 #include "ucioption.h"
30
31 using namespace std;
32
33 extern void benchmark(istringstream& is);
34
35 namespace {
36
37   // FEN string of the initial position, normal chess
38   const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
39
40   // Keep track of position keys along the setup moves (from start position to the
41   // position just before to start searching). This is needed by draw detection
42   // where, due to 50 moves rule, we need to check at most 100 plies back.
43   StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
44
45   void set_option(istringstream& up);
46   void set_position(Position& pos, istringstream& up);
47   void go(Position& pos, istringstream& up);
48   void perft(Position& pos, istringstream& up);
49 }
50
51
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.
56
57 void uci_loop(const string& args) {
58
59   Position pos(StartFEN, false, Threads.main_thread()); // The root position
60   string cmd, token;
61
62   Search::RootPosition = pos;
63
64   while (token != "quit")
65   {
66       if (!args.empty())
67           cmd = args;
68
69       else if (!getline(cin, cmd)) // Block here waiting for input
70           cmd = "quit";
71
72       istringstream is(cmd);
73
74       is >> skipws >> token;
75
76       if (token == "quit" || token == "stop")
77       {
78           Search::Signals.stop = true;
79           Threads.wait_for_search_finished(); // Cannot quit while threads are running
80       }
81
82       else if (token == "ponderhit")
83       {
84           // The opponent has played the expected move. GUI sends "ponderhit" if
85           // we were told to ponder on the same move the opponent has played. We
86           // should continue searching but switching from pondering to normal search.
87           Search::Limits.ponder = false;
88
89           if (Search::Signals.stopOnPonderhit)
90           {
91               Search::Signals.stop = true;
92               Threads.main_thread()->wake_up(); // Could be sleeping
93           }
94       }
95
96       else if (token == "go")
97           go(pos, is);
98
99       else if (token == "ucinewgame")
100       { /* Avoid returning "Unknown command" */ }
101
102       else if (token == "isready")
103           cout << "readyok" << endl;
104
105       else if (token == "position")
106           set_position(pos, is);
107
108       else if (token == "setoption")
109           set_option(is);
110
111       else if (token == "perft")
112           perft(pos, is);
113
114       else if (token == "d")
115           pos.print();
116
117       else if (token == "flip")
118           pos.flip();
119
120       else if (token == "eval")
121           cout << Eval::trace(pos) << endl;
122
123       else if (token == "bench")
124           benchmark(is);
125
126       else if (token == "key")
127           cout << "key: " << hex     << pos.key()
128                << "\nmaterial key: " << pos.material_key()
129                << "\npawn key: "     << pos.pawn_key() << endl;
130
131       else if (token == "uci")
132           cout << "id name "     << engine_info(true)
133                << "\n"           << Options
134                << "\nuciok"      << endl;
135       else
136           cout << "Unknown command: " << cmd << endl;
137
138       if (!args.empty()) // Command line arguments have one-shot behaviour
139       {
140           Threads.wait_for_search_finished();
141           break;
142       }
143   }
144 }
145
146
147 namespace {
148
149   // set_position() is called when engine receives the "position" UCI
150   // command. The function sets up the position described in the given
151   // fen string ("fen") or the starting position ("startpos") and then
152   // makes the moves given in the following move list ("moves").
153
154   void set_position(Position& pos, istringstream& is) {
155
156     Move m;
157     string token, fen;
158
159     is >> token;
160
161     if (token == "startpos")
162     {
163         fen = StartFEN;
164         is >> token; // Consume "moves" token if any
165     }
166     else if (token == "fen")
167         while (is >> token && token != "moves")
168             fen += token + " ";
169     else
170         return;
171
172     pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
173
174     // Parse move list (if any)
175     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
176     {
177         pos.do_move(m, *SetupState);
178
179         // Increment pointer to StateRingBuf circular buffer
180         if (++SetupState - StateRingBuf >= 102)
181             SetupState = StateRingBuf;
182     }
183   }
184
185
186   // set_option() is called when engine receives the "setoption" UCI command. The
187   // function updates the UCI option ("name") to the given value ("value").
188
189   void set_option(istringstream& is) {
190
191     string token, name, value;
192
193     is >> token; // Consume "name" token
194
195     // Read option name (can contain spaces)
196     while (is >> token && token != "value")
197         name += string(" ", !name.empty()) + token;
198
199     // Read option value (can contain spaces)
200     while (is >> token)
201         value += string(" ", !value.empty()) + token;
202
203     if (Options.count(name))
204         Options[name] = value;
205     else
206         cout << "No such option: " << name << endl;
207   }
208
209
210   // go() is called when engine receives the "go" UCI command. The function sets
211   // the thinking time and other parameters from the input string, and then starts
212   // the search.
213
214   void go(Position& pos, istringstream& is) {
215
216     Search::LimitsType limits;
217     vector<Move> searchMoves;
218     string token;
219
220     while (is >> token)
221     {
222         if (token == "wtime")
223             is >> limits.time[WHITE];
224         else if (token == "btime")
225             is >> limits.time[BLACK];
226         else if (token == "winc")
227             is >> limits.inc[WHITE];
228         else if (token == "binc")
229             is >> limits.inc[BLACK];
230         else if (token == "movestogo")
231             is >> limits.movestogo;
232         else if (token == "depth")
233             is >> limits.depth;
234         else if (token == "nodes")
235             is >> limits.nodes;
236         else if (token == "movetime")
237             is >> limits.movetime;
238         else if (token == "infinite")
239             limits.infinite = true;
240         else if (token == "ponder")
241             limits.ponder = true;
242         else if (token == "searchmoves")
243             while (is >> token)
244                 searchMoves.push_back(move_from_uci(pos, token));
245     }
246
247     Threads.start_searching(pos, limits, searchMoves);
248   }
249
250
251   // perft() is called when engine receives the "perft" command. The function
252   // calls perft() with the required search depth then prints counted leaf nodes
253   // and elapsed time.
254
255   void perft(Position& pos, istringstream& is) {
256
257     int depth;
258
259     if (!(is >> depth))
260         return;
261
262     Time time = Time::current_time();
263
264     int64_t n = Search::perft(pos, depth * ONE_PLY);
265
266     int e = time.elapsed();
267
268     cout << "\nNodes " << n
269          << "\nTime (ms) " << e
270          << "\nNodes/second " << int(n / (e / 1000.0)) << endl;
271   }
272 }