]> git.sesse.net Git - stockfish/blob - src/san.cpp
Fix a silly bug that disabled second killer
[stockfish] / src / san.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 Marco Costalba
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
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <cstring>
27 #include <iomanip>
28 #include <string>
29 #include <sstream>
30
31 #include "movepick.h"
32 #include "san.h"
33
34
35 ////
36 //// Local definitions
37 ////
38
39 namespace {
40
41   /// Types
42
43   enum Ambiguity {
44     AMBIGUITY_NONE,
45     AMBIGUITY_FILE,
46     AMBIGUITY_RANK,
47     AMBIGUITY_BOTH
48   };
49
50
51   /// Functions
52
53   Ambiguity move_ambiguity(Position &pos, Move m);
54   const std::string time_string(int milliseconds);
55   const std::string score_string(Value v);
56 }
57
58
59 ////
60 //// Functions
61 ////
62
63 /// move_to_san() takes a position and a move as input, where it is assumed
64 /// that the move is a legal move from the position.  The return value is
65 /// a string containing the move in short algebraic notation.
66
67 const std::string move_to_san(Position &pos, Move m) {
68   std::string str;
69
70   assert(pos.is_ok());
71   assert(move_is_ok(m));
72
73   if(m == MOVE_NONE) {
74     str = "(none)";
75     return str;
76   }
77   else if(m == MOVE_NULL) {
78     str = "(null)";
79     return str;
80   }
81   else if(move_is_long_castle(m))
82     str = "O-O-O";
83   else if(move_is_short_castle(m))
84     str = "O-O";
85   else {
86     Square from, to;
87     Piece pc;
88
89     from = move_from(m);
90     to = move_to(m);
91     pc = pos.piece_on(move_from(m));
92
93     str = "";
94
95     if(type_of_piece(pc) == PAWN) {
96       if(pos.move_is_capture(m))
97         str += file_to_char(square_file(move_from(m)));
98     }
99     else {
100       str += piece_type_to_char(type_of_piece(pc), true);
101
102       Ambiguity amb = move_ambiguity(pos, m);
103       switch(amb) {
104
105       case AMBIGUITY_NONE:
106         break;
107
108       case AMBIGUITY_FILE:
109         str += file_to_char(square_file(from));
110         break;
111
112       case AMBIGUITY_RANK:
113         str += rank_to_char(square_rank(from));
114         break;
115
116       case AMBIGUITY_BOTH:
117         str += square_to_string(from);
118         break;
119
120       default:
121         assert(false);
122       }
123     }
124
125     if(pos.move_is_capture(m))
126       str += "x";
127
128     str += square_to_string(move_to(m));
129
130     if(move_promotion(m)) {
131       str += "=";
132       str += piece_type_to_char(move_promotion(m), true);
133     }
134   }
135
136   // Is the move check?  We don't use pos.move_is_check(m) here, because
137   // Position::move_is_check doesn't detect all checks (not castling moves,
138   // promotions and en passant captures).
139   UndoInfo u;
140   pos.do_move(m, u);
141   if(pos.is_check())
142     str += pos.is_mate()? "#" : "+";
143   pos.undo_move(m, u);
144
145   return str;
146 }
147
148
149 /// move_from_san() takes a position and a string as input, and tries to
150 /// interpret the string as a move in short algebraic notation.  On success,
151 /// the move is returned.  On failure (i.e. if the string is unparsable, or
152 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
153
154 Move move_from_san(Position &pos, const std::string &movestr) {
155   assert(pos.is_ok());
156
157   MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
158
159   // Castling moves
160   if(movestr == "O-O-O") {
161     Move m;
162     while((m = mp.get_next_move()) != MOVE_NONE)
163       if(move_is_long_castle(m) && pos.pl_move_is_legal(m))
164         return m;
165     return MOVE_NONE;
166   }
167   else if(movestr == "O-O") {
168     Move m;
169     while((m = mp.get_next_move()) != MOVE_NONE)
170       if(move_is_short_castle(m) && pos.pl_move_is_legal(m))
171         return m;
172     return MOVE_NONE;
173   }
174
175   // Normal moves
176   const char *cstr = movestr.c_str();
177   const char *c;
178   char *cc;
179   char str[10];
180   int i;
181
182   // Initialize str[] by making a copy of movestr with the characters
183   // 'x', '=', '+' and '#' removed.
184   cc = str;
185   for(i=0, c=cstr; i<10 && *c!='\0' && *c!='\n' && *c!=' '; i++, c++)
186     if(!strchr("x=+#", *c)) {
187       *cc = strchr("nrq", *c)? toupper(*c) : *c;
188       cc++;
189     }
190   *cc = '\0';
191
192   size_t left = 0, right = strlen(str) - 1;
193   PieceType pt = NO_PIECE_TYPE, promotion;
194   Square to;
195   File fromFile = FILE_NONE;
196   Rank fromRank = RANK_NONE;
197
198   // Promotion?
199   if(strchr("BNRQ", str[right])) {
200     promotion = piece_type_from_char(str[right]);
201     right--;
202   }
203   else
204     promotion = NO_PIECE_TYPE;
205
206   // Find the moving piece:
207   if(left < right) {
208     if(strchr("BNRQK", str[left])) {
209       pt = piece_type_from_char(str[left]);
210       left++;
211     }
212     else
213       pt = PAWN;
214   }
215
216   // Find the to square:
217   if(left < right) {
218     if(str[right] < '1' || str[right] > '8' ||
219        str[right-1] < 'a' || str[right-1] > 'h')
220       return MOVE_NONE;
221     to = make_square(file_from_char(str[right-1]), rank_from_char(str[right]));
222     right -= 2;
223   }
224   else
225     return MOVE_NONE;
226
227   // Find the file and/or rank of the from square:
228   if(left <= right) {
229     if(strchr("abcdefgh", str[left])) {
230       fromFile = file_from_char(str[left]);
231       left++;
232     }
233     if(strchr("12345678", str[left]))
234       fromRank = rank_from_char(str[left]);
235   }
236
237   // Look for a matching move:
238   Move m, move = MOVE_NONE;
239   int matches = 0;
240
241   while((m = mp.get_next_move()) != MOVE_NONE) {
242     bool match = true;
243     if(pos.type_of_piece_on(move_from(m)) != pt)
244       match = false;
245     else if(move_to(m) != to)
246       match = false;
247     else if(move_promotion(m) != promotion)
248       match = false;
249     else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m)))
250       match = false;
251     else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m)))
252       match = false;
253     if(match) {
254       move = m;
255       matches++;
256     }
257   }
258
259   if(matches == 1)
260     return move;
261   else
262     return MOVE_NONE;
263 }
264
265
266 /// line_to_san() takes a position and a line (an array of moves representing
267 /// a sequence of legal moves from the position) as input, and returns a
268 /// string containing the line in short algebraic notation.  If the boolean
269 /// parameter 'breakLines' is true, line breaks are inserted, with a line
270 /// length of 80 characters.  After a line break, 'startColumn' spaces are
271 /// inserted at the beginning of the new line.
272
273 const std::string line_to_san(const Position &pos, Move line[], int startColumn,
274                               bool breakLines) {
275   Position p = Position(pos);
276   UndoInfo u;
277   std::stringstream s;
278   std::string moveStr;
279   size_t length, maxLength;
280
281   length = 0;
282   maxLength = 80 - startColumn;
283
284   for(int i = 0; line[i] != MOVE_NONE; i++) {
285     moveStr = move_to_san(p, line[i]);
286     length += moveStr.length() + 1;
287     if(breakLines && length > maxLength) {
288       s << "\n";
289       for(int j = 0; j < startColumn; j++)
290         s << " ";
291       length = moveStr.length() + 1;
292     }
293     s << moveStr << " ";
294
295     if(line[i] == MOVE_NULL)
296       p.do_null_move(u);
297     else
298       p.do_move(line[i], u);
299   }
300
301   return s.str();
302 }
303
304
305 /// pretty_pv() creates a human-readable string from a position and a PV.
306 /// It is used to write search information to the log file (which is created
307 /// when the UCI parameter "Use Search Log" is "true").
308
309 const std::string pretty_pv(const Position &pos, int time, int depth,
310                             uint64_t nodes, Value score, Move pv[]) {
311   std::stringstream s;
312
313   // Depth
314   s << std::setw(2) << std::setfill(' ') << depth << "  ";
315
316   // Score
317   s << std::setw(8) << score_string(score);
318
319   // Time
320   s << std::setw(8) << std::setfill(' ') << time_string(time) << " ";
321
322   // Nodes
323   if(nodes < 1000000ULL)
324     s << std::setw(8) << std::setfill(' ') << nodes << " ";
325   else if(nodes < 1000000000ULL)
326     s << std::setw(7) << std::setfill(' ') << nodes/1000ULL << 'k' << " ";
327   else
328     s << std::setw(7) << std::setfill(' ') << nodes/1000000ULL << 'M' << " ";
329
330   // PV
331   s << line_to_san(pos, pv, 30, true);
332
333   return s.str();
334 }
335
336
337 namespace {
338
339   Ambiguity move_ambiguity(Position &pos, Move m) {
340     Square from, to;
341     Piece pc;
342
343     from = move_from(m);
344     to = move_to(m);
345     pc = pos.piece_on(from);
346
347     // King moves are never ambiguous, because there is never two kings of
348     // the same color.
349     if(type_of_piece(pc) == KING)
350       return AMBIGUITY_NONE;
351
352     MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
353     Move mv, moveList[8];
354     int i, j, n;
355
356     n = 0;
357     while((mv = mp.get_next_move()) != MOVE_NONE)
358       if(move_to(mv) == to && pos.piece_on(move_from(mv)) == pc
359          && pos.pl_move_is_legal(mv))
360         moveList[n++] = mv;
361     if(n == 1)
362       return AMBIGUITY_NONE;
363
364     j = 0;
365     for(i = 0; i < n; i++)
366       if(square_file(move_from(moveList[i])) == square_file(from))
367         j++;
368     if(j == 1)
369       return AMBIGUITY_FILE;
370
371     j = 0;
372     for(i = 0; i < n; i++)
373       if(square_rank(move_from(moveList[i])) == square_rank(from))
374         j++;
375     if(j == 1)
376       return AMBIGUITY_RANK;
377
378     return AMBIGUITY_BOTH;
379   }
380
381
382   const std::string time_string(int milliseconds) {
383     std::stringstream s;
384
385     int hours = milliseconds / (1000 * 60 * 60);
386     int minutes = (milliseconds - hours*1000*60*60) / (60*1000);
387     int seconds = (milliseconds - hours*1000*60*60 - minutes*60*1000) / 1000;
388
389     if(hours)
390       s << hours << ':';
391     s << std::setw(2) << std::setfill('0') << minutes << ':';
392     s << std::setw(2) << std::setfill('0') << seconds;
393
394     return s.str();
395   }
396
397
398   const std::string score_string(Value v) {
399     std::stringstream s;
400
401     if(abs(v) >= VALUE_MATE - 200) {
402       if(v < 0)
403         s << "-#" << (VALUE_MATE + v) / 2;
404       else
405         s << "#" << (VALUE_MATE - v + 1) / 2;
406     }
407     else {
408       float floatScore = float(v) / float(PawnValueMidgame);
409       if(v >= 0)
410         s << '+';
411       s << std::setprecision(2) << std::fixed << floatScore;
412     }
413     return s.str();
414   }
415
416 }