2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2010-03-20 04:27:07 -06:00
|
|
|
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-09-01 06:05:02 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2008-09-01 06:05:02 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
//// Includes
|
|
|
|
////
|
|
|
|
|
2009-02-23 10:46:33 -07:00
|
|
|
#include <cassert>
|
2008-08-31 23:59:13 -06:00
|
|
|
#include <iostream>
|
2008-09-01 00:06:34 -06:00
|
|
|
#include <sstream>
|
2008-08-31 23:59:13 -06:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "evaluate.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "move.h"
|
|
|
|
#include "movegen.h"
|
|
|
|
#include "position.h"
|
|
|
|
#include "san.h"
|
|
|
|
#include "search.h"
|
|
|
|
#include "ucioption.h"
|
|
|
|
|
2009-05-20 04:40:07 -06:00
|
|
|
using namespace std;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2010-08-19 10:55:32 -06:00
|
|
|
// FEN string for the initial position
|
|
|
|
const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
// UCIParser is a class for parsing UCI input. The class
|
2008-09-01 00:16:17 -06:00
|
|
|
// is actually a string stream built on a given input string.
|
2010-12-13 01:48:12 -07:00
|
|
|
typedef istringstream UCIParser;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
// Local functions
|
2011-01-05 15:34:51 -07:00
|
|
|
void set_option(UCIParser& up);
|
|
|
|
void set_position(Position& pos, UCIParser& up);
|
|
|
|
bool go(Position& pos, UCIParser& up);
|
|
|
|
void perft(Position& pos, UCIParser& up);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
/// 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.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
bool execute_uci_command(const string& cmd) {
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-01-03 04:50:49 -07:00
|
|
|
static Position pos(StartPositionFEN, false, 0); // The root position
|
2010-12-13 03:17:06 -07:00
|
|
|
UCIParser up(cmd);
|
|
|
|
string token;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
if (!(up >> token)) // operator>>() skips any whitespace
|
|
|
|
return true;
|
2008-09-01 06:05:02 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
if (token == "quit")
|
|
|
|
return false;
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
if (token == "go")
|
|
|
|
return go(pos, up);
|
2008-09-01 00:16:17 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
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")
|
2011-01-03 04:50:49 -07:00
|
|
|
pos.from_fen(StartPositionFEN, false);
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
else if (token == "isready")
|
|
|
|
cout << "readyok" << endl;
|
2009-05-07 04:45:46 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
else if (token == "position")
|
|
|
|
set_position(pos, up);
|
2009-05-07 04:45:46 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
else if (token == "setoption")
|
|
|
|
set_option(up);
|
2010-12-13 01:48:12 -07:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
// The remaining commands are for debugging purposes only
|
|
|
|
else if (token == "d")
|
|
|
|
pos.print();
|
2010-12-13 01:48:12 -07:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
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;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
else if (token == "perft")
|
|
|
|
perft(pos, up);
|
2010-12-13 01:48:12 -07:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
else
|
|
|
|
cout << "Unknown command: " << cmd << endl;
|
2010-12-13 01:48:12 -07:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-13 01:48:12 -07:00
|
|
|
|
2010-01-07 03:59:32 -07:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
////
|
|
|
|
//// Local functions
|
|
|
|
////
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
namespace {
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
// set_position() is called when Stockfish receives the "position" UCI
|
2010-12-13 01:48:12 -07:00
|
|
|
// command. The input parameter is a UCIParser. It is assumed
|
2008-08-31 23:59:13 -06:00
|
|
|
// that this parser has consumed the first token of the UCI command
|
|
|
|
// ("position"), and is ready to read the second token ("startpos"
|
|
|
|
// or "fen", if the input is well-formed).
|
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
void set_position(Position& pos, UCIParser& up) {
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2009-05-20 04:40:07 -06:00
|
|
|
string token;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
if (!(up >> token) || (token != "startpos" && token != "fen"))
|
2010-01-07 03:59:32 -07:00
|
|
|
return;
|
2008-09-01 00:16:17 -06:00
|
|
|
|
|
|
|
if (token == "startpos")
|
2010-12-13 03:17:06 -07:00
|
|
|
{
|
2011-01-03 04:50:49 -07:00
|
|
|
pos.from_fen(StartPositionFEN, false);
|
2010-12-13 03:17:06 -07:00
|
|
|
if (!(up >> token))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else // fen
|
2008-09-01 00:16:17 -06:00
|
|
|
{
|
2009-05-20 04:40:07 -06:00
|
|
|
string fen;
|
2010-12-13 01:48:12 -07:00
|
|
|
while (up >> token && token != "moves")
|
2008-09-01 00:16:17 -06:00
|
|
|
{
|
2009-05-20 04:40:07 -06:00
|
|
|
fen += token;
|
|
|
|
fen += ' ';
|
2008-09-01 00:16:17 -06:00
|
|
|
}
|
2011-01-03 04:50:49 -07:00
|
|
|
pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
if (token != "moves")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Parse optional move list
|
|
|
|
Move move;
|
|
|
|
StateInfo st;
|
|
|
|
while (up >> token)
|
2008-09-01 00:16:17 -06:00
|
|
|
{
|
2010-12-13 03:17:06 -07:00
|
|
|
move = move_from_uci(pos, token);
|
|
|
|
pos.do_move(move, st);
|
|
|
|
if (pos.rule_50_counter() == 0)
|
|
|
|
pos.reset_game_ply();
|
2010-01-07 03:59:32 -07:00
|
|
|
|
2010-12-13 03:17:06 -07:00
|
|
|
pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2010-12-13 03:17:06 -07:00
|
|
|
// Our StateInfo st is about going out of scope so copy
|
|
|
|
// its content inside pos before it disappears.
|
|
|
|
pos.detach();
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
// set_option() is called when Stockfish receives the "setoption" UCI
|
2010-12-13 01:48:12 -07:00
|
|
|
// command. The input parameter is a UCIParser. It is assumed
|
2008-08-31 23:59:13 -06:00
|
|
|
// that this parser has consumed the first token of the UCI command
|
|
|
|
// ("setoption"), and is ready to read the second token ("name", if
|
|
|
|
// the input is well-formed).
|
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
void set_option(UCIParser& up) {
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2010-01-27 01:37:12 -07:00
|
|
|
string token, name, value;
|
2008-09-01 00:16:17 -06:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
if (!(up >> token) || token != "name") // operator>>() skips any whitespace
|
2010-01-07 03:59:32 -07:00
|
|
|
return;
|
2009-05-02 04:14:45 -06:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
if (!(up >> name))
|
2010-11-03 02:32:14 -06:00
|
|
|
return;
|
2010-01-07 03:59:32 -07:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
// Handle names with included spaces
|
|
|
|
while (up >> token && token != "value")
|
2010-11-03 02:32:14 -06:00
|
|
|
name += (" " + token);
|
2009-07-10 10:34:56 -06:00
|
|
|
|
2010-11-04 01:03:27 -06:00
|
|
|
if (Options.find(name) == Options.end())
|
|
|
|
{
|
|
|
|
cout << "No such option: " << name << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
// Is a button ?
|
|
|
|
if (token != "value")
|
2010-11-03 02:32:14 -06:00
|
|
|
{
|
2010-11-04 01:03:27 -06:00
|
|
|
Options[name].set_value("true");
|
2010-11-03 02:32:14 -06:00
|
|
|
return;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2010-11-03 02:32:14 -06:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
if (!(up >> value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Handle values with included spaces
|
|
|
|
while (up >> token)
|
2010-11-03 02:32:14 -06:00
|
|
|
value += (" " + token);
|
|
|
|
|
2010-11-04 01:03:27 -06:00
|
|
|
Options[name].set_value(value);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-20 04:40:07 -06:00
|
|
|
// go() is called when Stockfish receives the "go" UCI command. The
|
2010-12-13 01:48:12 -07:00
|
|
|
// input parameter is a UCIParser. It is assumed that this
|
2008-08-31 23:59:13 -06:00
|
|
|
// parser has consumed the first token of the UCI command ("go"),
|
2009-05-07 04:45:46 -06:00
|
|
|
// and is ready to read the second token. The function sets the
|
2008-08-31 23:59:13 -06:00
|
|
|
// thinking time and other parameters from the input string, and
|
|
|
|
// calls think() (defined in search.cpp) with the appropriate
|
2009-05-07 04:45:46 -06:00
|
|
|
// parameters. Returns false if a quit command is received while
|
|
|
|
// thinking, returns true otherwise.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
bool go(Position& pos, UCIParser& up) {
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2009-05-20 04:40:07 -06:00
|
|
|
string token;
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2008-09-01 00:16:17 -06:00
|
|
|
int time[2] = {0, 0}, inc[2] = {0, 0};
|
|
|
|
int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
|
2008-08-31 23:59:13 -06:00
|
|
|
bool infinite = false, ponder = false;
|
2010-11-03 02:32:14 -06:00
|
|
|
Move searchMoves[MOVES_MAX];
|
2008-08-31 23:59:13 -06:00
|
|
|
|
|
|
|
searchMoves[0] = MOVE_NONE;
|
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
while (up >> token)
|
2008-09-01 00:16:17 -06:00
|
|
|
{
|
|
|
|
if (token == "infinite")
|
|
|
|
infinite = true;
|
|
|
|
else if (token == "ponder")
|
|
|
|
ponder = true;
|
|
|
|
else if (token == "wtime")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> time[0];
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "btime")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> time[1];
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "winc")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> inc[0];
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "binc")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> inc[1];
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "movestogo")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> movesToGo;
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "depth")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> depth;
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "nodes")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> nodes;
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "movetime")
|
2010-12-13 01:48:12 -07:00
|
|
|
up >> moveTime;
|
2008-09-01 00:16:17 -06:00
|
|
|
else if (token == "searchmoves")
|
|
|
|
{
|
|
|
|
int numOfMoves = 0;
|
2010-12-13 01:48:12 -07:00
|
|
|
while (up >> token)
|
2010-12-09 04:10:02 -07:00
|
|
|
searchMoves[numOfMoves++] = move_from_uci(pos, token);
|
2010-01-26 09:55:27 -07:00
|
|
|
|
2008-09-01 00:16:17 -06:00
|
|
|
searchMoves[numOfMoves] = MOVE_NONE;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-31 04:37:43 -06:00
|
|
|
assert(pos.is_ok());
|
2009-02-23 10:46:33 -07:00
|
|
|
|
2010-10-31 04:37:43 -06:00
|
|
|
return think(pos, infinite, ponder, time, inc, movesToGo,
|
2010-07-15 09:05:56 -06:00
|
|
|
depth, nodes, moveTime, searchMoves);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2009-05-20 04:40:07 -06:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
void perft(Position& pos, UCIParser& up) {
|
2009-10-01 22:09:24 -06:00
|
|
|
|
2011-01-05 09:03:57 -07:00
|
|
|
int depth, tm;
|
|
|
|
int64_t n;
|
2009-10-01 22:09:24 -06:00
|
|
|
|
2010-12-13 01:48:12 -07:00
|
|
|
if (!(up >> depth))
|
2009-11-13 02:27:14 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
tm = get_system_time();
|
|
|
|
|
2010-10-31 04:37:43 -06:00
|
|
|
n = perft(pos, depth * ONE_PLY);
|
2009-10-01 22:09:24 -06:00
|
|
|
|
|
|
|
tm = get_system_time() - tm;
|
|
|
|
std::cout << "\nNodes " << n
|
|
|
|
<< "\nTime (ms) " << tm
|
2010-11-03 02:32:14 -06:00
|
|
|
<< "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;
|
2009-10-01 22:09:24 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|