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