+
+
+// Turns a Value to an integer centipawn number,
+// without treatment of mate and similar special scores.
+int UCI::to_cp(Value v) { return 100 * v / UCI::NormalizeToPawnValue; }
+
+// Converts a Value to a string by adhering to the UCI protocol specification:
+//
+// cp <x> The score from the engine's point of view in centipawns.
+// mate <y> Mate in 'y' moves (not plies). If the engine is getting mated,
+// uses negative values for 'y'.
+std::string UCI::value(Value v) {
+
+ assert(-VALUE_INFINITE < v && v < VALUE_INFINITE);
+
+ std::stringstream ss;
+
+ if (std::abs(v) < VALUE_TB_WIN_IN_MAX_PLY)
+ ss << "cp " << UCI::to_cp(v);
+ else if (std::abs(v) <= VALUE_TB)
+ {
+ const int ply = VALUE_TB - std::abs(v); // recompute ss->ply
+ ss << "cp " << (v > 0 ? 20000 - ply : -20000 + ply);
+ }
+ else
+ ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
+
+ return ss.str();
+}
+
+
+// Reports the win-draw-loss (WDL) statistics given an evaluation
+// and a game ply based on the data gathered for fishtest LTC games.
+std::string UCI::wdl(Value v, int ply) {
+
+ std::stringstream ss;
+
+ int wdl_w = win_rate_model(v, ply);
+ int wdl_l = win_rate_model(-v, ply);
+ int wdl_d = 1000 - wdl_w - wdl_l;
+ ss << " wdl " << wdl_w << " " << wdl_d << " " << wdl_l;
+
+ return ss.str();
+}
+
+
+// Converts a Square to a string in algebraic notation (g1, a7, etc.)
+std::string UCI::square(Square s) {
+ return std::string{char('a' + file_of(s)), char('1' + rank_of(s))};
+}
+
+
+// Converts a Move to a string in coordinate notation (g1f3, a7a8q).
+// The only special case is castling where the e1g1 notation is printed in
+// standard chess mode and in e1h1 notation it is printed in Chess960 mode.
+// Internally, all castling moves are always encoded as 'king captures rook'.
+std::string UCI::move(Move m, bool chess960) {
+
+ if (m == MOVE_NONE)
+ return "(none)";
+
+ if (m == MOVE_NULL)
+ return "0000";
+
+ Square from = from_sq(m);
+ Square to = to_sq(m);
+
+ if (type_of(m) == CASTLING && !chess960)
+ to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
+
+ std::string move = UCI::square(from) + UCI::square(to);
+
+ if (type_of(m) == PROMOTION)
+ move += " pnbrqk"[promotion_type(m)];
+
+ return move;
+}
+
+
+// Converts a string representing a move in coordinate notation
+// (g1f3, a7a8q) to the corresponding legal Move, if any.
+Move UCI::to_move(const Position& pos, std::string& str) {
+
+ if (str.length() == 5)
+ str[4] = char(tolower(str[4])); // The promotion piece character must be lowercased
+
+ for (const auto& m : MoveList<LEGAL>(pos))
+ if (str == UCI::move(m, pos.is_chess960()))
+ return m;
+
+ return MOVE_NONE;
+}
+
+} // namespace Stockfish