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