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