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-2013 Marco Costalba, Joona Kiiski, Tord Romstad
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.
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.
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/>.
20 #ifndef POSITION_H_INCLUDED
21 #define POSITION_H_INCLUDED
30 /// The checkInfo struct is initialized at c'tor time and keeps info used
31 /// to detect if a move gives check.
37 explicit CheckInfo(const Position&);
39 Bitboard dcCandidates;
41 Bitboard checkSq[PIECE_TYPE_NB];
46 /// The StateInfo struct stores information we need to restore a Position
47 /// object to its previous state when we retract a move. Whenever a move
48 /// is made on the board (by calling Position::do_move), a StateInfo object
49 /// must be passed as a parameter.
52 Key pawnKey, materialKey;
53 Value npMaterial[COLOR_NB];
54 int castleRights, rule50, pliesFromNull;
60 PieceType capturedType;
65 /// When making a move the current StateInfo up to 'key' excluded is copied to
66 /// the new one. Here we calculate the quad words (64bits) needed to be copied.
67 const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
70 /// The position data structure. A position consists of the following data:
72 /// * For each piece type, a bitboard representing the squares occupied
73 /// by pieces of that type.
74 /// * For each color, a bitboard representing the squares occupied by
75 /// pieces of that color.
76 /// * A bitboard of all occupied squares.
77 /// * A bitboard of all checking pieces.
78 /// * A 64-entry array of pieces, indexed by the squares of the board.
79 /// * The current side to move.
80 /// * Information about the castling rights for both sides.
81 /// * The initial files of the kings and both pairs of rooks. This is
82 /// used to implement the Chess960 castling rules.
83 /// * The en passant square (which is SQ_NONE if no en passant capture is
85 /// * The squares of the kings for both sides.
86 /// * Hash keys for the position itself, the current pawn structure, and
87 /// the current material situation.
88 /// * Hash keys for all previous positions in the game for detecting
90 /// * A counter for detecting 50 move rule draws.
95 Position(const Position& p, Thread* t) { *this = p; thisThread = t; }
96 Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
97 Position& operator=(const Position&);
101 void set(const std::string& fen, bool isChess960, Thread* th);
102 const std::string fen() const;
103 const std::string pretty(Move m = MOVE_NONE) const;
105 // Position representation
106 Bitboard pieces() const;
107 Bitboard pieces(PieceType pt) const;
108 Bitboard pieces(PieceType pt1, PieceType pt2) const;
109 Bitboard pieces(Color c) const;
110 Bitboard pieces(Color c, PieceType pt) const;
111 Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
112 Piece piece_on(Square s) const;
113 Square king_square(Color c) const;
114 Square ep_square() const;
115 bool is_empty(Square s) const;
116 template<PieceType Pt> int count(Color c) const;
117 template<PieceType Pt> const Square* list(Color c) const;
120 int can_castle(CastleRight f) const;
121 int can_castle(Color c) const;
122 bool castle_impeded(Color c, CastlingSide s) const;
123 Square castle_rook_square(Color c, CastlingSide s) const;
126 Bitboard checkers() const;
127 Bitboard discovered_check_candidates() const;
128 Bitboard pinned_pieces() const;
130 // Attacks to/from a given square
131 Bitboard attackers_to(Square s) const;
132 Bitboard attackers_to(Square s, Bitboard occ) const;
133 Bitboard attacks_from(Piece p, Square s) const;
134 static Bitboard attacks_from(Piece p, Square s, Bitboard occ);
135 template<PieceType> Bitboard attacks_from(Square s) const;
136 template<PieceType> Bitboard attacks_from(Square s, Color c) const;
138 // Properties of moves
139 bool move_gives_check(Move m, const CheckInfo& ci) const;
140 bool pl_move_is_legal(Move m, Bitboard pinned) const;
141 bool is_pseudo_legal(const Move m) const;
142 bool is_capture(Move m) const;
143 bool is_capture_or_promotion(Move m) const;
144 bool is_passed_pawn_push(Move m) const;
145 Piece piece_moved(Move m) const;
146 PieceType captured_piece_type() const;
149 bool pawn_is_passed(Color c, Square s) const;
150 bool pawn_on_7th(Color c) const;
151 bool opposite_bishops() const;
152 bool bishop_pair(Color c) const;
154 // Doing and undoing moves
155 void do_move(Move m, StateInfo& st);
156 void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
157 void undo_move(Move m);
158 void do_null_move(StateInfo& st);
159 void undo_null_move();
161 // Static exchange evaluation
162 int see(Move m, int asymmThreshold = 0) const;
163 int see_sign(Move m) const;
165 // Accessing hash keys
167 Key exclusion_key() const;
168 Key pawn_key() const;
169 Key material_key() const;
171 // Incremental piece-square evaluation
172 Score psq_score() const;
173 Value non_pawn_material(Color c) const;
175 // Other properties of the position
176 Color side_to_move() const;
177 int game_ply() const;
178 bool is_chess960() const;
179 Thread* this_thread() const;
180 int64_t nodes_searched() const;
181 void set_nodes_searched(int64_t n);
182 bool is_draw() const;
184 // Position consistency check, for debugging
185 bool pos_is_ok(int* failedStep = NULL) const;
189 // Initialization helpers (used while setting up a position)
191 void set_castle_right(Color c, Square rfrom);
194 void do_castle(Square kfrom, Square kto, Square rfrom, Square rto);
195 Bitboard hidden_checkers(Square ksq, Color c) const;
196 void put_piece(Square s, Color c, PieceType pt);
197 void remove_piece(Square s, Color c, PieceType pt);
198 void move_piece(Square from, Square to, Color c, PieceType pt);
200 // Computing hash keys from scratch (for initialization and debugging)
201 Key compute_key() const;
202 Key compute_pawn_key() const;
203 Key compute_material_key() const;
205 // Computing incremental evaluation scores and material counts
206 Score compute_psq_score() const;
207 Value compute_non_pawn_material(Color c) const;
210 Piece board[SQUARE_NB];
211 Bitboard byTypeBB[PIECE_TYPE_NB];
212 Bitboard byColorBB[COLOR_NB];
213 int pieceCount[COLOR_NB][PIECE_TYPE_NB];
214 Square pieceList[COLOR_NB][PIECE_TYPE_NB][16];
215 int index[SQUARE_NB];
218 int castleRightsMask[SQUARE_NB];
219 Square castleRookSquare[COLOR_NB][CASTLING_SIDE_NB];
220 Bitboard castlePath[COLOR_NB][CASTLING_SIDE_NB];
221 StateInfo startState;
230 inline int64_t Position::nodes_searched() const {
234 inline void Position::set_nodes_searched(int64_t n) {
238 inline Piece Position::piece_on(Square s) const {
242 inline Piece Position::piece_moved(Move m) const {
243 return board[from_sq(m)];
246 inline bool Position::is_empty(Square s) const {
247 return board[s] == NO_PIECE;
250 inline Color Position::side_to_move() const {
254 inline Bitboard Position::pieces() const {
255 return byTypeBB[ALL_PIECES];
258 inline Bitboard Position::pieces(PieceType pt) const {
262 inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
263 return byTypeBB[pt1] | byTypeBB[pt2];
266 inline Bitboard Position::pieces(Color c) const {
270 inline Bitboard Position::pieces(Color c, PieceType pt) const {
271 return byColorBB[c] & byTypeBB[pt];
274 inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
275 return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
278 template<PieceType Pt> inline int Position::count(Color c) const {
279 return pieceCount[c][Pt];
282 template<PieceType Pt> inline const Square* Position::list(Color c) const {
283 return pieceList[c][Pt];
286 inline Square Position::ep_square() const {
290 inline Square Position::king_square(Color c) const {
291 return pieceList[c][KING][0];
294 inline int Position::can_castle(CastleRight f) const {
295 return st->castleRights & f;
298 inline int Position::can_castle(Color c) const {
299 return st->castleRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
302 inline bool Position::castle_impeded(Color c, CastlingSide s) const {
303 return byTypeBB[ALL_PIECES] & castlePath[c][s];
306 inline Square Position::castle_rook_square(Color c, CastlingSide s) const {
307 return castleRookSquare[c][s];
310 template<PieceType Pt>
311 inline Bitboard Position::attacks_from(Square s) const {
313 return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, pieces())
314 : Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
315 : StepAttacksBB[Pt][s];
319 inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
320 return StepAttacksBB[make_piece(c, PAWN)][s];
323 inline Bitboard Position::attacks_from(Piece p, Square s) const {
324 return attacks_from(p, s, byTypeBB[ALL_PIECES]);
327 inline Bitboard Position::attackers_to(Square s) const {
328 return attackers_to(s, byTypeBB[ALL_PIECES]);
331 inline Bitboard Position::checkers() const {
332 return st->checkersBB;
335 inline Bitboard Position::discovered_check_candidates() const {
336 return hidden_checkers(king_square(~sideToMove), sideToMove);
339 inline Bitboard Position::pinned_pieces() const {
340 return hidden_checkers(king_square(sideToMove), ~sideToMove);
343 inline bool Position::pawn_is_passed(Color c, Square s) const {
344 return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
347 inline Key Position::key() const {
351 inline Key Position::pawn_key() const {
355 inline Key Position::material_key() const {
356 return st->materialKey;
359 inline Score Position::psq_score() const {
363 inline Value Position::non_pawn_material(Color c) const {
364 return st->npMaterial[c];
367 inline bool Position::is_passed_pawn_push(Move m) const {
369 return type_of(piece_moved(m)) == PAWN
370 && pawn_is_passed(sideToMove, to_sq(m));
373 inline int Position::game_ply() const {
377 inline bool Position::opposite_bishops() const {
379 return pieceCount[WHITE][BISHOP] == 1
380 && pieceCount[BLACK][BISHOP] == 1
381 && opposite_colors(pieceList[WHITE][BISHOP][0], pieceList[BLACK][BISHOP][0]);
384 inline bool Position::bishop_pair(Color c) const {
386 return pieceCount[c][BISHOP] >= 2
387 && opposite_colors(pieceList[c][BISHOP][0], pieceList[c][BISHOP][1]);
390 inline bool Position::pawn_on_7th(Color c) const {
391 return pieces(c, PAWN) & rank_bb(relative_rank(c, RANK_7));
394 inline bool Position::is_chess960() const {
398 inline bool Position::is_capture_or_promotion(Move m) const {
401 return type_of(m) ? type_of(m) != CASTLE : !is_empty(to_sq(m));
404 inline bool Position::is_capture(Move m) const {
406 // Note that castle is coded as "king captures the rook"
408 return (!is_empty(to_sq(m)) && type_of(m) != CASTLE) || type_of(m) == ENPASSANT;
411 inline PieceType Position::captured_piece_type() const {
412 return st->capturedType;
415 inline Thread* Position::this_thread() const {
419 inline void Position::put_piece(Square s, Color c, PieceType pt) {
421 board[s] = make_piece(c, pt);
422 byTypeBB[ALL_PIECES] |= s;
425 index[s] = pieceCount[c][pt]++;
426 pieceList[c][pt][index[s]] = s;
429 inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
431 // index[from] is not updated and becomes stale. This works as long
432 // as index[] 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 inline void Position::remove_piece(Square s, Color c, PieceType pt) {
445 // WARNING: This is not a reversible operation. If we remove a piece in
446 // do_move() and then replace it in undo_move() we will put it at the end of
447 // the list and not in its original place, it means index[] and pieceList[]
448 // are not guaranteed to be invariant to a do_move() + undo_move() sequence.
449 byTypeBB[ALL_PIECES] ^= s;
452 /* board[s] = NO_PIECE; */ // Not needed, will be overwritten by capturing
453 Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
454 index[lastSquare] = index[s];
455 pieceList[c][pt][index[lastSquare]] = lastSquare;
456 pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
459 #endif // #ifndef POSITION_H_INCLUDED