]> git.sesse.net Git - stockfish/blob - src/uci.cpp
3895c7a1d49aa001535430616d97c58920179697
[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 "ucioption.h"
30
31 using namespace std;
32
33 extern void benchmark(const Position& pos, istream& 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). Needed by repetition draw detection.
42   Search::StateStackPtr SetupStates;
43
44   void set_option(istringstream& up);
45   void set_position(Position& pos, istringstream& up);
46   void go(Position& pos, istringstream& up);
47 }
48
49
50 /// Wait for a command from the user, parse this text string as an UCI command,
51 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
52 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
53 /// commands, the function also supports a few debug commands.
54
55 void UCI::loop(const string& args) {
56
57   Position pos(StartFEN, false, Threads.main_thread()); // The root position
58   string cmd, token;
59
60   while (token != "quit")
61   {
62       if (!args.empty())
63           cmd = args;
64
65       else if (!getline(cin, cmd)) // Block here waiting for input
66           cmd = "quit";
67
68       istringstream is(cmd);
69
70       is >> skipws >> token;
71
72       if (token == "quit" || token == "stop")
73       {
74           Search::Signals.stop = true;
75           Threads.wait_for_search_finished(); // Cannot quit while threads are running
76       }
77
78       else if (token == "ponderhit")
79       {
80           // The opponent has played the expected move. GUI sends "ponderhit" if
81           // we were told to ponder on the same move the opponent has played. We
82           // should continue searching but switching from pondering to normal search.
83           Search::Limits.ponder = false;
84
85           if (Search::Signals.stopOnPonderhit)
86           {
87               Search::Signals.stop = true;
88               Threads.main_thread()->wake_up(); // Could be sleeping
89           }
90       }
91
92       else if (token == "go")
93           go(pos, is);
94
95       else if (token == "ucinewgame")
96       { /* Avoid returning "Unknown command" */ }
97
98       else if (token == "isready")
99           sync_cout << "readyok" << sync_endl;
100
101       else if (token == "position")
102           set_position(pos, is);
103
104       else if (token == "setoption")
105           set_option(is);
106
107       else if (token == "d")
108           pos.print();
109
110       else if (token == "flip")
111           pos.flip();
112
113       else if (token == "eval")
114           sync_cout << Eval::trace(pos) << sync_endl;
115
116       else if (token == "bench")
117           benchmark(pos, is);
118
119       else if (token == "key")
120           sync_cout << "key: " << hex     << pos.key()
121                     << "\nmaterial key: " << pos.material_key()
122                     << "\npawn key: "     << pos.pawn_key() << sync_endl;
123
124       else if (token == "uci")
125           sync_cout << "id name " << engine_info(true)
126                     << "\n"       << Options
127                     << "\nuciok"  << sync_endl;
128
129       else if (token == "perft" && (is >> token)) // Read depth
130       {
131           stringstream ss;
132
133           ss << Options["Hash"]    << " "
134              << Options["Threads"] << " " << token << " current perft";
135
136           benchmark(pos, ss);
137       }
138
139       else
140           sync_cout << "Unknown command: " << cmd << sync_endl;
141
142       if (!args.empty()) // Command line arguments have one-shot behaviour
143       {
144           Threads.wait_for_search_finished();
145           break;
146       }
147   }
148 }
149
150
151 namespace {
152
153   // set_position() is called when engine receives the "position" UCI command.
154   // The function sets up the position described in the given fen string ("fen")
155   // or the starting position ("startpos") and then makes the moves given in the
156   // following move list ("moves").
157
158   void set_position(Position& pos, istringstream& is) {
159
160     Move m;
161     string token, fen;
162
163     is >> token;
164
165     if (token == "startpos")
166     {
167         fen = StartFEN;
168         is >> token; // Consume "moves" token if any
169     }
170     else if (token == "fen")
171         while (is >> token && token != "moves")
172             fen += token + " ";
173     else
174         return;
175
176     pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
177     SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
178
179     // Parse move list (if any)
180     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
181     {
182         SetupStates->push(StateInfo());
183         pos.do_move(m, SetupStates->top());
184     }
185   }
186
187
188   // set_option() is called when engine receives the "setoption" UCI command. The
189   // function updates the UCI option ("name") to the given value ("value").
190
191   void set_option(istringstream& is) {
192
193     string token, name, value;
194
195     is >> token; // Consume "name" token
196
197     // Read option name (can contain spaces)
198     while (is >> token && token != "value")
199         name += string(" ", !name.empty()) + token;
200
201     // Read option value (can contain spaces)
202     while (is >> token)
203         value += string(" ", !value.empty()) + token;
204
205     if (Options.count(name))
206         Options[name] = value;
207     else
208         sync_cout << "No such option: " << name << sync_endl;
209   }
210
211
212   // go() is called when engine receives the "go" UCI command. The function sets
213   // the thinking time and other parameters from the input string, and then starts
214   // the search.
215
216   void go(Position& pos, istringstream& is) {
217
218     Search::LimitsType limits;
219     vector<Move> searchMoves;
220     string token;
221
222     while (is >> token)
223     {
224         if (token == "wtime")
225             is >> limits.time[WHITE];
226         else if (token == "btime")
227             is >> limits.time[BLACK];
228         else if (token == "winc")
229             is >> limits.inc[WHITE];
230         else if (token == "binc")
231             is >> limits.inc[BLACK];
232         else if (token == "movestogo")
233             is >> limits.movestogo;
234         else if (token == "depth")
235             is >> limits.depth;
236         else if (token == "nodes")
237             is >> limits.nodes;
238         else if (token == "movetime")
239             is >> limits.movetime;
240         else if (token == "infinite")
241             limits.infinite = true;
242         else if (token == "ponder")
243             limits.ponder = true;
244         else if (token == "searchmoves")
245             while (is >> token)
246                 searchMoves.push_back(move_from_uci(pos, token));
247     }
248
249     Threads.start_searching(pos, limits, searchMoves, SetupStates);
250   }
251 }