]> git.sesse.net Git - stockfish/blob - src/notation.cpp
Merge exclusion search conditions
[stockfish] / src / notation.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 static const char* PieceToChar = " PNBRQK pnbrqk";
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 print
32 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
33 /// mode. Internally castle moves are always coded as "king captures rook".
34
35 const string move_to_uci(Move m, bool chess960) {
36
37   Square from = from_sq(m);
38   Square to = to_sq(m);
39
40   if (m == MOVE_NONE)
41       return "(none)";
42
43   if (m == MOVE_NULL)
44       return "0000";
45
46   if (type_of(m) == CASTLE && !chess960)
47       to = (to > from ? FILE_G : FILE_C) | rank_of(from);
48
49   string move = square_to_string(from) + square_to_string(to);
50
51   if (type_of(m) == PROMOTION)
52       move += PieceToChar[promotion_type(m) + 7]; // Lower case
53
54   return move;
55 }
56
57
58 /// move_from_uci() takes a position and a string representing a move in
59 /// simple coordinate notation and returns an equivalent legal Move if any.
60
61 Move move_from_uci(const Position& pos, string& str) {
62
63   if (str.length() == 5) // Junior could send promotion piece in uppercase
64       str[4] = char(tolower(str[4]));
65
66   for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
67       if (str == move_to_uci(ml.move(), pos.is_chess960()))
68           return ml.move();
69
70   return MOVE_NONE;
71 }
72
73
74 /// move_to_san() takes a position and a legal Move as input and returns its
75 /// short algebraic notation representation.
76
77 const string move_to_san(Position& pos, Move m) {
78
79   if (m == MOVE_NONE)
80       return "(none)";
81
82   if (m == MOVE_NULL)
83       return "(null)";
84
85   assert(pos.move_is_legal(m));
86
87   Bitboard attackers;
88   bool ambiguousMove, ambiguousFile, ambiguousRank;
89   string san;
90   Color us = pos.side_to_move();
91   Square from = from_sq(m);
92   Square to = to_sq(m);
93   Piece pc = pos.piece_on(from);
94
95   if (type_of(m) == CASTLE)
96       san = to > from ? "O-O" : "O-O-O";
97   else
98   {
99       if (type_of(pc) != PAWN)
100       {
101           san = PieceToChar[pc];
102
103           // Disambiguation if we have more then one piece with destination 'to'
104           // note that for pawns is not needed because starting file is explicit.
105           ambiguousMove = ambiguousFile = ambiguousRank = false;
106
107           attackers = (pos.attacks_from(pc, to) & pos.pieces(us)) ^ from;
108
109           while (attackers)
110           {
111               Square sq = pop_lsb(&attackers);
112
113               // Pinned pieces are not included in the possible sub-set
114               if (!pos.pl_move_is_legal(make_move(sq, to), pos.pinned_pieces()))
115                   continue;
116
117               ambiguousFile |= file_of(sq) == file_of(from);
118               ambiguousRank |= rank_of(sq) == rank_of(from);
119               ambiguousMove = true;
120           }
121
122           if (ambiguousMove)
123           {
124               if (!ambiguousFile)
125                   san += file_to_char(file_of(from));
126
127               else if (!ambiguousRank)
128                   san += rank_to_char(rank_of(from));
129
130               else
131                   san += square_to_string(from);
132           }
133       }
134       else if (pos.is_capture(m))
135           san = file_to_char(file_of(from));
136
137       if (pos.is_capture(m))
138           san += 'x';
139
140       san += square_to_string(to);
141
142       if (type_of(m) == PROMOTION)
143           san += string("=") + PieceToChar[promotion_type(m)];
144   }
145
146   if (pos.move_gives_check(m, CheckInfo(pos)))
147   {
148       StateInfo st;
149       pos.do_move(m, st);
150       san += MoveList<LEGAL>(pos).size() ? "+" : "#";
151       pos.undo_move(m);
152   }
153
154   return san;
155 }