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