- // FEN string for the initial position
- const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
-
- // UCIParser is a class for parsing UCI input. The class
- // is actually a string stream built on a given input string.
- typedef istringstream UCIParser;
-
- // Local functions
- void set_option(UCIParser& up);
- void set_position(Position& pos, UCIParser& up);
- bool go(Position& pos, UCIParser& up);
- void perft(Position& pos, UCIParser& up);
- Move parse_uci_move(const Position& pos, const std::string &str);
-}
-
-
-/// execute_uci_command() takes a string as input, uses a UCIParser
-/// object to parse this text string as a UCI command, and calls
-/// the appropriate functions. In addition to the UCI commands,
-/// the function also supports a few debug commands.
-
-bool execute_uci_command(const string& cmd) {
-
- static Position pos(StartPositionFEN, false, 0); // The root position
- UCIParser up(cmd);
- string token;
-
- if (!(up >> token)) // operator>>() skips any whitespace
- return true;
-
- if (token == "quit")
- return false;
-
- if (token == "go")
- return go(pos, up);
-
- if (token == "uci")
- {
- cout << "id name " << engine_name()
- << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
- print_uci_options();
- cout << "uciok" << endl;
- }
- else if (token == "ucinewgame")
- pos.from_fen(StartPositionFEN, false);
-
- else if (token == "isready")
- cout << "readyok" << endl;
-
- else if (token == "position")
- set_position(pos, up);
-
- else if (token == "setoption")
- set_option(up);
-
- // The remaining commands are for debugging purposes only
- else if (token == "d")
- pos.print();
-
- else if (token == "flip")
- {
- Position p(pos, pos.thread());
- pos.flipped_copy(p);
- }
- else if (token == "eval")
- {
- Value evalMargin;
- cout << "Incremental mg: " << mg_value(pos.value())
- << "\nIncremental eg: " << eg_value(pos.value())
- << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
- }
- else if (token == "key")
- cout << "key: " << hex << pos.get_key()
- << "\nmaterial key: " << pos.get_material_key()
- << "\npawn key: " << pos.get_pawn_key() << endl;
-
- else if (token == "perft")
- perft(pos, up);
-
- else
- cout << "Unknown command: " << cmd << endl;
-
- return true;
-}
-
-
-////
-//// Local functions
-////
-
-namespace {
-
- // parse_uci_move() takes a position and a string as input, and attempts to
- // convert the string to a move, using simple coordinate notation (g1f3,
- // a7a8q, etc.). In order to correctly parse en passant captures and castling
- // moves, we need the position. This function is not robust, and expects that
- // the input move is legal and correctly formatted.