X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fuci.cpp;h=c011171a44c1b8e7a26e83d6c92fd25ba662737f;hb=fad595f5b6b343708fc0b4d8ab15ab73040f84c1;hp=21bbb88472d4ce55b1fb5e56eba372a333e074c7;hpb=84ec1f7331f402f4ab2e2201b72b3ee378cce659;p=stockfish diff --git a/src/uci.cpp b/src/uci.cpp index 21bbb884..c011171a 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -1,7 +1,7 @@ /* Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) - Copyright (C) 2008-2009 Marco Costalba + Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,7 +27,6 @@ #include #include -#include "book.h" #include "evaluate.h" #include "misc.h" #include "move.h" @@ -35,7 +34,6 @@ #include "position.h" #include "san.h" #include "search.h" -#include "uci.h" #include "ucioption.h" using namespace std; @@ -46,22 +44,19 @@ using namespace std; namespace { + // FEN string for the initial position + const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + // UCIInputParser is a class for parsing UCI input. The class // is actually a string stream built on a given input string. - typedef istringstream UCIInputParser; - // The root position. This is set up when the user (or in practice, the GUI) - // sends the "position" UCI command. The root position is sent to the think() - // function when the program receives the "go" command. - Position RootPosition(StartPosition); - // Local functions - bool handle_command(const string& command); + bool handle_command(Position& pos, const string& command); void set_option(UCIInputParser& uip); - void set_position(UCIInputParser& uip); - bool go(UCIInputParser& uip); - void perft(UCIInputParser& uip); + void set_position(Position& pos, UCIInputParser& uip); + bool go(Position& pos, UCIInputParser& uip); + void perft(Position& pos, UCIInputParser& uip); } @@ -79,7 +74,7 @@ namespace { void uci_main_loop() { - RootPosition.from_fen(StartPosition); + Position pos(StartPositionFEN, 0); // The root position string command; do { @@ -87,7 +82,7 @@ void uci_main_loop() { if (!getline(cin, command)) command = "quit"; - } while (handle_command(command)); + } while (handle_command(pos, command)); } @@ -102,7 +97,7 @@ namespace { // and calls the appropriate functions. In addition to the UCI // commands, the function also supports a few debug commands. - bool handle_command(const string& command) { + bool handle_command(Position& pos, const string& command) { UCIInputParser uip(command); string token; @@ -114,7 +109,7 @@ namespace { return false; if (token == "go") - return go(uip); + return go(pos, uip); if (token == "uci") { @@ -125,14 +120,13 @@ namespace { } else if (token == "ucinewgame") { - push_button("New Game"); - Position::init_piece_square_tables(); - RootPosition.from_fen(StartPosition); + Options["New Game"].set_value("true"); + pos.from_fen(StartPositionFEN); } else if (token == "isready") cout << "readyok" << endl; else if (token == "position") - set_position(uip); + set_position(pos, uip); else if (token == "setoption") set_option(uip); @@ -140,25 +134,25 @@ namespace { // Perhaps they should be removed later in order to reduce the // size of the program binary. else if (token == "d") - RootPosition.print(); + pos.print(); else if (token == "flip") { - Position p(RootPosition); - RootPosition.flipped_copy(p); + Position p(pos, pos.thread()); + pos.flipped_copy(p); } else if (token == "eval") { - EvalInfo ei; - cout << "Incremental mg: " << mg_value(RootPosition.value()) - << "\nIncremental eg: " << eg_value(RootPosition.value()) - << "\nFull eval: " << evaluate(RootPosition, ei, 0) << endl; + 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 << RootPosition.get_key() - << "\nmaterial key: " << RootPosition.get_material_key() - << "\npawn key: " << RootPosition.get_pawn_key() << endl; + cout << "key: " << hex << pos.get_key() + << "\nmaterial key: " << pos.get_material_key() + << "\npawn key: " << pos.get_pawn_key() << endl; else if (token == "perft") - perft(uip); + perft(pos, uip); else cout << "Unknown command: " << command << endl; @@ -172,7 +166,7 @@ namespace { // ("position"), and is ready to read the second token ("startpos" // or "fen", if the input is well-formed). - void set_position(UCIInputParser& uip) { + void set_position(Position& pos, UCIInputParser& uip) { string token; @@ -180,7 +174,7 @@ namespace { return; if (token == "startpos") - RootPosition.from_fen(StartPosition); + pos.from_fen(StartPositionFEN); else if (token == "fen") { string fen; @@ -189,7 +183,7 @@ namespace { fen += token; fen += ' '; } - RootPosition.from_fen(fen); + pos.from_fen(fen); } if (uip.good()) @@ -203,14 +197,16 @@ namespace { StateInfo st; while (uip >> token) { - move = move_from_string(RootPosition, token); - RootPosition.do_move(move, st); - if (RootPosition.rule_50_counter() == 0) - RootPosition.reset_game_ply(); + move = move_from_string(pos, token); + pos.do_move(move, st); + if (pos.rule_50_counter() == 0) + pos.reset_game_ply(); + + pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50 } // Our StateInfo st is about going out of scope so copy - // its content inside RootPosition before they disappear. - RootPosition.detach(); + // its content inside pos before it disappears. + pos.detach(); } } } @@ -224,26 +220,33 @@ namespace { void set_option(UCIInputParser& uip) { - string token, name; + string token, name, value; if (!(uip >> token)) // operator>>() skips any whitespace return; - if (token == "name" && uip >> name) - { - while (uip >> token && token != "value") - name += (" " + token); + if (token != "name" || !(uip >> name)) + return; - if (token == "value") - { - // Reads until end of line and left trim white space - getline(uip, token); - token.erase(0, token.find_first_not_of(" \n\r\t")); + while (uip >> token && token != "value") + name += (" " + token); - set_option_value(name, token); - } else - push_button(name); + if (Options.find(name) == Options.end()) + { + cout << "No such option: " << name << endl; + return; } + + if (token != "value" || !(uip >> value)) + { + Options[name].set_value("true"); + return; + } + + while (uip >> token) + value += (" " + token); + + Options[name].set_value(value); } @@ -256,14 +259,14 @@ namespace { // parameters. Returns false if a quit command is received while // thinking, returns true otherwise. - bool go(UCIInputParser& uip) { + bool go(Position& pos, UCIInputParser& uip) { string token; int time[2] = {0, 0}, inc[2] = {0, 0}; int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0; bool infinite = false, ponder = false; - Move searchMoves[500]; + Move searchMoves[MOVES_MAX]; searchMoves[0] = MOVE_NONE; @@ -292,40 +295,34 @@ namespace { else if (token == "searchmoves") { int numOfMoves = 0; - while (!uip.eof()) - { - uip >> token; - searchMoves[numOfMoves++] = move_from_string(RootPosition, token); - } + while (uip >> token) + searchMoves[numOfMoves++] = move_from_string(pos, token); + searchMoves[numOfMoves] = MOVE_NONE; } } - if (moveTime) - infinite = true; // HACK - - assert(RootPosition.is_ok()); + assert(pos.is_ok()); - return think(RootPosition, infinite, ponder, RootPosition.side_to_move(), - time, inc, movesToGo, depth, nodes, moveTime, searchMoves); + return think(pos, infinite, ponder, time, inc, movesToGo, + depth, nodes, moveTime, searchMoves); } - void perft(UCIInputParser& uip) { + void perft(Position& pos, UCIInputParser& uip) { string token; int depth, tm, n; - Position pos = RootPosition; if (!(uip >> depth)) return; tm = get_system_time(); - n = perft(pos, depth * OnePly); + n = perft(pos, depth * ONE_PLY); tm = get_system_time() - tm; std::cout << "\nNodes " << n << "\nTime (ms) " << tm - << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl; + << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl; } }