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()
36 extern Score psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
41 /// CheckInfo struct is initialized at constructor time and keeps info used to
42 /// detect if a move gives check.
46 explicit CheckInfo(const Position&);
48 Bitboard dcCandidates;
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 /// Position class stores information regarding the board representation as
80 /// pieces, side to move, hash keys, castling info, etc. Important methods are
81 /// do_move() and undo_move(), used by the search to update node info when
82 /// traversing the search tree.
89 Position() = default; // To define the global object RootPos
90 Position(const Position&) = delete;
91 Position(const Position& pos, Thread* th) { *this = pos; thisThread = th; }
92 Position(const std::string& f, bool c960, Thread* th) { set(f, c960, th); }
93 Position& operator=(const Position&); // To assign RootPos from UCI
95 // FEN string input/output
96 void set(const std::string& fenStr, bool isChess960, Thread* th);
97 const std::string fen() const;
99 // Position representation
100 Bitboard pieces() const;
101 Bitboard pieces(PieceType pt) const;
102 Bitboard pieces(PieceType pt1, PieceType pt2) const;
103 Bitboard pieces(Color c) const;
104 Bitboard pieces(Color c, PieceType pt) const;
105 Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
106 Piece piece_on(Square s) const;
107 Square ep_square() const;
108 bool empty(Square s) const;
109 template<PieceType Pt> int count(Color c) const;
110 template<PieceType Pt> const Square* squares(Color c) const;
111 template<PieceType Pt> Square square(Color c) const;
114 int can_castle(Color c) const;
115 int can_castle(CastlingRight cr) const;
116 bool castling_impeded(CastlingRight cr) const;
117 Square castling_rook_square(CastlingRight cr) const;
120 Bitboard checkers() const;
121 Bitboard discovered_check_candidates() const;
122 Bitboard pinned_pieces(Color c) const;
124 // Attacks to/from a given square
125 Bitboard attackers_to(Square s) const;
126 Bitboard attackers_to(Square s, Bitboard occupied) const;
127 Bitboard attacks_from(Piece pc, Square s) const;
128 template<PieceType> Bitboard attacks_from(Square s) const;
129 template<PieceType> Bitboard attacks_from(Square s, Color c) const;
131 // Properties of moves
132 bool legal(Move m, Bitboard pinned) const;
133 bool pseudo_legal(const Move m) const;
134 bool capture(Move m) const;
135 bool capture_or_promotion(Move m) const;
136 bool gives_check(Move m, const CheckInfo& ci) const;
137 bool advanced_pawn_push(Move m) const;
138 Piece moved_piece(Move m) const;
139 PieceType captured_piece_type() const;
142 bool pawn_passed(Color c, Square s) const;
143 bool opposite_bishops() const;
145 // Doing and undoing moves
146 void do_move(Move m, StateInfo& st, bool givesCheck);
147 void undo_move(Move m);
148 void do_null_move(StateInfo& st);
149 void undo_null_move();
151 // Static exchange evaluation
152 Value see(Move m) const;
153 Value see_sign(Move m) const;
155 // Accessing hash keys
157 Key key_after(Move m) const;
158 Key exclusion_key() const;
159 Key material_key() const;
160 Key pawn_key() const;
162 // Other properties of the position
163 Color side_to_move() const;
164 Phase game_phase() const;
165 int game_ply() const;
166 bool is_chess960() const;
167 Thread* this_thread() const;
168 uint64_t nodes_searched() const;
169 void set_nodes_searched(uint64_t n);
170 bool is_draw() const;
171 int rule50_count() const;
172 Score psq_score() const;
173 Value non_pawn_material(Color c) const;
175 // Position consistency check, for debugging
176 bool pos_is_ok(int* failedStep = nullptr) const;
180 // Initialization helpers (used while setting up a position)
182 void set_castling_right(Color c, Square rfrom);
183 void set_state(StateInfo* si) const;
186 Bitboard check_blockers(Color c, Color kingColor) const;
187 void put_piece(Color c, PieceType pt, Square s);
188 void remove_piece(Color c, PieceType pt, Square s);
189 void move_piece(Color c, PieceType pt, Square from, Square to);
191 void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
194 Piece board[SQUARE_NB];
195 Bitboard byTypeBB[PIECE_TYPE_NB];
196 Bitboard byColorBB[COLOR_NB];
197 int pieceCount[COLOR_NB][PIECE_TYPE_NB];
198 Square pieceList[COLOR_NB][PIECE_TYPE_NB][16];
199 int index[SQUARE_NB];
200 int castlingRightsMask[SQUARE_NB];
201 Square castlingRookSquare[CASTLING_RIGHT_NB];
202 Bitboard castlingPath[CASTLING_RIGHT_NB];
203 StateInfo startState;
212 extern std::ostream& operator<<(std::ostream& os, const Position& pos);
214 inline Color Position::side_to_move() const {
218 inline bool Position::empty(Square s) const {
219 return board[s] == NO_PIECE;
222 inline Piece Position::piece_on(Square s) const {
226 inline Piece Position::moved_piece(Move m) const {
227 return board[from_sq(m)];
230 inline Bitboard Position::pieces() const {
231 return byTypeBB[ALL_PIECES];
234 inline Bitboard Position::pieces(PieceType pt) const {
238 inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
239 return byTypeBB[pt1] | byTypeBB[pt2];
242 inline Bitboard Position::pieces(Color c) const {
246 inline Bitboard Position::pieces(Color c, PieceType pt) const {
247 return byColorBB[c] & byTypeBB[pt];
250 inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
251 return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
254 template<PieceType Pt> inline int Position::count(Color c) const {
255 return pieceCount[c][Pt];
258 template<PieceType Pt> inline const Square* Position::squares(Color c) const {
259 return pieceList[c][Pt];
262 template<PieceType Pt> inline Square Position::square(Color c) const {
263 assert(pieceCount[c][Pt] == 1);
264 return pieceList[c][Pt][0];
267 inline Square Position::ep_square() const {
271 inline int Position::can_castle(CastlingRight cr) const {
272 return st->castlingRights & cr;
275 inline int Position::can_castle(Color c) const {
276 return st->castlingRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
279 inline bool Position::castling_impeded(CastlingRight cr) const {
280 return byTypeBB[ALL_PIECES] & castlingPath[cr];
283 inline Square Position::castling_rook_square(CastlingRight cr) const {
284 return castlingRookSquare[cr];
287 template<PieceType Pt>
288 inline Bitboard Position::attacks_from(Square s) const {
289 return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
290 : Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
291 : StepAttacksBB[Pt][s];
295 inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
296 return StepAttacksBB[make_piece(c, PAWN)][s];
299 inline Bitboard Position::attacks_from(Piece pc, Square s) const {
300 return attacks_bb(pc, s, byTypeBB[ALL_PIECES]);
303 inline Bitboard Position::attackers_to(Square s) const {
304 return attackers_to(s, byTypeBB[ALL_PIECES]);
307 inline Bitboard Position::checkers() const {
308 return st->checkersBB;
311 inline Bitboard Position::discovered_check_candidates() const {
312 return check_blockers(sideToMove, ~sideToMove);
315 inline Bitboard Position::pinned_pieces(Color c) const {
316 return check_blockers(c, c);
319 inline bool Position::pawn_passed(Color c, Square s) const {
320 return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
323 inline bool Position::advanced_pawn_push(Move m) const {
324 return type_of(moved_piece(m)) == PAWN
325 && relative_rank(sideToMove, from_sq(m)) > RANK_4;
328 inline Key Position::key() const {
332 inline Key Position::pawn_key() const {
336 inline Key Position::material_key() const {
337 return st->materialKey;
340 inline Score Position::psq_score() const {
344 inline Value Position::non_pawn_material(Color c) const {
345 return st->nonPawnMaterial[c];
348 inline int Position::game_ply() const {
352 inline int Position::rule50_count() const {
356 inline uint64_t Position::nodes_searched() const {
360 inline void Position::set_nodes_searched(uint64_t n) {
364 inline bool Position::opposite_bishops() const {
365 return pieceCount[WHITE][BISHOP] == 1
366 && pieceCount[BLACK][BISHOP] == 1
367 && opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK));
370 inline bool Position::is_chess960() const {
374 inline bool Position::capture_or_promotion(Move m) const {
377 return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
380 inline bool Position::capture(Move m) const {
382 // Castling is encoded as "king captures the rook"
384 return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
387 inline PieceType Position::captured_piece_type() const {
388 return st->capturedType;
391 inline Thread* Position::this_thread() const {
395 inline void Position::put_piece(Color c, PieceType pt, Square s) {
397 board[s] = make_piece(c, pt);
398 byTypeBB[ALL_PIECES] |= s;
401 index[s] = pieceCount[c][pt]++;
402 pieceList[c][pt][index[s]] = s;
403 pieceCount[c][ALL_PIECES]++;
406 inline void Position::remove_piece(Color c, PieceType pt, Square s) {
408 // WARNING: This is not a reversible operation. If we remove a piece in
409 // do_move() and then replace it in undo_move() we will put it at the end of
410 // the list and not in its original place, it means index[] and pieceList[]
411 // are not guaranteed to be invariant to a do_move() + undo_move() sequence.
412 byTypeBB[ALL_PIECES] ^= s;
415 /* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
416 Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
417 index[lastSquare] = index[s];
418 pieceList[c][pt][index[lastSquare]] = lastSquare;
419 pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
420 pieceCount[c][ALL_PIECES]--;
423 inline void Position::move_piece(Color c, PieceType pt, Square from, Square to) {
425 // index[from] is not updated and becomes stale. This works as long as index[]
426 // is accessed just by known occupied squares.
427 Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
428 byTypeBB[ALL_PIECES] ^= from_to_bb;
429 byTypeBB[pt] ^= from_to_bb;
430 byColorBB[c] ^= from_to_bb;
431 board[from] = NO_PIECE;
432 board[to] = make_piece(c, pt);
433 index[to] = index[from];
434 pieceList[c][pt][index[to]] = to;
437 #endif // #ifndef POSITION_H_INCLUDED