]> git.sesse.net Git - stockfish/blob - src/move.cpp
Fix a race in pondering mode
[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 #include <cassert>
21 #include <cstring>
22 #include <string>
23
24 #include "move.h"
25 #include "movegen.h"
26 #include "position.h"
27
28 using std::string;
29
30 /// move_to_uci() converts a move to a string in coordinate notation
31 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we
32 /// print in the e1g1 notation in normal chess mode, and in e1h1 notation in
33 /// Chess960 mode. Instead internally Move is coded as "king captures rook".
34
35 const string move_to_uci(Move m, bool chess960) {
36
37   Square from = move_from(m);
38   Square to = move_to(m);
39   string promotion;
40
41   if (m == MOVE_NONE)
42       return "(none)";
43
44   if (m == MOVE_NULL)
45       return "0000";
46
47   if (is_castle(m) && !chess960)
48       to = from + (file_of(to) == FILE_H ? Square(2) : -Square(2));
49
50   if (is_promotion(m))
51       promotion = char(tolower(piece_type_to_char(promotion_piece_type(m))));
52
53   return square_to_string(from) + square_to_string(to) + promotion;
54 }
55
56
57 /// move_from_uci() takes a position and a string representing a move in
58 /// simple coordinate notation and returns an equivalent Move if any.
59 /// Moves are guaranteed to be legal.
60
61 Move move_from_uci(const Position& pos, const string& str) {
62
63   for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
64       if (str == move_to_uci(ml.move(), pos.is_chess960()))
65           return ml.move();
66
67   return MOVE_NONE;
68 }
69
70
71 /// move_to_san() takes a position and a move as input, where it is assumed
72 /// that the move is a legal move for the position. The return value is
73 /// a string containing the move in short algebraic notation.
74
75 const string move_to_san(Position& pos, Move m) {
76
77   if (m == MOVE_NONE)
78       return "(none)";
79
80   if (m == MOVE_NULL)
81       return "(null)";
82
83   assert(is_ok(m));
84
85   Bitboard attackers;
86   bool ambiguousMove, ambiguousFile, ambiguousRank;
87   Square sq, from = move_from(m);
88   Square to = move_to(m);
89   PieceType pt = type_of(pos.piece_on(from));
90   string san;
91
92   if (is_castle(m))
93       san = (move_to(m) < move_from(m) ? "O-O-O" : "O-O");
94   else
95   {
96       if (pt != PAWN)
97       {
98           san = piece_type_to_char(pt);
99
100           // Disambiguation if we have more then one piece with destination 'to'
101           // note that for pawns is not needed because starting file is explicit.
102           attackers = pos.attackers_to(to) & pos.pieces(pt, pos.side_to_move());
103           clear_bit(&attackers, from);
104           ambiguousMove = ambiguousFile = ambiguousRank = false;
105
106           while (attackers)
107           {
108               sq = pop_1st_bit(&attackers);
109
110               if (file_of(sq) == file_of(from))
111                   ambiguousFile = true;
112
113               if (rank_of(sq) == rank_of(from))
114                   ambiguousRank = true;
115
116               ambiguousMove = true;
117           }
118
119           if (ambiguousMove)
120           {
121               if (!ambiguousFile)
122                   san += file_to_char(file_of(from));
123               else if (!ambiguousRank)
124                   san += rank_to_char(rank_of(from));
125               else
126                   san += square_to_string(from);
127           }
128       }
129
130       if (pos.is_capture(m))
131       {
132           if (pt == PAWN)
133               san += file_to_char(file_of(from));
134
135           san += 'x';
136       }
137
138       san += square_to_string(to);
139
140       if (is_promotion(m))
141       {
142           san += '=';
143           san += piece_type_to_char(promotion_piece_type(m));
144       }
145   }
146
147   // The move gives check? We don't use pos.move_gives_check() here
148   // because we need to test for a mate after the move is done.
149   StateInfo st;
150   pos.do_move(m, st);
151   if (pos.in_check())
152       san += pos.is_mate() ? "#" : "+";
153   pos.undo_move(m);
154
155   return san;
156 }