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