#include "move.h"
#include "movegen.h"
+#include "search.h"
using std::string;
/// It is used to write search information to the log file (which is created
/// when the UCI parameter "Use Search Log" is "true").
-const string pretty_pv(const Position& pos, int time, int depth,
+const string pretty_pv(Position& pos, int time, int depth,
Value score, ValueType type, Move pv[]) {
const int64_t K = 1000;
const size_t maxLength = 80 - startColumn;
const string lf = string("\n") + string(startColumn, ' ');
- StateInfo st;
+ StateInfo state[PLY_MAX_PLUS_2], *st = state;
+ Move* m = pv;
std::stringstream s;
string san;
size_t length = 0;
- Position p(pos, pos.thread());
-
// First print depth, score, time and searched nodes...
s << std::setw(2) << depth
<< (type == VALUE_TYPE_LOWER ? " >" : type == VALUE_TYPE_UPPER ? " <" : " ")
s << std::setw(7) << pos.nodes_searched() / M << " M ";
// ...then print the full PV line in short algebraic notation
- for (Move* m = pv; *m != MOVE_NONE; m++)
+ while (*m != MOVE_NONE)
{
- san = move_to_san(p, *m);
+ san = move_to_san(pos, *m);
length += san.length() + 1;
if (length > maxLength)
}
s << san << ' ';
- p.do_move(*m, st);
+ pos.do_move(*m++, *st++);
}
+ // Restore original position before to leave
+ while (m != pv) pos.undo_move(*--m);
+
return s.str();
}
extern const std::string move_to_uci(Move m, bool chess960);
extern Move move_from_uci(const Position& pos, const std::string& str);
extern const std::string move_to_san(Position& pos, Move m);
-extern const std::string pretty_pv(const Position& pos, int time, int depth, Value score, ValueType type, Move pv[]);
+extern const std::string pretty_pv(Position& pos, int time, int depth, Value score, ValueType type, Move pv[]);
#endif // !defined(MOVE_H_INCLUDED)
/// It should be used when setting up a position on board.
/// You can't undo the move.
-void Position::do_setup_move(Move m, StateInfo& newSt) {
+void Position::do_setup_move(Move m) {
+
+ StateInfo newSt;
do_move(m, newSt);
// Update the number of plies played from the starting position
startPosPlyCounter++;
+
+ // Our StateInfo newSt is about going out of scope so copy
+ // its content inside pos before it disappears.
+ detach();
}
/// Position::do_move() makes a move, and saves all information necessary
assert(is_ok());
assert(move_is_ok(m));
+ assert(&newSt != st);
nodes++;
Key key = st->key;
Value npMaterial[2];
};
- if (&newSt != st)
- memcpy(&newSt, st, sizeof(ReducedStateInfo));
+ memcpy(&newSt, st, sizeof(ReducedStateInfo));
newSt.previous = st;
st = &newSt;
bool square_is_weak(Square s, Color c) const;
// Doing and undoing moves
- void detach();
- void do_setup_move(Move m, StateInfo& St);
+ void do_setup_move(Move m);
void do_move(Move m, StateInfo& st);
void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
void undo_move(Move m);
// Initialization helper functions (used while setting up a position)
void clear();
+ void detach();
void put_piece(Piece p, Square s);
void do_allow_oo(Color c);
void do_allow_ooo(Color c);
void extract_pv_from_tt(Position& pos);
void insert_pv_in_tt(Position& pos);
- std::string pv_info_to_uci(const Position& pos, Value alpha, Value beta, int pvLine = 0);
+ std::string pv_info_to_uci(Position& pos, Value alpha, Value beta, int pvLine = 0);
int64_t nodes;
Value pv_score;
// formatted according to UCI specification and eventually writes the info
// to a log file. It is called at each iteration or after a new pv is found.
- std::string RootMove::pv_info_to_uci(const Position& pos, Value alpha, Value beta, int pvLine) {
+ std::string RootMove::pv_info_to_uci(Position& pos, Value alpha, Value beta, int pvLine) {
std::stringstream s, l;
Move* m = pv;
void set_position(Position& pos, UCIParser& up) {
- string token;
+ string fen, token;
if (!(up >> token) || (token != "startpos" && token != "fen"))
return;
if (token == "startpos")
{
pos.from_fen(StartPositionFEN, false);
- if (!(up >> token))
- return;
+ up >> token; // Consume "moves" token
}
else // fen
{
- string fen;
while (up >> token && token != "moves")
- {
- fen += token;
- fen += ' ';
- }
+ fen += token + string(" ");
+
pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
}
- if (token != "moves")
- return;
-
- // Parse optional move list
- Move move;
- StateInfo st;
+ // Parse move list (if any)
while (up >> token)
- {
- move = move_from_uci(pos, token);
- pos.do_setup_move(move, st);
- }
- // Our StateInfo st is about going out of scope so copy
- // its content inside pos before it disappears.
- pos.detach();
+ pos.do_setup_move(move_from_uci(pos, token));
}