-
- // No defaul, copy c'tor or assignment allowed, default c'tor will not be
- // generated anyhow because of user-defined c'tors.
- Position(const Position&);
- Position& operator=(const Position&);
-
-public:
- Position(const Position& pos, int threadID);
- Position(const std::string& fen, bool isChess960, int threadID);
-
- // Text input/output
- void from_fen(const std::string& fen, bool isChess960);
- const std::string to_fen() const;
- void print(Move m = MOVE_NONE) const;
-
- // The piece on a given square
- Piece piece_on(Square s) const;
- bool square_is_empty(Square s) const;
-
- // Side to move
- Color side_to_move() const;
-
- // Bitboard representation of the position
- Bitboard empty_squares() const;
- Bitboard occupied_squares() const;
- Bitboard pieces(Color c) const;
- Bitboard pieces(PieceType pt) const;
- Bitboard pieces(PieceType pt, Color c) const;
- Bitboard pieces(PieceType pt1, PieceType pt2) const;
- Bitboard pieces(PieceType pt1, PieceType pt2, Color c) const;
-
- // Number of pieces of each color and type
- int piece_count(Color c, PieceType pt) const;
-
- // The en passant square
- Square ep_square() const;
-
- // Current king position for each color
- Square king_square(Color c) const;
-
- // Castling rights
- bool can_castle(CastleRight f) const;
- bool can_castle(Color c) const;
- Square castle_rook_square(CastleRight f) const;
-
- // Bitboards for pinned pieces and discovered check candidates
- Bitboard discovered_check_candidates() const;
- Bitboard pinned_pieces() const;
-
- // Checking pieces and under check information
- Bitboard checkers() const;
- bool in_check() const;
-
- // Piece lists
- const Square* piece_list(Color c, PieceType pt) const;
-
- // Information about attacks to or from a given square
- Bitboard attackers_to(Square s) const;
- Bitboard attackers_to(Square s, Bitboard occ) const;
- Bitboard attacks_from(Piece p, Square s) const;
- static Bitboard attacks_from(Piece p, Square s, Bitboard occ);
- template<PieceType> Bitboard attacks_from(Square s) const;
- template<PieceType> Bitboard attacks_from(Square s, Color c) const;
-
- // Properties of moves
- bool move_gives_check(Move m, const CheckInfo& ci) const;
- bool move_attacks_square(Move m, Square s) const;
- bool pl_move_is_legal(Move m, Bitboard pinned) const;
- bool is_pseudo_legal(const Move m) const;
- bool is_capture(Move m) const;
- bool is_capture_or_promotion(Move m) const;
- bool is_passed_pawn_push(Move m) const;
-
- // Piece captured with previous moves
- PieceType captured_piece_type() const;
-
- // Information about pawns
- bool pawn_is_passed(Color c, Square s) const;
-
- // Doing and undoing moves
- void do_move(Move m, StateInfo& st);
- void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
- void undo_move(Move m);
- void do_null_move(StateInfo& st);
- void undo_null_move();
-
- // Static exchange evaluation
- int see(Move m) const;
- int see_sign(Move m) const;
-
- // Accessing hash keys
- Key get_key() const;
- Key get_exclusion_key() const;
- Key get_pawn_key() const;
- Key get_material_key() const;
-
- // Incremental evaluation
- Score value() const;
- Value non_pawn_material(Color c) const;
- Score pst_delta(Piece piece, Square from, Square to) const;
-
- // Game termination checks
- bool is_mate() const;
- template<bool SkipRepetition> bool is_draw() const;
-
- // Plies from start position to the beginning of search
- int startpos_ply_counter() const;
-
- // Other properties of the position
- bool opposite_colored_bishops() const;
- bool has_pawn_on_7th(Color c) const;
- bool is_chess960() const;
-
- // Current thread ID searching on the position
- int thread() const;
-
- int64_t nodes_searched() const;
- void set_nodes_searched(int64_t n);
-
- // Position consistency check, for debugging
- bool pos_is_ok(int* failedStep = NULL) const;
- void flip_me();
-
- // Global initialization
- static void init();
-
-private:
-
- // Initialization helper functions (used while setting up a position)
- void clear();
- void put_piece(Piece p, Square s);
- void set_castle(int f, Square ksq, Square rsq);
- void set_castling_rights(char token);
- bool move_is_legal(const Move m) const;
-
- // Helper functions for doing and undoing moves
- void do_capture_move(Key& key, PieceType capture, Color them, Square to, bool ep);
- void do_castle_move(Move m);
- void undo_castle_move(Move m);
-
- template<bool FindPinned>
- Bitboard hidden_checkers() const;
-
- // Computing hash keys from scratch (for initialization and debugging)
- Key compute_key() const;
- Key compute_pawn_key() const;
- Key compute_material_key() const;
-
- // Computing incremental evaluation scores and material counts
- Score pst(Piece p, Square s) const;
- Score compute_value() const;
- Value compute_non_pawn_material(Color c) const;
-
- // Board
- Piece board[64]; // [square]
-
- // Bitboards
- Bitboard byTypeBB[8]; // [pieceType]
- Bitboard byColorBB[2]; // [color]
-
- // Piece counts
- int pieceCount[2][8]; // [color][pieceType]
-
- // Piece lists
- Square pieceList[2][8][16]; // [color][pieceType][index]
- int index[64]; // [square]
-
- // Other info
- int castleRightsMask[64]; // [square]
- Square castleRookSquare[16]; // [castleRight]
- StateInfo startState;
- int64_t nodes;
- int startPosPly;
- Color sideToMove;
- int threadID;
- StateInfo* st;
- int chess960;
-
- // Static variables
- static Score pieceSquareTable[16][64]; // [piece][square]
- static Key zobrist[2][8][64]; // [color][pieceType][square]
- static Key zobEp[64]; // [square]
- static Key zobCastle[16]; // [castleRight]
- static Key zobSideToMove;
- static Key zobExclusion;
+ public:
+ static void init();
+
+ Position() = default;
+ Position(const Position&) = delete;
+ Position& operator=(const Position&) = delete;
+
+ // FEN string input/output
+ Position& set(const std::string& fenStr, bool isChess960, StateInfo* si);
+ Position& set(const std::string& code, Color c, StateInfo* si);
+ std::string fen() const;
+
+ // Position representation
+ Bitboard pieces(PieceType pt = ALL_PIECES) const;
+ template<typename... PieceTypes>
+ Bitboard pieces(PieceType pt, PieceTypes... pts) const;
+ Bitboard pieces(Color c) const;
+ template<typename... PieceTypes>
+ Bitboard pieces(Color c, PieceTypes... pts) const;
+ Piece piece_on(Square s) const;
+ Square ep_square() const;
+ bool empty(Square s) const;
+ template<PieceType Pt>
+ int count(Color c) const;
+ template<PieceType Pt>
+ int count() const;
+ template<PieceType Pt>
+ Square square(Color c) const;
+
+ // Castling
+ CastlingRights castling_rights(Color c) const;
+ bool can_castle(CastlingRights cr) const;
+ bool castling_impeded(CastlingRights cr) const;
+ Square castling_rook_square(CastlingRights cr) const;
+
+ // Checking
+ Bitboard checkers() const;
+ Bitboard blockers_for_king(Color c) const;
+ Bitboard check_squares(PieceType pt) const;
+ Bitboard pinners(Color c) const;
+
+ // Attacks to/from a given square
+ Bitboard attackers_to(Square s) const;
+ Bitboard attackers_to(Square s, Bitboard occupied) const;
+ void update_slider_blockers(Color c) const;
+ template<PieceType Pt>
+ Bitboard attacks_by(Color c) const;
+
+ // Properties of moves
+ bool legal(Move m) const;
+ bool pseudo_legal(const Move m) const;
+ bool capture(Move m) const;
+ bool capture_stage(Move m) const;
+ bool gives_check(Move m) const;
+ Piece moved_piece(Move m) const;
+ Piece captured_piece() const;
+
+ // Doing and undoing moves
+ void do_move(Move m, StateInfo& newSt);
+ void do_move(Move m, StateInfo& newSt, bool givesCheck);
+ void undo_move(Move m);
+ void do_null_move(StateInfo& newSt, TranspositionTable& tt);
+ void undo_null_move();
+
+ // Static Exchange Evaluation
+ bool see_ge(Move m, int threshold = 0) const;
+
+ // Accessing hash keys
+ Key key() const;
+ Key key_after(Move m) const;
+ Key material_key() const;
+ Key pawn_key() const;
+
+ // Other properties of the position
+ Color side_to_move() const;
+ int game_ply() const;
+ bool is_chess960() const;
+ bool is_draw(int ply) const;
+ bool upcoming_repetition(int ply) const;
+ bool has_repeated() const;
+ int rule50_count() const;
+ Value non_pawn_material(Color c) const;
+ Value non_pawn_material() const;
+
+ // Position consistency check, for debugging
+ bool pos_is_ok() const;
+ void flip();
+
+ // Used by NNUE
+ StateInfo* state() const;
+
+ void put_piece(Piece pc, Square s);
+ void remove_piece(Square s);
+
+ private:
+ // Initialization helpers (used while setting up a position)
+ void set_castling_right(Color c, Square rfrom);
+ void set_state() const;
+ void set_check_info() const;
+
+ // Other helpers
+ void move_piece(Square from, Square to);
+ template<bool Do>
+ void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
+ template<bool AfterMove>
+ Key adjust_key50(Key k) const;
+
+ // Data members
+ Piece board[SQUARE_NB];
+ Bitboard byTypeBB[PIECE_TYPE_NB];
+ Bitboard byColorBB[COLOR_NB];
+ int pieceCount[PIECE_NB];
+ int castlingRightsMask[SQUARE_NB];
+ Square castlingRookSquare[CASTLING_RIGHT_NB];
+ Bitboard castlingPath[CASTLING_RIGHT_NB];
+ StateInfo* st;
+ int gamePly;
+ Color sideToMove;
+ bool chess960;