]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Stockfish 2.2.2
[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 #include <vector>
24
25 #include "evaluate.h"
26 #include "misc.h"
27 #include "position.h"
28 #include "search.h"
29 #include "thread.h"
30 #include "ucioption.h"
31
32 using namespace std;
33
34 namespace {
35
36   // FEN string of the initial position, normal chess
37   const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
38
39   // Keep track of position keys along the setup moves (from start position to the
40   // position just before to start searching). This is needed by draw detection
41   // where, due to 50 moves rule, we need to check at most 100 plies back.
42   StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
43
44   void set_option(istringstream& up);
45   void set_position(Position& pos, istringstream& up);
46   void go(Position& pos, istringstream& up);
47   void perft(Position& pos, istringstream& up);
48 }
49
50
51 /// Wait for a command from the user, parse this text string as an UCI command,
52 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
53 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
54 /// commands, the function also supports a few debug commands.
55
56 void uci_loop() {
57
58   Position pos(StartFEN, false, 0); // The root position
59   string cmd, token;
60
61   while (token != "quit")
62   {
63       if (!getline(cin, cmd)) // Block here waiting for input
64           cmd = "quit";
65
66       istringstream is(cmd);
67
68       is >> skipws >> token;
69
70       if (token == "quit" || token == "stop")
71           Threads.stop_thinking();
72
73       else if (token == "ponderhit")
74       {
75           // The opponent has played the expected move. GUI sends "ponderhit" if
76           // we were told to ponder on the same move the opponent has played. We
77           // should continue searching but switching from pondering to normal search.
78           Search::Limits.ponder = false;
79
80           if (Search::Signals.stopOnPonderhit)
81               Threads.stop_thinking();
82       }
83
84       else if (token == "go")
85           go(pos, is);
86
87       else if (token == "ucinewgame")
88           pos.from_fen(StartFEN, false);
89
90       else if (token == "isready")
91           cout << "readyok" << endl;
92
93       else if (token == "position")
94           set_position(pos, is);
95
96       else if (token == "setoption")
97           set_option(is);
98
99       else if (token == "perft")
100           perft(pos, is);
101
102       else if (token == "d")
103           pos.print();
104
105       else if (token == "flip")
106           pos.flip_me();
107
108       else if (token == "eval")
109       {
110           read_evaluation_uci_options(pos.side_to_move());
111           cout << trace_evaluate(pos) << endl;
112       }
113
114       else if (token == "key")
115           cout << "key: " << hex     << pos.key()
116                << "\nmaterial key: " << pos.material_key()
117                << "\npawn key: "     << pos.pawn_key() << endl;
118
119       else if (token == "uci")
120           cout << "id name "     << engine_info(true)
121                << "\n"           << Options
122                << "\nuciok"      << endl;
123       else
124           cout << "Unknown command: " << cmd << endl;
125   }
126 }
127
128
129 namespace {
130
131   // set_position() is called when engine receives the "position" UCI
132   // command. The function sets up the position described in the given
133   // fen string ("fen") or the starting position ("startpos") and then
134   // makes the moves given in the following move list ("moves").
135
136   void set_position(Position& pos, istringstream& is) {
137
138     Move m;
139     string token, fen;
140
141     is >> token;
142
143     if (token == "startpos")
144     {
145         fen = StartFEN;
146         is >> token; // Consume "moves" token if any
147     }
148     else if (token == "fen")
149         while (is >> token && token != "moves")
150             fen += token + " ";
151     else
152         return;
153
154     pos.from_fen(fen, Options["UCI_Chess960"]);
155
156     // Parse move list (if any)
157     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
158     {
159         pos.do_move(m, *SetupState);
160
161         // Increment pointer to StateRingBuf circular buffer
162         if (++SetupState - StateRingBuf >= 102)
163             SetupState = StateRingBuf;
164     }
165   }
166
167
168   // set_option() is called when engine receives the "setoption" UCI command. The
169   // function updates the UCI option ("name") to the given value ("value").
170
171   void set_option(istringstream& is) {
172
173     string token, name, value;
174
175     is >> token; // Consume "name" token
176
177     // Read option name (can contain spaces)
178     while (is >> token && token != "value")
179         name += string(" ", !name.empty()) + token;
180
181     // Read option value (can contain spaces)
182     while (is >> token)
183         value += string(" ", !value.empty()) + token;
184
185     if (!Options.count(name))
186         cout << "No such option: " << name << endl;
187
188     else if (value.empty()) // UCI buttons don't have a value
189         Options[name] = true;
190
191     else
192         Options[name] = value;
193   }
194
195
196   // go() is called when engine receives the "go" UCI command. The function sets
197   // the thinking time and other parameters from the input string, and then starts
198   // the main searching thread.
199
200   void go(Position& pos, istringstream& is) {
201
202     string token;
203     Search::LimitsType limits;
204     std::set<Move> searchMoves;
205     int time[] = { 0, 0 }, inc[] = { 0, 0 };
206
207     while (is >> token)
208     {
209         if (token == "infinite")
210             limits.infinite = true;
211         else if (token == "ponder")
212             limits.ponder = true;
213         else if (token == "wtime")
214             is >> time[WHITE];
215         else if (token == "btime")
216             is >> time[BLACK];
217         else if (token == "winc")
218             is >> inc[WHITE];
219         else if (token == "binc")
220             is >> inc[BLACK];
221         else if (token == "movestogo")
222             is >> limits.movesToGo;
223         else if (token == "depth")
224             is >> limits.maxDepth;
225         else if (token == "nodes")
226             is >> limits.maxNodes;
227         else if (token == "movetime")
228             is >> limits.maxTime;
229         else if (token == "searchmoves")
230             while (is >> token)
231                 searchMoves.insert(move_from_uci(pos, token));
232     }
233
234     limits.time = time[pos.side_to_move()];
235     limits.increment = inc[pos.side_to_move()];
236
237     Threads.start_thinking(pos, limits, searchMoves, true);
238   }
239
240
241   // perft() is called when engine receives the "perft" command. The function
242   // calls perft() with the required search depth then prints counted leaf nodes
243   // and elapsed time.
244
245   void perft(Position& pos, istringstream& is) {
246
247     int depth, time;
248
249     if (!(is >> depth))
250         return;
251
252     time = system_time();
253
254     int64_t n = Search::perft(pos, depth * ONE_PLY);
255
256     time = system_time() - time;
257
258     std::cout << "\nNodes " << n
259               << "\nTime (ms) " << time
260               << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;
261   }
262 }