#include <iostream>
#include <sstream>
#include <string.h>
#include <set>
#include <assert.h>
#include "uci.h"
#include "testools.h"
#include "notation.h"
//using namespace std;
void 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")
{
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;
// //SetPonderLimit(false);
// // Search::Signals.stopOnPonderhit)
// //if (IsStopOnPonderHit())
// //StopThinking();
// //Stop();
}
else if (token == "go")
{
uci_go(is);
}
else if (token == "ucinewgame")
{
SetStartPosition();
}
else if (token == "isready")
{
std::cout << "readyok" << std::endl;
}
else if (token == "position")
{
uci_set_position(is);
}
else if (token == "setoption")
{
uci_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;
SetDebug((value=="on"));
}
else if (token == "flip")
{
// // TODO: implement this method
// int toBeImplemented=1;
// //pos.flip_me();
// //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());
// //ReadEvaluation();
//
// //std::cout << trace_evaluate(pos) << std::endl;
// //TraceEvaluate();
}
*/
/*
// NOT PART OF UCI PROTOCOL V.2004
else if (token == "key")
{
// std::cout << "key: " << hex << pos.key()
// << "\nmaterial key: " << pos.material_key()
// << "\npawn key: " << pos.pawn_key() << std::endl;
}
*/
else if (token == "uci")
{
cout << GetEngineInfo(true) << endl
<< GetEngineOptions()
<< "uciok" << endl;
}
else if ( token == "test" )
{
SetDebug(true);
DebugTest test;
test.LaunchTests();
token = "dummy";
}
else if ( token == "dummy" )
{
// do nothing
}
else
{
cout << "Unknown command: " << cmd << endl;
}
}
}
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
void uci_set_position(istringstream& is)
{
bool isChess960 = false;
Move m;
string token, fen;
Position fenPos;
assert(brainStatus != THINKING);
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"]);
fenPos.set(fen, isChess960, NULL);
// Parse move list (if any)
while (is >> token && (m = move_from_uci(fenPos, token)) != MOVE_NONE)
{
StateInfo newSt;
fenPos.do_move(m, newSt);
}
// TODO: be carefull on that affectation
currentPosition = fenPos;
}
// 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;
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;
std::set<Move> searchMoves;
int time[] = { 0, 0 }, inc[] = { 0, 0 };
while (is >> token)
{
if (token == "infinite")
{
brainMode = INFINITE_MODE;
}
else if (token == "ponder")
{
//SetLimitsPonder(true);//limits.ponder = true;
SetPonderMode();
}
else if (token == "wtime")
{
is >> time[WHITE];
}
else if (token == "btime")
{
is >> time[BLACK];
}
else if (token == "winc")
{
is >> inc[WHITE];
}
else if (token == "binc")
{
is >> inc[BLACK];
}
else if (token == "movestogo")
{
int movesToGo;
is >> movesToGo;
SetMovestogo(movesToGo);
}
else if (token == "depth")
{
int depth;
is >> depth;
SetDepthMode(depth);
}
else if (token == "nodes")
{
int maxNodes;
is >> maxNodes;
SetNodesMode(maxNodes);
}
else if (token == "movetime")
{
int movetime;
is >> movetime;
SetMovetimeMode(movetime);
}
else if (token == "searchmoves")
{
// TODO: refactor this part
//while (is >> token)
// searchMoves.insert(move_from_uci(pos, token));
}
}
Start();
}