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