]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Introduce struct Mutex and ConditionVariable
[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 "notation.h"
26 #include "position.h"
27 #include "search.h"
28 #include "thread.h"
29 #include "tt.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). This is needed by draw detection
43   // where, due to 50 moves rule, we need to check at most 100 plies back.
44   StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
45
46   void set_option(istringstream& up);
47   void set_position(Position& pos, istringstream& up);
48   void go(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   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.main_thread()->wake_up(); // Could be sleeping
91           }
92       }
93
94       else if (token == "go")
95           go(pos, is);
96
97       else if (token == "ucinewgame")
98           TT.clear();
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 == "d")
110           pos.print();
111
112       else if (token == "flip")
113           pos.flip();
114
115       else if (token == "eval")
116           cout << Eval::trace(pos) << endl;
117
118       else if (token == "bench")
119           benchmark(pos, is);
120
121       else if (token == "key")
122           cout << "key: " << hex     << pos.key()
123                << "\nmaterial key: " << pos.material_key()
124                << "\npawn key: "     << pos.pawn_key() << endl;
125
126       else if (token == "uci")
127           cout << "id name "     << engine_info(true)
128                << "\n"           << Options
129                << "\nuciok"      << endl;
130
131       else if (token == "perft" && (is >> token)) // Read depth
132       {
133           stringstream ss;
134
135           ss << Options["Hash"]    << " "
136              << Options["Threads"] << " " << token << " current perft";
137
138           benchmark(pos, ss);
139       }
140
141       else
142           cout << "Unknown command: " << cmd << endl;
143
144       if (!args.empty()) // Command line arguments have one-shot behaviour
145       {
146           Threads.wait_for_search_finished();
147           break;
148       }
149   }
150 }
151
152
153 namespace {
154
155   // set_position() is called when engine receives the "position" UCI
156   // command. The function sets up the position described in the given
157   // fen string ("fen") or the starting position ("startpos") and then
158   // makes the moves given in the following move list ("moves").
159
160   void set_position(Position& pos, istringstream& is) {
161
162     Move m;
163     string token, fen;
164
165     is >> token;
166
167     if (token == "startpos")
168     {
169         fen = StartFEN;
170         is >> token; // Consume "moves" token if any
171     }
172     else if (token == "fen")
173         while (is >> token && token != "moves")
174             fen += token + " ";
175     else
176         return;
177
178     pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
179
180     // Parse move list (if any)
181     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
182     {
183         pos.do_move(m, *SetupState);
184
185         // Increment pointer to StateRingBuf circular buffer
186         if (++SetupState - StateRingBuf >= 102)
187             SetupState = StateRingBuf;
188     }
189   }
190
191
192   // set_option() is called when engine receives the "setoption" UCI command. The
193   // function updates the UCI option ("name") to the given value ("value").
194
195   void set_option(istringstream& is) {
196
197     string token, name, value;
198
199     is >> token; // Consume "name" token
200
201     // Read option name (can contain spaces)
202     while (is >> token && token != "value")
203         name += string(" ", !name.empty()) + token;
204
205     // Read option value (can contain spaces)
206     while (is >> token)
207         value += string(" ", !value.empty()) + token;
208
209     if (Options.count(name))
210         Options[name] = value;
211     else
212         cout << "No such option: " << name << endl;
213   }
214
215
216   // go() is called when engine receives the "go" UCI command. The function sets
217   // the thinking time and other parameters from the input string, and then starts
218   // the search.
219
220   void go(Position& pos, istringstream& is) {
221
222     Search::LimitsType limits;
223     vector<Move> searchMoves;
224     string token;
225
226     while (is >> token)
227     {
228         if (token == "wtime")
229             is >> limits.time[WHITE];
230         else if (token == "btime")
231             is >> limits.time[BLACK];
232         else if (token == "winc")
233             is >> limits.inc[WHITE];
234         else if (token == "binc")
235             is >> limits.inc[BLACK];
236         else if (token == "movestogo")
237             is >> limits.movestogo;
238         else if (token == "depth")
239             is >> limits.depth;
240         else if (token == "nodes")
241             is >> limits.nodes;
242         else if (token == "movetime")
243             is >> limits.movetime;
244         else if (token == "infinite")
245             limits.infinite = true;
246         else if (token == "ponder")
247             limits.ponder = true;
248         else if (token == "searchmoves")
249             while (is >> token)
250                 searchMoves.push_back(move_from_uci(pos, token));
251     }
252
253     Threads.start_searching(pos, limits, searchMoves);
254   }
255 }