1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Allocate RootPosition on the stack

And pass it as an argument.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2010-10-31 11:37:43 +01:00
parent d74025a34e
commit 19cf779629

View file

@ -51,20 +51,14 @@ namespace {
// UCIInputParser is a class for parsing UCI input. The class // UCIInputParser is a class for parsing UCI input. The class
// is actually a string stream built on a given input string. // is actually a string stream built on a given input string.
typedef istringstream UCIInputParser; 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(StartPositionFEN, 0);
// Local functions // Local functions
bool handle_command(const string& command); bool handle_command(Position& pos, const string& command);
void set_option(UCIInputParser& uip); void set_option(UCIInputParser& uip);
void set_position(UCIInputParser& uip); void set_position(Position& pos, UCIInputParser& uip);
bool go(UCIInputParser& uip); bool go(Position& pos, UCIInputParser& uip);
void perft(UCIInputParser& uip); void perft(Position& pos, UCIInputParser& uip);
} }
@ -82,6 +76,7 @@ namespace {
void uci_main_loop() { void uci_main_loop() {
Position pos(StartPositionFEN, 0); // The root position
string command; string command;
do { do {
@ -89,7 +84,7 @@ void uci_main_loop() {
if (!getline(cin, command)) if (!getline(cin, command))
command = "quit"; command = "quit";
} while (handle_command(command)); } while (handle_command(pos, command));
} }
@ -104,7 +99,7 @@ namespace {
// and calls the appropriate functions. In addition to the UCI // and calls the appropriate functions. In addition to the UCI
// commands, the function also supports a few debug commands. // 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); UCIInputParser uip(command);
string token; string token;
@ -116,7 +111,7 @@ namespace {
return false; return false;
if (token == "go") if (token == "go")
return go(uip); return go(pos, uip);
if (token == "uci") if (token == "uci")
{ {
@ -128,12 +123,12 @@ namespace {
else if (token == "ucinewgame") else if (token == "ucinewgame")
{ {
push_button("New Game"); push_button("New Game");
RootPosition.from_fen(StartPositionFEN); pos.from_fen(StartPositionFEN);
} }
else if (token == "isready") else if (token == "isready")
cout << "readyok" << endl; cout << "readyok" << endl;
else if (token == "position") else if (token == "position")
set_position(uip); set_position(pos, uip);
else if (token == "setoption") else if (token == "setoption")
set_option(uip); set_option(uip);
@ -141,25 +136,22 @@ namespace {
// Perhaps they should be removed later in order to reduce the // Perhaps they should be removed later in order to reduce the
// size of the program binary. // size of the program binary.
else if (token == "d") else if (token == "d")
RootPosition.print(); pos.print();
else if (token == "flip") else if (token == "flip")
{ pos.flipped_copy(Position(pos, pos.thread()));
Position p(RootPosition, RootPosition.thread());
RootPosition.flipped_copy(p);
}
else if (token == "eval") else if (token == "eval")
{ {
Value evalMargin; Value evalMargin;
cout << "Incremental mg: " << mg_value(RootPosition.value()) cout << "Incremental mg: " << mg_value(pos.value())
<< "\nIncremental eg: " << eg_value(RootPosition.value()) << "\nIncremental eg: " << eg_value(pos.value())
<< "\nFull eval: " << evaluate(RootPosition, evalMargin) << endl; << "\nFull eval: " << evaluate(pos, evalMargin) << endl;
} }
else if (token == "key") else if (token == "key")
cout << "key: " << hex << RootPosition.get_key() cout << "key: " << hex << pos.get_key()
<< "\nmaterial key: " << RootPosition.get_material_key() << "\nmaterial key: " << pos.get_material_key()
<< "\npawn key: " << RootPosition.get_pawn_key() << endl; << "\npawn key: " << pos.get_pawn_key() << endl;
else if (token == "perft") else if (token == "perft")
perft(uip); perft(pos, uip);
else else
cout << "Unknown command: " << command << endl; cout << "Unknown command: " << command << endl;
@ -173,7 +165,7 @@ namespace {
// ("position"), and is ready to read the second token ("startpos" // ("position"), and is ready to read the second token ("startpos"
// or "fen", if the input is well-formed). // or "fen", if the input is well-formed).
void set_position(UCIInputParser& uip) { void set_position(Position& pos, UCIInputParser& uip) {
string token; string token;
@ -181,7 +173,7 @@ namespace {
return; return;
if (token == "startpos") if (token == "startpos")
RootPosition.from_fen(StartPositionFEN); pos.from_fen(StartPositionFEN);
else if (token == "fen") else if (token == "fen")
{ {
string fen; string fen;
@ -190,7 +182,7 @@ namespace {
fen += token; fen += token;
fen += ' '; fen += ' ';
} }
RootPosition.from_fen(fen); pos.from_fen(fen);
} }
if (uip.good()) if (uip.good())
@ -204,16 +196,16 @@ namespace {
StateInfo st; StateInfo st;
while (uip >> token) while (uip >> token)
{ {
move = move_from_string(RootPosition, token); move = move_from_string(pos, token);
RootPosition.do_move(move, st); pos.do_move(move, st);
if (RootPosition.rule_50_counter() == 0) if (pos.rule_50_counter() == 0)
RootPosition.reset_game_ply(); pos.reset_game_ply();
RootPosition.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50 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 // Our StateInfo st is about going out of scope so copy
// its content inside RootPosition before it disappears. // its content inside pos before it disappears.
RootPosition.detach(); pos.detach();
} }
} }
} }
@ -258,7 +250,7 @@ namespace {
// parameters. Returns false if a quit command is received while // parameters. Returns false if a quit command is received while
// thinking, returns true otherwise. // thinking, returns true otherwise.
bool go(UCIInputParser& uip) { bool go(Position& pos, UCIInputParser& uip) {
string token; string token;
@ -295,19 +287,19 @@ namespace {
{ {
int numOfMoves = 0; int numOfMoves = 0;
while (uip >> token) while (uip >> token)
searchMoves[numOfMoves++] = move_from_string(RootPosition, token); searchMoves[numOfMoves++] = move_from_string(pos, token);
searchMoves[numOfMoves] = MOVE_NONE; searchMoves[numOfMoves] = MOVE_NONE;
} }
} }
assert(RootPosition.is_ok()); assert(pos.is_ok());
return think(RootPosition, infinite, ponder, time, inc, movesToGo, return think(pos, infinite, ponder, time, inc, movesToGo,
depth, nodes, moveTime, searchMoves); depth, nodes, moveTime, searchMoves);
} }
void perft(UCIInputParser& uip) { void perft(Position& pos, UCIInputParser& uip) {
string token; string token;
int depth, tm, n; int depth, tm, n;
@ -317,7 +309,7 @@ namespace {
tm = get_system_time(); tm = get_system_time();
n = perft(RootPosition, depth * ONE_PLY); n = perft(pos, depth * ONE_PLY);
tm = get_system_time() - tm; tm = get_system_time() - tm;
std::cout << "\nNodes " << n std::cout << "\nNodes " << n