2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #if !defined(POSITION_H_INCLUDED)
21 #define POSITION_H_INCLUDED
23 // Disable a silly and noisy warning from MSVC compiler
26 // Forcing value to bool 'true' or 'false' (performance warning)
27 #pragma warning(disable: 4800)
\r
37 #include "direction.h"
49 /// FEN string for the initial position:
50 const std::string StartPosition =
51 "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
53 /// Maximum number of plies per game (220 should be enough, because the
54 /// maximum search depth is 100, and during position setup we reset the
55 /// move counter for every non-reversible move):
56 const int MaxGameLength = 220;
63 /// Castle rights, encoded as bit fields:
75 /// The UndoInfo struct stores information we need to restore a Position
76 /// object to its previous state when we retract a move. Whenever a move
77 /// is made on the board (by calling Position::do_move), an UndoInfo object
78 /// must be passed as a parameter. When the move is unmade (by calling
79 /// Position::undo_move), the same UndoInfo object must be passed again.
85 Key key, pawnKey, materialKey;
89 Value mgValue, egValue;
93 /// The position data structure. A position consists of the following data:
95 /// * For each piece type, a bitboard representing the squares occupied
96 /// by pieces of that type.
97 /// * For each color, a bitboard representing the squares occupiecd by
98 /// pieces of that color.
99 /// * A bitboard of all occupied squares.
100 /// * A bitboard of all checking pieces.
101 /// * A 64-entry array of pieces, indexed by the squares of the board.
102 /// * The current side to move.
103 /// * Information about the castling rights for both sides.
104 /// * The initial files of the kings and both pairs of rooks. This is
105 /// used to implement the Chess960 castling rules.
106 /// * The en passant square (which is SQ_NONE if no en passant capture is
108 /// * The squares of the kings for both sides.
109 /// * The last move played.
110 /// * Hash keys for the position itself, the current pawn structure, and
111 /// the current material situation.
112 /// * Hash keys for all previous positions in the game (for detecting
113 /// repetition draws.
114 /// * A counter for detecting 50 move rule draws.
118 friend class MaterialInfo;
123 Position(const Position &pos);
124 Position(const std::string &fen);
127 void from_fen(const std::string &fen);
128 const std::string to_fen() const;
132 void copy(const Position &pos);
133 void flipped_copy(const Position &pos);
135 // The piece on a given square
136 Piece piece_on(Square s) const;
137 PieceType type_of_piece_on(Square s) const;
138 Color color_of_piece_on(Square s) const;
139 bool square_is_empty(Square s) const;
140 bool square_is_occupied(Square s) const;
141 Value midgame_value_of_piece_on(Square s) const;
142 Value endgame_value_of_piece_on(Square s) const;
145 Color side_to_move() const;
147 // Bitboard representation of the position
148 Bitboard empty_squares() const;
149 Bitboard occupied_squares() const;
150 Bitboard pieces_of_color(Color c) const;
151 Bitboard pieces_of_type(PieceType pt) const;
152 Bitboard pieces_of_color_and_type(Color c, PieceType pt) const;
153 Bitboard pawns() const;
154 Bitboard knights() const;
155 Bitboard bishops() const;
156 Bitboard rooks() const;
157 Bitboard queens() const;
158 Bitboard kings() const;
159 Bitboard rooks_and_queens() const;
160 Bitboard bishops_and_queens() const;
161 Bitboard sliders() const;
162 Bitboard pawns(Color c) const;
163 Bitboard knights(Color c) const;
164 Bitboard bishops(Color c) const;
165 Bitboard rooks(Color c) const;
166 Bitboard queens(Color c) const;
167 Bitboard kings(Color c) const;
168 Bitboard rooks_and_queens(Color c) const;
169 Bitboard bishops_and_queens(Color c) const;
170 Bitboard sliders_of_color(Color c) const;
172 // Number of pieces of each color and type
173 int piece_count(Color c, PieceType pt) const;
174 int pawn_count(Color c) const;
175 int knight_count(Color c) const;
176 int bishop_count(Color c) const;
177 int rook_count(Color c) const;
178 int queen_count(Color c) const;
180 // The en passant square:
181 Square ep_square() const;
183 // Current king position for each color
184 Square king_square(Color c) const;
187 bool can_castle_kingside(Color c) const;
188 bool can_castle_queenside(Color c) const;
189 bool can_castle(Color c) const;
190 Square initial_kr_square(Color c) const;
191 Square initial_qr_square(Color c) const;
194 Bitboard sliding_attacks(Square s, Direction d) const;
195 Bitboard ray_attacks(Square s, SignedDirection d) const;
196 Bitboard pawn_attacks(Color c, Square s) const;
197 Bitboard white_pawn_attacks(Square s) const;
198 Bitboard black_pawn_attacks(Square s) const;
201 Bitboard piece_attacks(Square s) const;
203 // Bitboards for pinned pieces and discovered check candidates
204 Bitboard discovered_check_candidates(Color c) const;
205 Bitboard pinned_pieces(Color c) const;
208 Bitboard checkers() const;
211 Square piece_list(Color c, PieceType pt, int index) const;
212 Square pawn_list(Color c, int index) const;
213 Square knight_list(Color c, int index) const;
214 Square bishop_list(Color c, int index) const;
215 Square rook_list(Color c, int index) const;
216 Square queen_list(Color c, int index) const;
218 // Attack information for a given square
219 bool square_is_attacked(Square s, Color c) const;
220 Bitboard attacks_to(Square s) const;
221 Bitboard attacks_to(Square s, Color c) const;
222 bool is_check() const;
223 bool piece_attacks_square(Square f, Square t) const;
224 bool white_pawn_attacks_square(Square f, Square t) const;
225 bool black_pawn_attacks_square(Square f, Square t) const;
226 bool knight_attacks_square(Square f, Square t) const;
227 bool bishop_attacks_square(Square f, Square t) const;
228 bool rook_attacks_square(Square f, Square t) const;
229 bool queen_attacks_square(Square f, Square t) const;
230 bool king_attacks_square(Square f, Square t) const;
232 // Properties of moves
233 bool move_is_legal(Move m) const;
234 bool move_is_legal(Move m, Bitboard pinned) const;
235 bool move_is_check(Move m) const;
236 bool move_is_check(Move m, Bitboard dcCandidates) const;
237 bool move_is_capture(Move m) const;
238 bool move_is_deep_pawn_push(Move m) const;
239 bool move_is_pawn_push_to_7th(Move m) const;
240 bool move_is_passed_pawn_push(Move m) const;
241 bool move_was_passed_pawn_push(Move m) const;
242 bool move_attacks_square(Move m, Square s) const;
244 // Information about pawns
245 bool pawn_is_passed(Color c, Square s) const;
246 bool pawn_is_isolated(Color c, Square s) const;
247 bool pawn_is_doubled(Color c, Square s) const;
249 // Open and half-open files
250 bool file_is_open(File f) const;
251 bool file_is_half_open(Color c, File f) const;
254 bool square_is_weak(Square s, Color c) const;
256 // Doing and undoing moves
257 void backup(UndoInfo &u) const;
258 void restore(const UndoInfo &u);
259 void do_move(Move m, UndoInfo &u);
260 void do_move(Move m, UndoInfo &u, Bitboard dcCandidates);
261 void undo_move(Move m, const UndoInfo &u);
262 void do_null_move(UndoInfo &u);
263 void undo_null_move(const UndoInfo &u);
265 // Static exchange evaluation
266 int see(Square from, Square to) const;
267 int see(Move m) const;
269 // Accessing hash keys
271 Key get_pawn_key() const;
272 Key get_material_key() const;
274 // Incremental evaluation
275 Value mg_value() const;
276 Value eg_value() const;
277 Value non_pawn_material(Color c) const;
278 Phase game_phase() const;
279 Value mg_pst_delta(Move m) const;
281 // Game termination checks
283 bool is_draw() const;
285 // Check if one side threatens a mate in one
286 bool has_mate_threat(Color c);
288 // Number of plies since the last non-reversible move
289 int rule_50_counter() const;
291 // Other properties of the position
292 bool opposite_colored_bishops() const;
293 bool has_pawn_on_7th(Color c) const;
295 // Reset the gamePly variable to 0
296 void reset_game_ply();
298 // Position consistency check, for debugging
299 bool is_ok(int* failedStep = NULL) const;
301 // Static member functions:
302 static void init_zobrist();
303 static void init_piece_square_tables();
306 // Initialization helper functions (used while setting up a position)
308 void put_piece(Piece p, Square s);
309 void allow_oo(Color c);
310 void allow_ooo(Color c);
312 // Helper functions for doing and undoing moves
313 void do_castle_move(Move m);
314 void do_promotion_move(Move m, UndoInfo &u);
315 void do_ep_move(Move m);
316 void undo_castle_move(Move m);
317 void undo_promotion_move(Move m, const UndoInfo &u);
318 void undo_ep_move(Move m);
319 void find_checkers();
321 // Computing hash keys from scratch (for initialization and debugging)
322 Key compute_key() const;
323 Key compute_pawn_key() const;
324 Key compute_material_key() const;
326 // Computing incremental evaluation scores and material counts
327 Value mg_pst(Color c, PieceType pt, Square s) const;
328 Value eg_pst(Color c, PieceType pt, Square s) const;
329 Value compute_mg_value() const;
330 Value compute_eg_value() const;
331 Value compute_non_pawn_material(Color c) const;
334 Bitboard byColorBB[2], byTypeBB[8];
341 int pieceCount[2][8]; // [color][pieceType]
344 Square pieceList[2][8][16]; // [color][pieceType][index]
350 File initialKFile, initialKRFile, initialQRFile;
352 Square kingSquare[2];
354 Key key, pawnKey, materialKey, history[MaxGameLength];
356 Value mgValue, egValue;
360 static int castleRightsMask[64];
361 static Key zobrist[2][8][64];
362 static Key zobEp[64];
363 static Key zobCastle[16];
364 static Key zobMaterial[2][8][16];
365 static Key zobSideToMove;
366 static Value MgPieceSquareTable[16][64];
367 static Value EgPieceSquareTable[16][64];
372 //// Inline functions
375 inline Piece Position::piece_on(Square s) const {
379 inline Color Position::color_of_piece_on(Square s) const {
380 return color_of_piece(piece_on(s));
383 inline PieceType Position::type_of_piece_on(Square s) const {
384 return type_of_piece(piece_on(s));
387 inline bool Position::square_is_empty(Square s) const {
388 return piece_on(s) == EMPTY;
391 inline bool Position::square_is_occupied(Square s) const {
392 return !square_is_empty(s);
395 inline Value Position::midgame_value_of_piece_on(Square s) const {
396 return piece_value_midgame(piece_on(s));
399 inline Value Position::endgame_value_of_piece_on(Square s) const {
400 return piece_value_endgame(piece_on(s));
403 inline Color Position::side_to_move() const {
407 inline Bitboard Position::occupied_squares() const {
411 inline Bitboard Position::empty_squares() const {
412 return ~(occupied_squares());
415 inline Bitboard Position::pieces_of_color(Color c) const {
419 inline Bitboard Position::pieces_of_type(PieceType pt) const {
423 inline Bitboard Position::pieces_of_color_and_type(Color c, PieceType pt) const {
424 return pieces_of_color(c) & pieces_of_type(pt);
427 inline Bitboard Position::pawns() const {
428 return pieces_of_type(PAWN);
431 inline Bitboard Position::knights() const {
432 return pieces_of_type(KNIGHT);
435 inline Bitboard Position::bishops() const {
436 return pieces_of_type(BISHOP);
439 inline Bitboard Position::rooks() const {
440 return pieces_of_type(ROOK);
443 inline Bitboard Position::queens() const {
444 return pieces_of_type(QUEEN);
447 inline Bitboard Position::kings() const {
448 return pieces_of_type(KING);
451 inline Bitboard Position::rooks_and_queens() const {
452 return rooks() | queens();
455 inline Bitboard Position::bishops_and_queens() const {
456 return bishops() | queens();
459 inline Bitboard Position::sliders() const {
460 return bishops() | queens() | rooks();
463 inline Bitboard Position::pawns(Color c) const {
464 return pieces_of_color_and_type(c, PAWN);
467 inline Bitboard Position::knights(Color c) const {
468 return pieces_of_color_and_type(c, KNIGHT);
471 inline Bitboard Position::bishops(Color c) const {
472 return pieces_of_color_and_type(c, BISHOP);
475 inline Bitboard Position::rooks(Color c) const {
476 return pieces_of_color_and_type(c, ROOK);
479 inline Bitboard Position::queens(Color c) const {
480 return pieces_of_color_and_type(c, QUEEN);
483 inline Bitboard Position::kings(Color c) const {
484 return pieces_of_color_and_type(c, KING);
487 inline Bitboard Position::rooks_and_queens(Color c) const {
488 return rooks_and_queens() & pieces_of_color(c);
491 inline Bitboard Position::bishops_and_queens(Color c) const {
492 return bishops_and_queens() & pieces_of_color(c);
495 inline Bitboard Position::sliders_of_color(Color c) const {
496 return sliders() & pieces_of_color(c);
499 inline int Position::piece_count(Color c, PieceType pt) const {
500 return pieceCount[c][pt];
503 inline int Position::pawn_count(Color c) const {
504 return piece_count(c, PAWN);
507 inline int Position::knight_count(Color c) const {
508 return piece_count(c, KNIGHT);
511 inline int Position::bishop_count(Color c) const {
512 return piece_count(c, BISHOP);
515 inline int Position::rook_count(Color c) const {
516 return piece_count(c, ROOK);
519 inline int Position::queen_count(Color c) const {
520 return piece_count(c, QUEEN);
523 inline Square Position::piece_list(Color c, PieceType pt, int index) const {
524 return pieceList[c][pt][index];
527 inline Square Position::pawn_list(Color c, int index) const {
528 return piece_list(c, PAWN, index);
531 inline Square Position::knight_list(Color c, int index) const {
532 return piece_list(c, KNIGHT, index);
535 inline Square Position::bishop_list(Color c, int index) const {
536 return piece_list(c, BISHOP, index);
539 inline Square Position::rook_list(Color c, int index) const {
540 return piece_list(c, ROOK, index);
543 inline Square Position::queen_list(Color c, int index) const {
544 return piece_list(c, QUEEN, index);
547 inline Square Position::ep_square() const {
551 inline Square Position::king_square(Color c) const {
552 return kingSquare[c];
555 inline bool Position::can_castle_kingside(Color side) const {
556 return castleRights & (1+int(side));
559 inline bool Position::can_castle_queenside(Color side) const {
560 return castleRights & (4+4*int(side));
563 inline bool Position::can_castle(Color side) const {
564 return can_castle_kingside(side) || can_castle_queenside(side);
567 inline Square Position::initial_kr_square(Color c) const {
568 return relative_square(c, make_square(initialKRFile, RANK_1));
571 inline Square Position::initial_qr_square(Color c) const {
572 return relative_square(c, make_square(initialQRFile, RANK_1));
575 inline Bitboard Position::pawn_attacks(Color c, Square s) const {
576 return StepAttackBB[pawn_of_color(c)][s];
579 inline Bitboard Position::white_pawn_attacks(Square s) const {
580 return pawn_attacks(WHITE, s);
583 inline Bitboard Position::black_pawn_attacks(Square s) const {
584 return pawn_attacks(BLACK, s);
588 inline Bitboard Position::piece_attacks<KNIGHT>(Square s) const {
589 return StepAttackBB[KNIGHT][s];
593 inline Bitboard Position::piece_attacks<BISHOP>(Square s) const {
594 return bishop_attacks_bb(s, occupied_squares());
598 inline Bitboard Position::piece_attacks<ROOK>(Square s) const {
599 return rook_attacks_bb(s, occupied_squares());
603 inline Bitboard Position::piece_attacks<QUEEN>(Square s) const {
604 return piece_attacks<ROOK>(s) | piece_attacks<BISHOP>(s);
608 inline Bitboard Position::piece_attacks<KING>(Square s) const {
609 return StepAttackBB[KING][s];
612 inline Bitboard Position::checkers() const {
616 inline bool Position::is_check() const {
617 return checkers() != EmptyBoardBB;
620 inline bool Position::white_pawn_attacks_square(Square f, Square t) const {
621 return bit_is_set(white_pawn_attacks(f), t);
624 inline bool Position::black_pawn_attacks_square(Square f, Square t) const {
625 return bit_is_set(black_pawn_attacks(f), t);
628 inline bool Position::knight_attacks_square(Square f, Square t) const {
629 return bit_is_set(piece_attacks<KNIGHT>(f), t);
632 inline bool Position::bishop_attacks_square(Square f, Square t) const {
633 return bit_is_set(piece_attacks<BISHOP>(f), t);
636 inline bool Position::rook_attacks_square(Square f, Square t) const {
637 return bit_is_set(piece_attacks<ROOK>(f), t);
640 inline bool Position::queen_attacks_square(Square f, Square t) const {
641 return bit_is_set(piece_attacks<QUEEN>(f), t);
644 inline bool Position::king_attacks_square(Square f, Square t) const {
645 return bit_is_set(piece_attacks<KING>(f), t);
648 inline bool Position::pawn_is_passed(Color c, Square s) const {
649 return !(pawns(opposite_color(c)) & passed_pawn_mask(c, s));
652 inline bool Position::pawn_is_isolated(Color c, Square s) const {
653 return !(pawns(c) & neighboring_files_bb(s));
656 inline bool Position::pawn_is_doubled(Color c, Square s) const {
657 return pawns(c) & squares_behind(c, s);
660 inline bool Position::file_is_open(File f) const {
661 return !(pawns() & file_bb(f));
664 inline bool Position::file_is_half_open(Color c, File f) const {
665 return !(pawns(c) & file_bb(f));
668 inline bool Position::square_is_weak(Square s, Color c) const {
669 return !(pawns(c) & outpost_mask(opposite_color(c), s));
672 inline Key Position::get_key() const {
676 inline Key Position::get_pawn_key() const {
680 inline Key Position::get_material_key() const {
684 inline Value Position::mg_pst(Color c, PieceType pt, Square s) const {
685 return MgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
688 inline Value Position::mg_pst_delta(Move m) const {
689 return MgPieceSquareTable[piece_on(move_from(m))][move_to(m)]
690 -MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
693 inline Value Position::eg_pst(Color c, PieceType pt, Square s) const {
694 return EgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
697 inline Value Position::mg_value() const {
701 inline Value Position::eg_value() const {
705 inline Value Position::non_pawn_material(Color c) const {
706 return npMaterial[c];
709 inline Phase Position::game_phase() const {
711 // The purpose of the Value(325) terms below is to make sure the difference
712 // between MidgameLimit and EndgameLimit is a power of 2, which should make
713 // the division at the end of the function a bit faster.
714 static const Value MidgameLimit = 2 * QueenValueMidgame
715 + 2 * RookValueMidgame
716 + 6 * BishopValueMidgame
719 static const Value EndgameLimit = 4 * RookValueMidgame - Value(325);
721 Value npm = non_pawn_material(WHITE) + non_pawn_material(BLACK);
723 if (npm >= MidgameLimit)
724 return PHASE_MIDGAME;
725 else if(npm <= EndgameLimit)
726 return PHASE_ENDGAME;
728 return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
731 inline bool Position::move_is_deep_pawn_push(Move m) const {
733 Color c = side_to_move();
734 return piece_on(move_from(m)) == pawn_of_color(c)
735 && relative_rank(c, move_to(m)) > RANK_4;
738 inline bool Position::move_is_pawn_push_to_7th(Move m) const {
740 Color c = side_to_move();
741 return piece_on(move_from(m)) == pawn_of_color(c)
742 && relative_rank(c, move_to(m)) == RANK_7;
745 inline bool Position::move_is_passed_pawn_push(Move m) const {
747 Color c = side_to_move();
748 return piece_on(move_from(m)) == pawn_of_color(c)
749 && pawn_is_passed(c, move_to(m));
752 inline bool Position::move_was_passed_pawn_push(Move m) const {
754 Color c = opposite_color(side_to_move());
755 return piece_on(move_to(m)) == pawn_of_color(c)
756 && pawn_is_passed(c, move_to(m));
759 inline int Position::rule_50_counter() const {
764 inline bool Position::opposite_colored_bishops() const {
766 return bishop_count(WHITE) == 1
767 && bishop_count(BLACK) == 1
768 && square_color(bishop_list(WHITE, 0)) != square_color(bishop_list(BLACK, 0));
771 inline bool Position::has_pawn_on_7th(Color c) const {
773 return pawns(c) & relative_rank_bb(c, RANK_7);
777 #endif // !defined(POSITION_H_INCLUDED)