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];
45 /// CheckInfo struct keeps info used to detect if a move gives check
49 Bitboard blockersForKing[COLOR_NB];
50 Bitboard checkSquares[PIECE_TYPE_NB];
55 /// StateInfo struct stores information needed to restore a Position object to
56 /// its previous state when we retract a move. Whenever a move is made on the
57 /// board (by calling Position::do_move), a StateInfo object must be passed.
61 // Copied when making a move
64 Value nonPawnMaterial[COLOR_NB];
71 // Not copied when making a move
74 PieceType capturedType;
79 // In a std::deque references to elements are unaffected upon resizing
80 typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
83 /// Position class stores information regarding the board representation as
84 /// pieces, side to move, hash keys, castling info, etc. Important methods are
85 /// do_move() and undo_move(), used by the search to update node info when
86 /// traversing the search tree.
94 Position(const Position&) = delete;
95 Position& operator=(const Position&) = delete;
97 // FEN string input/output
98 Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
99 const std::string fen() const;
101 // Position representation
102 Bitboard pieces() const;
103 Bitboard pieces(PieceType pt) const;
104 Bitboard pieces(PieceType pt1, PieceType pt2) const;
105 Bitboard pieces(Color c) const;
106 Bitboard pieces(Color c, PieceType pt) const;
107 Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
108 Piece piece_on(Square s) const;
109 Square ep_square() const;
110 bool empty(Square s) const;
111 template<PieceType Pt> int count(Color c) const;
112 template<PieceType Pt> const Square* squares(Color c) const;
113 template<PieceType Pt> Square square(Color c) const;
116 int can_castle(Color c) const;
117 int can_castle(CastlingRight cr) const;
118 bool castling_impeded(CastlingRight cr) const;
119 Square castling_rook_square(CastlingRight cr) const;
122 Bitboard checkers() const;
123 Bitboard discovered_check_candidates() const;
124 Bitboard pinned_pieces(Color c) const;
125 const CheckInfo& check_info() const;
127 // Attacks to/from a given square
128 Bitboard attackers_to(Square s) const;
129 Bitboard attackers_to(Square s, Bitboard occupied) const;
130 Bitboard attacks_from(Piece pc, Square s) const;
131 template<PieceType> Bitboard attacks_from(Square s) const;
132 template<PieceType> Bitboard attacks_from(Square s, Color c) const;
133 Bitboard slider_blockers(Bitboard sliders, Square s) const;
135 // Properties of moves
136 bool legal(Move m) const;
137 bool pseudo_legal(const Move m) const;
138 bool capture(Move m) const;
139 bool capture_or_promotion(Move m) const;
140 bool gives_check(Move m) const;
141 bool advanced_pawn_push(Move m) const;
142 Piece moved_piece(Move m) const;
143 PieceType captured_piece_type() const;
146 bool pawn_passed(Color c, Square s) const;
147 bool opposite_bishops() const;
149 // Doing and undoing moves
150 void do_move(Move m, StateInfo& st, bool givesCheck);
151 void undo_move(Move m);
152 void do_null_move(StateInfo& st);
153 void undo_null_move();
155 // Static exchange evaluation
156 Value see(Move m) const;
157 Value see_sign(Move m) const;
159 // Accessing hash keys
161 Key key_after(Move m) const;
162 Key exclusion_key() const;
163 Key material_key() const;
164 Key pawn_key() const;
166 // Other properties of the position
167 Color side_to_move() const;
168 Phase game_phase() const;
169 int game_ply() const;
170 bool is_chess960() const;
171 Thread* this_thread() const;
172 uint64_t nodes_searched() const;
173 void set_nodes_searched(uint64_t n);
174 bool is_draw() const;
175 int rule50_count() const;
176 Score psq_score() const;
177 Value non_pawn_material(Color c) const;
179 // Position consistency check, for debugging
180 bool pos_is_ok(int* failedStep = nullptr) const;
184 // Initialization helpers (used while setting up a position)
185 void set_castling_right(Color c, Square rfrom);
186 void set_state(StateInfo* si) const;
187 void set_check_info(CheckInfo* ci) 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 st->ci.blockersForKing[~sideToMove] & pieces(sideToMove);
317 inline Bitboard Position::pinned_pieces(Color c) const {
318 return st->ci.blockersForKing[c] & pieces(c);
321 inline const CheckInfo& Position::check_info() const {
325 inline bool Position::pawn_passed(Color c, Square s) const {
326 return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
329 inline bool Position::advanced_pawn_push(Move m) const {
330 return type_of(moved_piece(m)) == PAWN
331 && relative_rank(sideToMove, from_sq(m)) > RANK_4;
334 inline Key Position::key() const {
338 inline Key Position::pawn_key() const {
342 inline Key Position::material_key() const {
343 return st->materialKey;
346 inline Score Position::psq_score() const {
350 inline Value Position::non_pawn_material(Color c) const {
351 return st->nonPawnMaterial[c];
354 inline int Position::game_ply() const {
358 inline int Position::rule50_count() const {
362 inline uint64_t Position::nodes_searched() const {
366 inline void Position::set_nodes_searched(uint64_t n) {
370 inline bool Position::opposite_bishops() const {
371 return pieceCount[WHITE][BISHOP] == 1
372 && pieceCount[BLACK][BISHOP] == 1
373 && opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK));
376 inline bool Position::is_chess960() const {
380 inline bool Position::capture_or_promotion(Move m) const {
383 return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
386 inline bool Position::capture(Move m) const {
388 // Castling is encoded as "king captures the rook"
390 return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
393 inline PieceType Position::captured_piece_type() const {
394 return st->capturedType;
397 inline Thread* Position::this_thread() const {
401 inline void Position::put_piece(Color c, PieceType pt, Square s) {
403 board[s] = make_piece(c, pt);
404 byTypeBB[ALL_PIECES] |= s;
407 index[s] = pieceCount[c][pt]++;
408 pieceList[c][pt][index[s]] = s;
409 pieceCount[c][ALL_PIECES]++;
412 inline void Position::remove_piece(Color c, PieceType pt, Square s) {
414 // WARNING: This is not a reversible operation. If we remove a piece in
415 // do_move() and then replace it in undo_move() we will put it at the end of
416 // the list and not in its original place, it means index[] and pieceList[]
417 // are not guaranteed to be invariant to a do_move() + undo_move() sequence.
418 byTypeBB[ALL_PIECES] ^= s;
421 /* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
422 Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
423 index[lastSquare] = index[s];
424 pieceList[c][pt][index[lastSquare]] = lastSquare;
425 pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
426 pieceCount[c][ALL_PIECES]--;
429 inline void Position::move_piece(Color c, PieceType pt, Square from, Square to) {
431 // index[from] is not updated and becomes stale. This works as long as index[]
432 // is accessed just by known occupied squares.
433 Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
434 byTypeBB[ALL_PIECES] ^= from_to_bb;
435 byTypeBB[pt] ^= from_to_bb;
436 byColorBB[c] ^= from_to_bb;
437 board[from] = NO_PIECE;
438 board[to] = make_piece(c, pt);
439 index[to] = index[from];
440 pieceList[c][pt][index[to]] = to;
443 #endif // #ifndef POSITION_H_INCLUDED