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