]> git.sesse.net Git - stockfish/blob - src/move.cpp
Rename ThreadsManager to ThreadPool
[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 print
30 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
31 /// mode. Internally castle moves are always 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 = (to > from ? FILE_G : FILE_C) | rank_of(from);
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 legal Move if any.
57
58 Move move_from_uci(const Position& pos, string& str) {
59
60   if (str.length() == 5) // Junior could send promotion piece in uppercase
61       str[4] = char(tolower(str[4]));
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 legal Move as input and returns its
72 /// short algebraic notation representation.
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(pos.move_is_legal(m));
83
84   Bitboard attackers;
85   bool ambiguousMove, ambiguousFile, ambiguousRank;
86   string san;
87   Square from = from_sq(m);
88   Square to = to_sq(m);
89   PieceType pt = type_of(pos.piece_on(from));
90
91   if (is_castle(m))
92       san = to > from ? "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(pos.side_to_move(), pt);
102           attackers ^= from;
103           ambiguousMove = ambiguousFile = ambiguousRank = false;
104
105           while (attackers)
106           {
107               Square 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               ambiguousFile |= file_of(sq) == file_of(from);
114               ambiguousRank |= rank_of(sq) == rank_of(from);
115               ambiguousMove = true;
116           }
117
118           if (ambiguousMove)
119           {
120               if (!ambiguousFile)
121                   san += file_to_char(file_of(from));
122
123               else if (!ambiguousRank)
124                   san += rank_to_char(rank_of(from));
125
126               else
127                   san += square_to_string(from);
128           }
129       }
130
131       if (pos.is_capture(m))
132       {
133           if (pt == PAWN)
134               san += file_to_char(file_of(from));
135
136           san += 'x';
137       }
138
139       san += square_to_string(to);
140
141       if (is_promotion(m))
142           san += string("=") + piece_type_to_char(promotion_type(m));
143   }
144
145   if (pos.move_gives_check(m, CheckInfo(pos)))
146   {
147       StateInfo st;
148       pos.do_move(m, st);
149       san += MoveList<MV_LEGAL>(pos).size() ? "+" : "#";
150       pos.undo_move(m);
151   }
152
153   return san;
154 }