[go: up one dir, main page]

Menu

[r225]: / tags / 0.2.5 / Uci.cpp  Maximize  Restore  History

Download this file

384 lines (329 with data), 9.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <iostream>
#include <sstream>
#include <string.h>
#include <set>
#include "Uci.h"
#include "DebugTest.h"
#include "DebugBench.h"
#include "Move.h"
using namespace std;
Uci::Uci()
{
//engine = eng;
if(ChessEngine.debugMode)
cout << ChessEngine.GetEngineInfo() ;
}
Uci::~Uci(void)
{
}
int Uci::loop(){
string cmd, token;
while (token != "quit")
{
//Threads.DisplayStatus();
if (!getline(cin, cmd)) // Block here waiting for input
cmd = "quit";
istringstream is(cmd);
is >> skipws >> token;
if (token == "quit" || token == "stop")
{
ChessEngine.Stop();
}
else if (token == "ponderhit")
{
// // TODO: implement PonderMode
//
// // The opponent has played the expected move. GUI sends "ponderhit" if
// // we were told to ponder on the same move the opponent has played. We
// // should continue searching but switching from pondering to normal search.
// //Search::Limits.ponder = false;
// //ChessEngine.SetPonderLimit(false);
// // Search::Signals.stopOnPonderhit)
// //if (ChessEngine.IsStopOnPonderHit())
// //ChessEngine.StopThinking();
// //ChessEngine.Stop();
}
else if (token == "go") {
go(is);
}
else if (token == "ucinewgame"){
//pos.from_fen(StartFEN, false);
ChessEngine.SetStartPosition();
}
else if (token == "isready")
{
cout << "readyok" << endl;
}
else if (token == "position")
{
set_position(is);
}
else if (token == "setoption")
{
set_option(is);
}
/*
// NOT PART OF UCI PROTOCOL V.2004
else if (token == "perft")
{
perft(pos, is);
}
*/
else if (token == "debug")
{
string value;
is >> skipws >> value;
ChessEngine.SetDebug((value=="on"));
}
else if (token == "flip")
{
// // TODO: implement this method
// int toBeImplemented=1;
// //pos.flip_me();
// //ChessEngine.FlipBoard();
}
/*
// NOT PART OF UCI PROTOCOL V.2004
else if (token == "eval")
{
// // TODO: implement this method
// int toBeImplemented=1;
// //read_evaluation_uci_options(pos.side_to_move());
// //ChessEngine.ReadEvaluation();
//
// //cout << trace_evaluate(pos) << endl;
// //ChessEngine.TraceEvaluate();
}
*/
/*
// NOT PART OF UCI PROTOCOL V.2004
else if (token == "key")
{
// cout << "key: " << hex << pos.key()
// << "\nmaterial key: " << pos.material_key()
// << "\npawn key: " << pos.pawn_key() << endl;
}
*/
else if (token == "uci")
{
cout << ChessEngine.GetEngineInfo()
<< ChessEngine.GetEngineOptions()
<< "uciok" << endl;
}
else if ( token == "test" )
{
ChessEngine.SetDebug(true);
DebugTest test;
test.LaunchTests();
token = "dummy";
}
else if ( token == "bench" )
{
ChessEngine.SetDebug(false);
DebugBench bench;
bench.LaunchBench();
token = "dummy";
}
else if ( token == "dummy" )
{
// do nothing
}
else
{
cout << "Unknown command: " << cmd << endl;
}
}
return 0;
}
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
void Uci::set_position(istringstream& is) {
Move m;
string token, fen;
is >> token;
if (token == "startpos")
{
fen = StartFEN;
is >> token; // Consume "moves" token if any
}
else if (token == "fen")
while (is >> token && token != "moves")
fen += token + " ";
else
return;
//pos.from_fen(fen, Options["UCI_Chess960"]);
ChessEngine.SetPosition(fen);
// Parse move list (if any)
//while (is >> token && (m = string_to_move(ChessEngine.currentPosition, token)) != MOVE_NONE)
while (is >> token && (m = string_to_move(/*ChessEngine.currentPosition,*/ token)) != MOVE_NONE)
{
//TODO: take current position in consideration for stringToMove method (cPiece and cColor are missing in generated move)
//m = move_from_uci(pos, token);
//m = stringToMove(token);
//pos.do_move(m, *SetupState);
//ChessEngine.DoMove(m, *SetupState);
ChessEngine.Play(m);
/*
// Increment pointer to StateRingBuf circular buffer
if (++SetupState - StateRingBuf >= 102)
SetupState = StateRingBuf;
*/
}
}
// set_option() is called when engine receives the "setoption" UCI command. The
// function updates the UCI option ("name") to the given value ("value").
void Uci::set_option(istringstream& is) {
string token, name, value;
is >> token; // Consume "name" token
// Read option name (can contain spaces)
while (is >> token && token != "value")
name += string(" ", !name.empty()) + token;
// Read option value (can contain spaces)
while (is >> token)
value += string(" ", !value.empty()) + token;
/*
//if (!Options.size()(name))
if (!ChessEngine.IsValidOption(name))
cout << "No such option: " << name << endl;
else if (value.empty()) // UCI buttons don't have a value
Options[name] = true;
else
Options[name] = value;
else */
ChessEngine.SetOption(name,value);
}
// go() is called when engine receives the "go" UCI command. The function sets
// the thinking time and other parameters from the input string, and then starts
// the main searching thread.
void Uci::go(istringstream& is) {
string token;
//LimitsType limits;
std::set<Move> searchMoves;
int time[] = { 0, 0 }, inc[] = { 0, 0 };
while (is >> token)
{
if (token == "infinite") {
//ChessEngine.SetLimitsInfinite(true);//limits.infinite = true;
ChessEngine.brainMode = INFINITE_MODE;
}
else if (token == "ponder") {
//ChessEngine.SetLimitsPonder(true);//limits.ponder = true;
ChessEngine.SetPonderMode();
}
else if (token == "wtime"){
is >> time[WHITE];
//is >> ChessEngine.wtime;
}
else if (token == "btime"){
is >> time[BLACK];
//is >> ChessEngine.btime;
}
else if (token == "winc"){
is >> inc[WHITE];
//is >> ChessEngine.winc;
}
else if (token == "binc"){
is >> inc[BLACK];
//is >> ChessEngine.binc;
}
else if (token == "movestogo"){
//is >> limits.movestogo;
int movesToGo;
is >> movesToGo;
ChessEngine.SetMovestogo(movesToGo);
}
else if (token == "depth"){
//is >> limits.depth;
//ChessEngine.brainMode = DEPTH_MODE;
int depth;
is >> depth;
ChessEngine.SetDepthMode(depth);
}
else if (token == "nodes"){
//is >> limits.maxNodes;
//ChessEngine.brainMode = NODES_MODE;
int maxNodes;
is >> maxNodes;
ChessEngine.SetNodesMode(maxNodes);
}
else if (token == "movetime"){
//is >> limits.maxTime;
//is >> ChessEngine.thinkDelay;
//ChessEngine.brainMode = MOVETIME_MODE;
int movetime;
is >> movetime;
ChessEngine.SetMovetimeMode(movetime);
}
else if (token == "searchmoves"){
// TODO: refactor this part
//while (is >> token)
// searchMoves.insert(move_from_uci(pos, token));
}
}
//limits.time = time[pos.side_to_move() pos.turn?0:1];
//limits.increment = inc[pos.side_to_move() pos.turn?0:1];
//Threads.start_thinking(pos, limits, searchMoves, true);
//ChessEngine.StartThinking(pos, limits, searchMoves, true);
ChessEngine.Start();
}
// perft() is called when engine receives the "perft" command. The function
// calls perft() with the required search depth then prints counted leaf nodes
// and elapsed time.
/*
void Uci::perft(istringstream& is) {
int depth, time;
if (!(is >> depth))
return;
// TODO: use watch (to be improved)
Watch w;
w.Start();
//time = system_time();
//int64_t n = Search::perft(pos, depth * ONE_PLY); long ???
long long n = perft(pos, depth * ONE_PLY);
// TODO: use watch
w.Stop();
time = w.GetElapsedTime();
//time = system_time() - time;
std::cout << "\nNodes " << n
<< "\nTime (ms) " << time
<< "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;
}
*/
///// Search::perft() is our utility to verify move generation. All the leaf nodes
///// up to the given depth are generated and counted and the sum returned.
//
////int64_t
//long long perft(Position& pos, int depth) {
//
// //StateInfo st;
// //int64_t cnt = 0;
// long long cnt = 0;
// /*
//
// Movevector<MV_LEGAL> ml(pos);
//
// // At the last ply just return the number of moves (leaf nodes)
// if (depth == ONE_PLY)
// return ml.size();
//
// CheckInfo ci(pos);
// for ( ; !ml.end(); ++ml)
// {
// pos.do_move(ml.move(), st, ci, pos.move_gives_check(ml.move(), ci));
// cnt += perft(pos, depth - ONE_PLY);
// pos.undo_move(ml.move());
// }
// */
// return cnt;
//}
/*
/// move_from_uci() takes a position and a string representing a move in
/// simple coordinate notation and returns an equivalent Move if any.
/// Moves are guaranteed to be legal.
Move Uci::move_from_uci(const Position& pos, const string& str) {
for (Movevector<MV_LEGAL> ml(pos); !ml.end(); ++ml)
if (str == move_to_uci(ml.move(), pos.is_chess960()))
return ml.move();
return MOVE_NONE;
return NULL;
}
*/