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