+ // pv_to_uci() returns a string with information on the current PV line
+ // formatted according to UCI specification.
+
+ string pv_to_uci(const Move pv[], int pvNum, bool chess960) {
+
+ std::stringstream s;
+
+ s << " multipv " << pvNum << " pv " << set960(chess960);
+
+ for ( ; *pv != MOVE_NONE; pv++)
+ s << *pv << " ";
+
+ return s.str();
+ }
+
+ // depth_to_uci() returns a string with information on the current depth and
+ // seldepth formatted according to UCI specification.
+
+ string depth_to_uci(Depth depth) {
+
+ std::stringstream s;
+
+ // Retrieve max searched depth among threads
+ int selDepth = 0;
+ for (int i = 0; i < Threads.size(); i++)
+ if (Threads[i].maxPly > selDepth)
+ selDepth = Threads[i].maxPly;
+
+ s << " depth " << depth / ONE_PLY << " seldepth " << selDepth;
+
+ return s.str();
+ }
+
+ string time_to_string(int millisecs) {
+
+ const int MSecMinute = 1000 * 60;
+ const int MSecHour = 1000 * 60 * 60;
+
+ int hours = millisecs / MSecHour;
+ int minutes = (millisecs % MSecHour) / MSecMinute;
+ int seconds = ((millisecs % MSecHour) % MSecMinute) / 1000;
+
+ std::stringstream s;
+
+ if (hours)
+ s << hours << ':';
+
+ s << std::setfill('0') << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
+ return s.str();
+ }
+
+ string score_to_string(Value v) {
+
+ std::stringstream s;
+
+ if (v >= VALUE_MATE_IN_PLY_MAX)
+ s << "#" << (VALUE_MATE - v + 1) / 2;
+ else if (v <= VALUE_MATED_IN_PLY_MAX)
+ s << "-#" << (VALUE_MATE + v) / 2;
+ else
+ s << std::setprecision(2) << std::fixed << std::showpos << float(v) / PawnValueMidgame;
+
+ return s.str();
+ }
+
+ // pretty_pv() creates a human-readable string from a position and a PV.
+ // It is used to write search information to the log file (which is created
+ // when the UCI parameter "Use Search Log" is "true").
+
+ string pretty_pv(Position& pos, int depth, Value value, int time, Move pv[]) {
+
+ const int64_t K = 1000;
+ const int64_t M = 1000000;
+ const int startColumn = 28;
+ const size_t maxLength = 80 - startColumn;
+
+ StateInfo state[PLY_MAX_PLUS_2], *st = state;
+ Move* m = pv;
+ string san;
+ std::stringstream s;
+ size_t length = 0;
+
+ // First print depth, score, time and searched nodes...
+ s << set960(pos.is_chess960())
+ << std::setw(2) << depth
+ << std::setw(8) << score_to_string(value)
+ << std::setw(8) << time_to_string(time);
+
+ if (pos.nodes_searched() < M)
+ s << std::setw(8) << pos.nodes_searched() / 1 << " ";
+ else if (pos.nodes_searched() < K * M)
+ s << std::setw(7) << pos.nodes_searched() / K << "K ";
+ else
+ s << std::setw(7) << pos.nodes_searched() / M << "M ";
+
+ // ...then print the full PV line in short algebraic notation
+ while (*m != MOVE_NONE)
+ {
+ san = move_to_san(pos, *m);
+ length += san.length() + 1;
+
+ if (length > maxLength)
+ {
+ length = san.length() + 1;
+ s << "\n" + string(startColumn, ' ');
+ }
+ s << san << ' ';
+
+ pos.do_move(*m++, *st++);
+ }
+
+ // Restore original position before to leave
+ while (m != pv) pos.undo_move(*--m);
+
+ return s.str();
+ }