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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5 Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef POSITION_H_INCLUDED
22 #define POSITION_H_INCLUDED
25 #include <cstddef> // For offsetof()
27 #include <memory> // For std::unique_ptr
39 extern Score psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
44 /// CheckInfo struct is initialized at constructor time and keeps info used to
45 /// detect if a move gives check.
49 explicit CheckInfo(const Position&);
51 Bitboard dcCandidates;
53 Bitboard checkSquares[PIECE_TYPE_NB];
58 /// StateInfo struct stores information needed to restore a Position object to
59 /// its previous state when we retract a move. Whenever a move is made on the
60 /// board (by calling Position::do_move), a StateInfo object must be passed.
64 // Copied when making a move
67 Value nonPawnMaterial[COLOR_NB];
74 // Not copied when making a move
77 PieceType capturedType;
81 // In a std::deque references to elements are unaffected upon resizing
82 typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
85 /// Position class stores information regarding the board representation as
86 /// pieces, side to move, hash keys, castling info, etc. Important methods are
87 /// do_move() and undo_move(), used by the search to update node info when
88 /// traversing the search tree.
96 Position(const Position&) = delete;
97 Position& operator=(const Position&) = delete;
99 // FEN string input/output
100 Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
101 const std::string fen() const;
103 // Position representation
104 Bitboard pieces() const;
105 Bitboard pieces(PieceType pt) const;
106 Bitboard pieces(PieceType pt1, PieceType pt2) const;
107 Bitboard pieces(Color c) const;
108 Bitboard pieces(Color c, PieceType pt) const;
109 Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
110 Piece piece_on(Square s) const;
111 Square ep_square() const;
112 bool empty(Square s) const;
113 template<PieceType Pt> int count(Color c) const;
114 template<PieceType Pt> const Square* squares(Color c) const;
115 template<PieceType Pt> Square square(Color c) const;
118 int can_castle(Color c) const;
119 int can_castle(CastlingRight cr) const;
120 bool castling_impeded(CastlingRight cr) const;
121 Square castling_rook_square(CastlingRight cr) const;
124 Bitboard checkers() const;
125 Bitboard discovered_check_candidates() const;
126 Bitboard pinned_pieces(Color c) const;
128 // Attacks to/from a given square
129 Bitboard attackers_to(Square s) const;
130 Bitboard attackers_to(Square s, Bitboard occupied) const;
131 Bitboard attacks_from(Piece pc, Square s) const;
132 template<PieceType> Bitboard attacks_from(Square s) const;
133 template<PieceType> Bitboard attacks_from(Square s, Color c) const;
134 Bitboard slider_blockers(Bitboard target, Bitboard sliders, Square s) const;
136 // Properties of moves
137 bool legal(Move m, Bitboard pinned) const;
138 bool pseudo_legal(const Move m) const;
139 bool capture(Move m) const;
140 bool capture_or_promotion(Move m) const;
141 bool gives_check(Move m, const CheckInfo& ci) const;
142 bool advanced_pawn_push(Move m) const;
143 Piece moved_piece(Move m) const;
144 PieceType captured_piece_type() const;
147 bool pawn_passed(Color c, Square s) const;
148 bool opposite_bishops() const;
150 // Doing and undoing moves
151 void do_move(Move m, StateInfo& st, bool givesCheck);
152 void undo_move(Move m);
153 void do_null_move(StateInfo& st);
154 void undo_null_move();
156 // Static exchange evaluation
157 Value see(Move m) const;
158 Value see_sign(Move m) const;
160 // Accessing hash keys
162 Key key_after(Move m) const;
163 Key exclusion_key() const;
164 Key material_key() const;
165 Key pawn_key() const;
167 // Other properties of the position
168 Color side_to_move() const;
169 Phase game_phase() const;
170 int game_ply() const;
171 bool is_chess960() const;
172 Thread* this_thread() const;
173 uint64_t nodes_searched() const;
174 void set_nodes_searched(uint64_t n);
175 bool is_draw() const;
176 int rule50_count() const;
177 Score psq_score() const;
178 Value non_pawn_material(Color c) const;
180 // Position consistency check, for debugging
181 bool pos_is_ok(int* failedStep = nullptr) const;
185 // Initialization helpers (used while setting up a position)
186 void set_castling_right(Color c, Square rfrom);
187 void set_state(StateInfo* si) const;
190 void put_piece(Color c, PieceType pt, Square s);
191 void remove_piece(Color c, PieceType pt, Square s);
192 void move_piece(Color c, PieceType pt, Square from, Square to);
194 void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
197 Piece board[SQUARE_NB];
198 Bitboard byTypeBB[PIECE_TYPE_NB];
199 Bitboard byColorBB[COLOR_NB];
200 int pieceCount[COLOR_NB][PIECE_TYPE_NB];
201 Square pieceList[COLOR_NB][PIECE_TYPE_NB][16];
202 int index[SQUARE_NB];
203 int castlingRightsMask[SQUARE_NB];
204 Square castlingRookSquare[CASTLING_RIGHT_NB];
205 Bitboard castlingPath[CASTLING_RIGHT_NB];
214 extern std::ostream& operator<<(std::ostream& os, const Position& pos);
216 inline Color Position::side_to_move() const {
220 inline bool Position::empty(Square s) const {
221 return board[s] == NO_PIECE;
224 inline Piece Position::piece_on(Square s) const {
228 inline Piece Position::moved_piece(Move m) const {
229 return board[from_sq(m)];
232 inline Bitboard Position::pieces() const {
233 return byTypeBB[ALL_PIECES];
236 inline Bitboard Position::pieces(PieceType pt) const {
240 inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
241 return byTypeBB[pt1] | byTypeBB[pt2];
244 inline Bitboard Position::pieces(Color c) const {
248 inline Bitboard Position::pieces(Color c, PieceType pt) const {
249 return byColorBB[c] & byTypeBB[pt];
252 inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
253 return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
256 template<PieceType Pt> inline int Position::count(Color c) const {
257 return pieceCount[c][Pt];
260 template<PieceType Pt> inline const Square* Position::squares(Color c) const {
261 return pieceList[c][Pt];
264 template<PieceType Pt> inline Square Position::square(Color c) const {
265 assert(pieceCount[c][Pt] == 1);
266 return pieceList[c][Pt][0];
269 inline Square Position::ep_square() const {
273 inline int Position::can_castle(CastlingRight cr) const {
274 return st->castlingRights & cr;
277 inline int Position::can_castle(Color c) const {
278 return st->castlingRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
281 inline bool Position::castling_impeded(CastlingRight cr) const {
282 return byTypeBB[ALL_PIECES] & castlingPath[cr];
285 inline Square Position::castling_rook_square(CastlingRight cr) const {
286 return castlingRookSquare[cr];
289 template<PieceType Pt>
290 inline Bitboard Position::attacks_from(Square s) const {
291 return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
292 : Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
293 : StepAttacksBB[Pt][s];
297 inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
298 return StepAttacksBB[make_piece(c, PAWN)][s];
301 inline Bitboard Position::attacks_from(Piece pc, Square s) const {
302 return attacks_bb(pc, s, byTypeBB[ALL_PIECES]);
305 inline Bitboard Position::attackers_to(Square s) const {
306 return attackers_to(s, byTypeBB[ALL_PIECES]);
309 inline Bitboard Position::checkers() const {
310 return st->checkersBB;
313 inline Bitboard Position::discovered_check_candidates() const {
314 return slider_blockers(pieces(sideToMove), pieces(sideToMove), square<KING>(~sideToMove));
317 inline Bitboard Position::pinned_pieces(Color c) const {
318 return slider_blockers(pieces(c), pieces(~c), square<KING>(c));
321 inline bool Position::pawn_passed(Color c, Square s) const {
322 return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
325 inline bool Position::advanced_pawn_push(Move m) const {
326 return type_of(moved_piece(m)) == PAWN
327 && relative_rank(sideToMove, from_sq(m)) > RANK_4;
330 inline Key Position::key() const {
334 inline Key Position::pawn_key() const {
338 inline Key Position::material_key() const {
339 return st->materialKey;
342 inline Score Position::psq_score() const {
346 inline Value Position::non_pawn_material(Color c) const {
347 return st->nonPawnMaterial[c];
350 inline int Position::game_ply() const {
354 inline int Position::rule50_count() const {
358 inline uint64_t Position::nodes_searched() const {
362 inline void Position::set_nodes_searched(uint64_t n) {
366 inline bool Position::opposite_bishops() const {
367 return pieceCount[WHITE][BISHOP] == 1
368 && pieceCount[BLACK][BISHOP] == 1
369 && opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK));
372 inline bool Position::is_chess960() const {
376 inline bool Position::capture_or_promotion(Move m) const {
379 return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
382 inline bool Position::capture(Move m) const {
384 // Castling is encoded as "king captures the rook"
386 return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
389 inline PieceType Position::captured_piece_type() const {
390 return st->capturedType;
393 inline Thread* Position::this_thread() const {
397 inline void Position::put_piece(Color c, PieceType pt, Square s) {
399 board[s] = make_piece(c, pt);
400 byTypeBB[ALL_PIECES] |= s;
403 index[s] = pieceCount[c][pt]++;
404 pieceList[c][pt][index[s]] = s;
405 pieceCount[c][ALL_PIECES]++;
408 inline void Position::remove_piece(Color c, PieceType pt, Square s) {
410 // WARNING: This is not a reversible operation. If we remove a piece in
411 // do_move() and then replace it in undo_move() we will put it at the end of
412 // the list and not in its original place, it means index[] and pieceList[]
413 // are not guaranteed to be invariant to a do_move() + undo_move() sequence.
414 byTypeBB[ALL_PIECES] ^= s;
417 /* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
418 Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
419 index[lastSquare] = index[s];
420 pieceList[c][pt][index[lastSquare]] = lastSquare;
421 pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
422 pieceCount[c][ALL_PIECES]--;
425 inline void Position::move_piece(Color c, PieceType pt, Square from, Square to) {
427 // index[from] is not updated and becomes stale. This works as long as index[]
428 // is accessed just by known occupied squares.
429 Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
430 byTypeBB[ALL_PIECES] ^= from_to_bb;
431 byTypeBB[pt] ^= from_to_bb;
432 byColorBB[c] ^= from_to_bb;
433 board[from] = NO_PIECE;
434 board[to] = make_piece(c, pt);
435 index[to] = index[from];
436 pieceList[c][pt][index[to]] = to;
439 #endif // #ifndef POSITION_H_INCLUDED