]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Move Min() and Max() macros to types.h
[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 <cctype>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30
31 #include "evaluate.h"
32 #include "misc.h"
33 #include "move.h"
34 #include "movegen.h"
35 #include "position.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   Value dummy;
69   string token;
70
71   up >> token; // operator>>() skips any whitespace
72
73   if (token == "quit")
74       return false;
75
76   else if (token == "go")
77       return go(pos, up);
78
79   else if (token == "uci")
80       cout << "id name " << engine_name()
81            << "\nid author " << engine_author()
82            << "\n" << options_to_uci()
83            << "\nuciok" << endl;
84
85   else if (token == "ucinewgame")
86       pos.from_fen(StartPositionFEN, false);
87
88   else if (token == "isready")
89       cout << "readyok" << endl;
90
91   else if (token == "position")
92       set_position(pos, up);
93
94   else if (token == "setoption")
95       set_option(up);
96
97   else if (token == "d")
98       pos.print();
99
100   else if (token == "eval")
101       cout << "Incremental mg: "   << mg_value(pos.value())
102            << "\nIncremental eg: " << eg_value(pos.value())
103            << "\nFull eval: "      << evaluate(pos, dummy) << endl;
104
105   else if (token == "key")
106       cout << "key: " << hex     << pos.get_key()
107            << "\nmaterial key: " << pos.get_material_key()
108            << "\npawn key: "     << pos.get_pawn_key() << endl;
109
110   else if (token == "perft")
111       perft(pos, up);
112
113   else if (token == "flip")
114   {
115       Position p(pos, pos.thread());
116       pos.flipped_copy(p);
117   }
118   else
119       cout << "Unknown command: " << cmd << endl;
120
121   return true;
122 }
123
124
125 ////
126 //// Local functions
127 ////
128
129 namespace {
130
131   // set_position() is called when Stockfish receives the "position" UCI
132   // command. The input parameter is a UCIParser. It is assumed
133   // that this parser has consumed the first token of the UCI command
134   // ("position"), and is ready to read the second token ("startpos"
135   // or "fen", if the input is well-formed).
136
137   void set_position(Position& pos, UCIParser& up) {
138
139     string fen, token;
140
141     up >> token; // operator>>() skips any whitespace
142
143     if (token == "startpos")
144     {
145         pos.from_fen(StartPositionFEN, false);
146         up >> token; // Consume "moves" token
147     }
148     else if (token == "fen")
149     {
150         while (up >> token && token != "moves")
151             fen += token + " ";
152
153         pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
154     }
155     else return;
156
157     // Parse move list (if any)
158     while (up >> token)
159         pos.do_setup_move(move_from_uci(pos, token));
160   }
161
162
163   // set_option() is called when Stockfish receives the "setoption" UCI
164   // command. The input parameter is a UCIParser. It is assumed
165   // that this parser has consumed the first token of the UCI command
166   // ("setoption"), and is ready to read the second token ("name", if
167   // the input is well-formed).
168
169   void set_option(UCIParser& up) {
170
171     string value = "true"; // UCI buttons don't have a "value" field
172     string token, name;
173
174     up >> token; // Consume "name" token
175     up >> name;  // Read option name
176
177     // Handle names with included spaces
178     while (up >> token && token != "value")
179         name += " " + token;
180
181     up >> value; // Read option value
182
183     // Handle values with included spaces
184     while (up >> token)
185         value += " " + token;
186
187     if (Options.find(name) != Options.end())
188         Options[name].set_value(value);
189     else
190         cout << "No such option: " << name << endl;
191   }
192
193
194   // go() is called when Stockfish receives the "go" UCI command. The
195   // input parameter is a UCIParser. It is assumed that this
196   // parser has consumed the first token of the UCI command ("go"),
197   // and is ready to read the second token. The function sets the
198   // thinking time and other parameters from the input string, and
199   // calls think() (defined in search.cpp) with the appropriate
200   // parameters. Returns false if a quit command is received while
201   // thinking, returns true otherwise.
202
203   bool go(Position& pos, UCIParser& up) {
204
205     string token;
206     Move searchMoves[MOVES_MAX];
207     int movesToGo, depth, nodes, moveTime, numOfMoves;
208     bool infinite, ponder;
209     int time[2] = {0, 0}, inc[2] = {0, 0};
210
211     searchMoves[0] = MOVE_NONE;
212     infinite = ponder = false;
213     movesToGo = depth = nodes = moveTime = numOfMoves = 0;
214
215     while (up >> token)
216     {
217         if (token == "infinite")
218             infinite = true;
219         else if (token == "ponder")
220             ponder = true;
221         else if (token == "wtime")
222             up >> time[0];
223         else if (token == "btime")
224             up >> time[1];
225         else if (token == "winc")
226             up >> inc[0];
227         else if (token == "binc")
228             up >> inc[1];
229         else if (token == "movestogo")
230             up >> movesToGo;
231         else if (token == "depth")
232             up >> depth;
233         else if (token == "nodes")
234             up >> nodes;
235         else if (token == "movetime")
236             up >> moveTime;
237         else if (token == "searchmoves")
238         {
239             while (up >> token)
240                 searchMoves[numOfMoves++] = move_from_uci(pos, token);
241
242             searchMoves[numOfMoves] = MOVE_NONE;
243         }
244     }
245
246     assert(pos.is_ok());
247
248     return think(pos, infinite, ponder, time, inc, movesToGo,
249                  depth, nodes, moveTime, searchMoves);
250   }
251
252   void perft(Position& pos, UCIParser& up) {
253
254     int depth, tm;
255     int64_t n;
256
257     if (!(up >> depth))
258         return;
259
260     tm = get_system_time();
261
262     n = perft(pos, depth * ONE_PLY);
263
264     tm = get_system_time() - tm;
265     std::cout << "\nNodes " << n
266               << "\nTime (ms) " << tm
267               << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;
268   }
269 }