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