]> git.sesse.net Git - stockfish/blob - src/notation.cpp
3686016b3eb6c92da2550f19df4f6c746a8d9aa3
[stockfish] / src / notation.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <cassert>
21 #include <iomanip>
22 #include <sstream>
23 #include <stack>
24
25 #include "movegen.h"
26 #include "notation.h"
27 #include "position.h"
28
29 using namespace std;
30
31 static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
32
33
34 /// score_to_uci() converts a value to a string suitable for use with the UCI
35 /// protocol specifications:
36 ///
37 /// cp <x>     The score from the engine's point of view in centipawns.
38 /// mate <y>   Mate in y moves, not plies. If the engine is getting mated
39 ///            use negative values for y.
40
41 string score_to_uci(Value v, Value alpha, Value beta) {
42
43   stringstream ss;
44
45   if (abs(v) < VALUE_MATE_IN_MAX_PLY)
46       ss << "cp " << v * 100 / PawnValueEg;
47   else
48       ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
49
50   ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
51
52   return ss.str();
53 }
54
55
56 /// move_to_uci() converts a move to a string in coordinate notation
57 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
58 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
59 /// mode. Internally castling moves are always encoded as "king captures rook".
60
61 const string move_to_uci(Move m, bool chess960) {
62
63   Square from = from_sq(m);
64   Square to = to_sq(m);
65
66   if (m == MOVE_NONE)
67       return "(none)";
68
69   if (m == MOVE_NULL)
70       return "0000";
71
72   if (type_of(m) == CASTLING && !chess960)
73       to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
74
75   string move = to_string(from) + to_string(to);
76
77   if (type_of(m) == PROMOTION)
78       move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
79
80   return move;
81 }
82
83
84 /// move_from_uci() takes a position and a string representing a move in
85 /// simple coordinate notation and returns an equivalent legal Move if any.
86
87 Move move_from_uci(const Position& pos, string& str) {
88
89   if (str.length() == 5) // Junior could send promotion piece in uppercase
90       str[4] = char(tolower(str[4]));
91
92   for (MoveList<LEGAL> it(pos); *it; ++it)
93       if (str == move_to_uci(*it, pos.is_chess960()))
94           return *it;
95
96   return MOVE_NONE;
97 }
98
99
100 /// pretty_pv() formats human-readable search information, typically to be
101 /// appended to the search log file. It uses the two helpers below to pretty
102 /// format the time and score respectively.
103
104 static string format(int64_t msecs) {
105
106   const int MSecMinute = 1000 * 60;
107   const int MSecHour   = 1000 * 60 * 60;
108
109   int64_t hours   =   msecs / MSecHour;
110   int64_t minutes =  (msecs % MSecHour) / MSecMinute;
111   int64_t seconds = ((msecs % MSecHour) % MSecMinute) / 1000;
112
113   stringstream ss;
114
115   if (hours)
116       ss << hours << ':';
117
118   ss << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
119
120   return ss.str();
121 }
122
123 static string format(Value v) {
124
125   stringstream ss;
126
127   if (v >= VALUE_MATE_IN_MAX_PLY)
128       ss << "#" << (VALUE_MATE - v + 1) / 2;
129
130   else if (v <= VALUE_MATED_IN_MAX_PLY)
131       ss << "-#" << (VALUE_MATE + v) / 2;
132
133   else
134       ss << setprecision(2) << fixed << showpos << double(v) / PawnValueEg;
135
136   return ss.str();
137 }
138
139 string pretty_pv(const Position& pos, int depth, Value value, int64_t msecs, Move pv[]) {
140
141   const uint64_t K = 1000;
142   const uint64_t M = 1000000;
143
144   stringstream ss;
145   ss << setw(2) << depth << setw(8) << format(value) << setw(8) << format(msecs);
146
147   if (pos.nodes_searched() < M)
148       ss << setw(8) << pos.nodes_searched() / 1 << "  ";
149
150   else if (pos.nodes_searched() < K * M)
151       ss << setw(7) << pos.nodes_searched() / K << "K  ";
152
153   else
154       ss << setw(7) << pos.nodes_searched() / M << "M  ";
155
156   string str = ss.str();
157
158   for (Move *m = pv; *m != MOVE_NONE; m++)
159       str += move_to_uci(*m, pos.is_chess960()) + ' ';
160
161   return str;
162 }