]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Fix variable naming in prototypes at uci.cpp
[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
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29
30 #include "evaluate.h"
31 #include "misc.h"
32 #include "move.h"
33 #include "movegen.h"
34 #include "position.h"
35 #include "san.h"
36 #include "search.h"
37 #include "ucioption.h"
38
39 using namespace std;
40
41
42 namespace {
43
44   // FEN string for the initial position
45   const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
46
47   // UCIParser is a class for parsing UCI input. The class
48   // is actually a string stream built on a given input string.
49   typedef istringstream UCIParser;
50
51   // Local functions
52   void set_option(UCIParser& up);
53   void set_position(Position& pos, UCIParser& up);
54   bool go(Position& pos, UCIParser& up);
55   void perft(Position& pos, UCIParser& up);
56 }
57
58
59 /// execute_uci_command() takes a string as input, uses a UCIParser
60 /// object to parse this text string as a UCI command, and calls
61 /// the appropriate functions. In addition to the UCI commands,
62 /// the function also supports a few debug commands.
63
64 bool execute_uci_command(const string& cmd) {
65
66   static Position pos(StartPositionFEN, false, 0); // The root position
67   UCIParser up(cmd);
68   string token;
69
70   if (!(up >> token)) // operator>>() skips any whitespace
71       return true;
72
73   if (token == "quit")
74       return false;
75
76   if (token == "go")
77       return go(pos, up);
78
79   if (token == "uci")
80   {
81       cout << "id name " << engine_name()
82            << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
83       print_uci_options();
84       cout << "uciok" << endl;
85   }
86   else if (token == "ucinewgame")
87       pos.from_fen(StartPositionFEN, false);
88
89   else if (token == "isready")
90       cout << "readyok" << endl;
91
92   else if (token == "position")
93       set_position(pos, up);
94
95   else if (token == "setoption")
96       set_option(up);
97
98   // The remaining commands are for debugging purposes only
99   else if (token == "d")
100       pos.print();
101
102   else if (token == "flip")
103   {
104       Position p(pos, pos.thread());
105       pos.flipped_copy(p);
106   }
107   else if (token == "eval")
108   {
109       Value evalMargin;
110       cout << "Incremental mg: "   << mg_value(pos.value())
111            << "\nIncremental eg: " << eg_value(pos.value())
112            << "\nFull eval: "      << evaluate(pos, evalMargin) << endl;
113   }
114   else if (token == "key")
115       cout << "key: " << hex << pos.get_key()
116            << "\nmaterial key: " << pos.get_material_key()
117            << "\npawn key: " << pos.get_pawn_key() << endl;
118
119   else if (token == "perft")
120       perft(pos, up);
121
122   else
123       cout << "Unknown command: " << cmd << endl;
124
125   return true;
126 }
127
128
129 ////
130 //// Local functions
131 ////
132
133 namespace {
134
135   // set_position() is called when Stockfish receives the "position" UCI
136   // command. The input parameter is a UCIParser. It is assumed
137   // that this parser has consumed the first token of the UCI command
138   // ("position"), and is ready to read the second token ("startpos"
139   // or "fen", if the input is well-formed).
140
141   void set_position(Position& pos, UCIParser& up) {
142
143     string token;
144
145     if (!(up >> token) || (token != "startpos" && token != "fen"))
146         return;
147
148     if (token == "startpos")
149     {
150         pos.from_fen(StartPositionFEN, false);
151         if (!(up >> token))
152             return;
153     }
154     else // fen
155     {
156         string fen;
157         while (up >> token && token != "moves")
158         {
159             fen += token;
160             fen += ' ';
161         }
162         pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
163     }
164
165     if (token != "moves")
166         return;
167
168     // Parse optional move list
169     Move move;
170     StateInfo st;
171     while (up >> token)
172     {
173         move = move_from_uci(pos, token);
174         pos.do_move(move, st);
175         if (pos.rule_50_counter() == 0)
176             pos.reset_game_ply();
177
178         pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
179     }
180     // Our StateInfo st is about going out of scope so copy
181     // its content inside pos before it disappears.
182     pos.detach();
183   }
184
185
186   // set_option() is called when Stockfish receives the "setoption" UCI
187   // command. The input parameter is a UCIParser. It is assumed
188   // that this parser has consumed the first token of the UCI command
189   // ("setoption"), and is ready to read the second token ("name", if
190   // the input is well-formed).
191
192   void set_option(UCIParser& up) {
193
194     string token, name, value;
195
196     if (!(up >> token) || token != "name") // operator>>() skips any whitespace
197         return;
198
199     if (!(up >> name))
200         return;
201
202     // Handle names with included spaces
203     while (up >> token && token != "value")
204         name += (" " + token);
205
206     if (Options.find(name) == Options.end())
207     {
208         cout << "No such option: " << name << endl;
209         return;
210     }
211
212     // Is a button ?
213     if (token != "value")
214     {
215         Options[name].set_value("true");
216         return;
217     }
218
219     if (!(up >> value))
220         return;
221
222     // Handle values with included spaces
223     while (up >> token)
224         value += (" " + token);
225
226     Options[name].set_value(value);
227   }
228
229
230   // go() is called when Stockfish receives the "go" UCI command. The
231   // input parameter is a UCIParser. It is assumed that this
232   // parser has consumed the first token of the UCI command ("go"),
233   // and is ready to read the second token. The function sets the
234   // thinking time and other parameters from the input string, and
235   // calls think() (defined in search.cpp) with the appropriate
236   // parameters. Returns false if a quit command is received while
237   // thinking, returns true otherwise.
238
239   bool go(Position& pos, UCIParser& up) {
240
241     string token;
242
243     int time[2] = {0, 0}, inc[2] = {0, 0};
244     int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
245     bool infinite = false, ponder = false;
246     Move searchMoves[MOVES_MAX];
247
248     searchMoves[0] = MOVE_NONE;
249
250     while (up >> token)
251     {
252         if (token == "infinite")
253             infinite = true;
254         else if (token == "ponder")
255             ponder = true;
256         else if (token == "wtime")
257             up >> time[0];
258         else if (token == "btime")
259             up >> time[1];
260         else if (token == "winc")
261             up >> inc[0];
262         else if (token == "binc")
263             up >> inc[1];
264         else if (token == "movestogo")
265             up >> movesToGo;
266         else if (token == "depth")
267             up >> depth;
268         else if (token == "nodes")
269             up >> nodes;
270         else if (token == "movetime")
271             up >> moveTime;
272         else if (token == "searchmoves")
273         {
274             int numOfMoves = 0;
275             while (up >> token)
276                 searchMoves[numOfMoves++] = move_from_uci(pos, token);
277
278             searchMoves[numOfMoves] = MOVE_NONE;
279         }
280     }
281
282     assert(pos.is_ok());
283
284     return think(pos, infinite, ponder, time, inc, movesToGo,
285                  depth, nodes, moveTime, searchMoves);
286   }
287
288   void perft(Position& pos, UCIParser& up) {
289
290     int depth, tm;
291     int64_t n;
292
293     if (!(up >> depth))
294         return;
295
296     tm = get_system_time();
297
298     n = perft(pos, depth * ONE_PLY);
299
300     tm = get_system_time() - tm;
301     std::cout << "\nNodes " << n
302               << "\nTime (ms) " << tm
303               << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;
304   }
305 }