]> git.sesse.net Git - stockfish/blob - src/notation.cpp
Fix regression in move_to_san()
[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-2012 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 <string>
24
25 #include "movegen.h"
26 #include "notation.h"
27 #include "position.h"
28
29 using namespace std;
30
31 static const char* PieceToChar = " 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 s;
44
45   if (abs(v) < VALUE_MATE_IN_MAX_PLY)
46       s << "cp " << v * 100 / int(PawnValueMidgame);
47   else
48       s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
49
50   s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
51
52   return s.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 castle moves are always coded 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) == CASTLE && !chess960)
73       to = (to > from ? FILE_G : FILE_C) | rank_of(from);
74
75   string move = square_to_string(from) + square_to_string(to);
76
77   if (type_of(m) == PROMOTION)
78       move += PieceToChar[make_piece(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> ml(pos); !ml.end(); ++ml)
93       if (str == move_to_uci(ml.move(), pos.is_chess960()))
94           return ml.move();
95
96   return MOVE_NONE;
97 }
98
99
100 /// move_to_san() takes a position and a legal Move as input and returns its
101 /// short algebraic notation representation.
102
103 const string move_to_san(Position& pos, Move m) {
104
105   if (m == MOVE_NONE)
106       return "(none)";
107
108   if (m == MOVE_NULL)
109       return "(null)";
110
111   assert(pos.move_is_legal(m));
112
113   Bitboard attackers;
114   bool ambiguousMove, ambiguousFile, ambiguousRank;
115   string san;
116   Color us = pos.side_to_move();
117   Square from = from_sq(m);
118   Square to = to_sq(m);
119   Piece pc = pos.piece_on(from);
120   PieceType pt = type_of(pc);
121
122   if (type_of(m) == CASTLE)
123       san = to > from ? "O-O" : "O-O-O";
124   else
125   {
126       if (pt != PAWN)
127       {
128           san = PieceToChar[pt]; // Upper case
129
130           // Disambiguation if we have more then one piece with destination 'to'
131           // note that for pawns is not needed because starting file is explicit.
132           ambiguousMove = ambiguousFile = ambiguousRank = false;
133
134           attackers = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
135
136           while (attackers)
137           {
138               Square sq = pop_lsb(&attackers);
139
140               // Pinned pieces are not included in the possible sub-set
141               if (!pos.pl_move_is_legal(make_move(sq, to), pos.pinned_pieces()))
142                   continue;
143
144               ambiguousFile |= file_of(sq) == file_of(from);
145               ambiguousRank |= rank_of(sq) == rank_of(from);
146               ambiguousMove = true;
147           }
148
149           if (ambiguousMove)
150           {
151               if (!ambiguousFile)
152                   san += file_to_char(file_of(from));
153
154               else if (!ambiguousRank)
155                   san += rank_to_char(rank_of(from));
156
157               else
158                   san += square_to_string(from);
159           }
160       }
161       else if (pos.is_capture(m))
162           san = file_to_char(file_of(from));
163
164       if (pos.is_capture(m))
165           san += 'x';
166
167       san += square_to_string(to);
168
169       if (type_of(m) == PROMOTION)
170           san += string("=") + PieceToChar[promotion_type(m)];
171   }
172
173   if (pos.move_gives_check(m, CheckInfo(pos)))
174   {
175       StateInfo st;
176       pos.do_move(m, st);
177       san += MoveList<LEGAL>(pos).size() ? "+" : "#";
178       pos.undo_move(m);
179   }
180
181   return san;
182 }
183
184
185 /// pretty_pv() formats human-readable search information, typically to be
186 /// appended to the search log file. It uses the two helpers below to pretty
187 /// format time and score respectively.
188
189 static string time_to_string(int millisecs) {
190
191   const int MSecMinute = 1000 * 60;
192   const int MSecHour   = 1000 * 60 * 60;
193
194   int hours = millisecs / MSecHour;
195   int minutes =  (millisecs % MSecHour) / MSecMinute;
196   int seconds = ((millisecs % MSecHour) % MSecMinute) / 1000;
197
198   stringstream s;
199
200   if (hours)
201       s << hours << ':';
202
203   s << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
204
205   return s.str();
206 }
207
208 static string score_to_string(Value v) {
209
210   stringstream s;
211
212   if (v >= VALUE_MATE_IN_MAX_PLY)
213       s << "#" << (VALUE_MATE - v + 1) / 2;
214
215   else if (v <= VALUE_MATED_IN_MAX_PLY)
216       s << "-#" << (VALUE_MATE + v) / 2;
217
218   else
219       s << setprecision(2) << fixed << showpos << float(v) / PawnValueMidgame;
220
221   return s.str();
222 }
223
224 string pretty_pv(Position& pos, int depth, Value value, int time, Move pv[]) {
225
226   const int64_t K = 1000;
227   const int64_t M = 1000000;
228
229   StateInfo state[MAX_PLY_PLUS_2], *st = state;
230   Move* m = pv;
231   string san, padding;
232   size_t length;
233   stringstream s;
234
235   s << setw(2) << depth
236     << setw(8) << score_to_string(value)
237     << setw(8) << time_to_string(time);
238
239   if (pos.nodes_searched() < M)
240       s << setw(8) << pos.nodes_searched() / 1 << "  ";
241
242   else if (pos.nodes_searched() < K * M)
243       s << setw(7) << pos.nodes_searched() / K << "K  ";
244
245   else
246       s << setw(7) << pos.nodes_searched() / M << "M  ";
247
248   padding = string(s.str().length(), ' ');
249   length = padding.length();
250
251   while (*m != MOVE_NONE)
252   {
253       san = move_to_san(pos, *m);
254
255       if (length + san.length() > 80)
256       {
257           s << "\n" + padding;
258           length = padding.length();
259       }
260
261       s << san << ' ';
262       length += san.length() + 1;
263
264       pos.do_move(*m++, *st++);
265   }
266
267   while (m != pv)
268       pos.undo_move(*--m);
269
270   return s.str();
271 }