]> git.sesse.net Git - stockfish/blob - src/move.cpp
Ressurect move.cpp
[stockfish] / src / move.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-2010 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
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <cstring>
27 #include <iomanip>
28 #include <string>
29 #include <sstream>
30
31 #include "move.h"
32 #include "movegen.h"
33
34 using std::string;
35
36 ////
37 //// Local definitions
38 ////
39
40 namespace {
41
42   enum Ambiguity {
43     AMBIGUITY_NONE, AMBIGUITY_FILE, AMBIGUITY_RANK, AMBIGUITY_BOTH
44   };
45
46   Ambiguity move_ambiguity(const Position& pos, Move m);
47   const string time_string(int milliseconds);
48   const string score_string(Value v);
49 }
50
51
52 ////
53 //// Functions
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
58 /// print in the e1g1 notation in normal chess mode, and in e1h1 notation in
59 /// Chess960 mode.
60
61 const std::string move_to_uci(Move m, bool chess960) {
62
63   std::string promotion;
64   Square from = move_from(m);
65   Square to = move_to(m);
66
67   if (m == MOVE_NONE)
68       return "(none)";
69
70   if (m == MOVE_NULL)
71       return "0000";
72
73   if (move_is_short_castle(m) && !chess960)
74       return from == SQ_E1 ? "e1g1" : "e8g8";
75
76   if (move_is_long_castle(m) && !chess960)
77       return from == SQ_E1 ? "e1c1" : "e8c8";
78
79   if (move_is_promotion(m))
80       promotion = char(tolower(piece_type_to_char(move_promotion_piece(m))));
81
82   return square_to_string(from) + square_to_string(to) + promotion;
83 }
84
85
86 /// move_from_uci() takes a position and a string representing a move in
87 /// simple coordinate notation and returns an equivalent Move.
88
89 Move move_from_uci(const Position& pos, const std::string& str) {
90
91   MoveStack mlist[MOVES_MAX];
92   MoveStack* last = generate<MV_LEGAL>(pos, mlist);
93
94   for (MoveStack* cur = mlist; cur != last; cur++)
95       if (str == move_to_uci(cur->move, pos.is_chess960()))
96           return cur->move;
97
98   return MOVE_NONE;
99 }
100
101
102 /// move_to_san() takes a position and a move as input, where it is assumed
103 /// that the move is a legal move from the position. The return value is
104 /// a string containing the move in short algebraic notation.
105
106 const string move_to_san(Position& pos, Move m) {
107
108   assert(pos.is_ok());
109   assert(move_is_ok(m));
110
111   string san;
112   Square from = move_from(m);
113   Square to = move_to(m);
114   PieceType pt = type_of_piece(pos.piece_on(from));
115
116   if (m == MOVE_NONE)
117       return "(none)";
118
119   if (m == MOVE_NULL)
120       return "(null)";
121
122   if (move_is_long_castle(m))
123       san = "O-O-O";
124   else if (move_is_short_castle(m))
125       san = "O-O";
126   else
127   {
128       if (pt != PAWN)
129       {
130           san += piece_type_to_char(pt);
131
132           switch (move_ambiguity(pos, m)) {
133           case AMBIGUITY_NONE:
134             break;
135           case AMBIGUITY_FILE:
136             san += file_to_char(square_file(from));
137             break;
138           case AMBIGUITY_RANK:
139             san += rank_to_char(square_rank(from));
140             break;
141           case AMBIGUITY_BOTH:
142             san += square_to_string(from);
143             break;
144           default:
145             assert(false);
146           }
147       }
148
149       if (pos.move_is_capture(m))
150       {
151           if (pt == PAWN)
152               san += file_to_char(square_file(from));
153
154           san += 'x';
155       }
156       san += square_to_string(to);
157
158       if (move_is_promotion(m))
159       {
160           san += '=';
161           san += piece_type_to_char(move_promotion_piece(m));
162       }
163   }
164
165   // The move gives check ? We don't use pos.move_is_check() here
166   // because we need to test for mate after the move is done.
167   StateInfo st;
168   pos.do_move(m, st);
169   if (pos.is_check())
170       san += pos.is_mate() ? "#" : "+";
171   pos.undo_move(m);
172
173   return san;
174 }
175
176
177 /// line_to_san() takes a position and a line (an array of moves representing
178 /// a sequence of legal moves from the position) as input, and returns a
179 /// string containing the line in short algebraic notation.  If the boolean
180 /// parameter 'breakLines' is true, line breaks are inserted, with a line
181 /// length of 80 characters.  After a line break, 'startColumn' spaces are
182 /// inserted at the beginning of the new line.
183
184 const string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
185
186   StateInfo st;
187   std::stringstream s;
188   string moveStr;
189   size_t length = 0;
190   size_t maxLength = 80 - startColumn;
191   Position p(pos, pos.thread());
192
193   for (Move* m = line; *m != MOVE_NONE; m++)
194   {
195       moveStr = move_to_san(p, *m);
196       length += moveStr.length() + 1;
197       if (breakLines && length > maxLength)
198       {
199           s << "\n" << std::setw(startColumn) << " ";
200           length = moveStr.length() + 1;
201       }
202       s << moveStr << ' ';
203
204       if (*m == MOVE_NULL)
205           p.do_null_move(st);
206       else
207           p.do_move(*m, st);
208   }
209   return s.str();
210 }
211
212
213 /// pretty_pv() creates a human-readable string from a position and a PV.
214 /// It is used to write search information to the log file (which is created
215 /// when the UCI parameter "Use Search Log" is "true").
216
217 const string pretty_pv(const Position& pos, int time, int depth,
218                        Value score, ValueType type, Move pv[]) {
219
220   const int64_t K = 1000;
221   const int64_t M = 1000000;
222
223   std::stringstream s;
224
225   // Depth
226   s << std::setw(2) << depth << "  ";
227
228   // Score
229   s << (type == VALUE_TYPE_LOWER ? ">" : type == VALUE_TYPE_UPPER ? "<" : " ")
230     << std::setw(7) << score_string(score);
231
232   // Time
233   s << std::setw(8) << time_string(time) << " ";
234
235   // Nodes
236   if (pos.nodes_searched() < M)
237       s << std::setw(8) << pos.nodes_searched() / 1 << " ";
238   else if (pos.nodes_searched() < K * M)
239       s << std::setw(7) << pos.nodes_searched() / K << "K ";
240   else
241       s << std::setw(7) << pos.nodes_searched() / M << "M ";
242
243   // PV
244   s << line_to_san(pos, pv, 30, true);
245
246   return s.str();
247 }
248
249
250 namespace {
251
252   Ambiguity move_ambiguity(const Position& pos, Move m) {
253
254     MoveStack mlist[MOVES_MAX], *last;
255     Move candidates[8];
256     Square from = move_from(m);
257     Square to = move_to(m);
258     Piece pc = pos.piece_on(from);
259     int matches = 0, f = 0, r = 0;
260
261     // If there is only one piece 'pc' then move cannot be ambiguous
262     if (pos.piece_count(pos.side_to_move(), type_of_piece(pc)) == 1)
263         return AMBIGUITY_NONE;
264
265     // Collect all legal moves of piece 'pc' with destination 'to'
266     last = generate<MV_LEGAL>(pos, mlist);
267     for (MoveStack* cur = mlist; cur != last; cur++)
268         if (move_to(cur->move) == to && pos.piece_on(move_from(cur->move)) == pc)
269             candidates[matches++] = cur->move;
270
271     if (matches == 1)
272         return AMBIGUITY_NONE;
273
274     for (int i = 0; i < matches; i++)
275     {
276         if (square_file(move_from(candidates[i])) == square_file(from))
277             f++;
278
279         if (square_rank(move_from(candidates[i])) == square_rank(from))
280             r++;
281     }
282
283     return f == 1 ? AMBIGUITY_FILE : r == 1 ? AMBIGUITY_RANK : AMBIGUITY_BOTH;
284   }
285
286
287   const string time_string(int millisecs) {
288
289     const int MSecMinute = 1000 * 60;
290     const int MSecHour   = 1000 * 60 * 60;
291
292     std::stringstream s;
293     s << std::setfill('0');
294
295     int hours = millisecs / MSecHour;
296     int minutes = (millisecs - hours * MSecHour) / MSecMinute;
297     int seconds = (millisecs - hours * MSecHour - minutes * MSecMinute) / 1000;
298
299     if (hours)
300         s << hours << ':';
301
302     s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
303     return s.str();
304   }
305
306
307   const string score_string(Value v) {
308
309     std::stringstream s;
310
311     if (v >= VALUE_MATE - 200)
312         s << "#" << (VALUE_MATE - v + 1) / 2;
313     else if (v <= -VALUE_MATE + 200)
314         s << "-#" << (VALUE_MATE + v) / 2;
315     else
316     {
317         float floatScore = float(v) / float(PawnValueMidgame);
318         if (v >= 0)
319             s << '+';
320
321         s << std::setprecision(2) << std::fixed << floatScore;
322     }
323     return s.str();
324   }
325 }