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