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