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