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