Subversion Repositories Games.Chess Giants

Rev

Rev 154 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  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
  6.  
  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.
  11.  
  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.
  16.  
  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/>.
  19. */
  20.  
  21. #ifndef POSITION_H_INCLUDED
  22. #define POSITION_H_INCLUDED
  23.  
  24. #include <cassert>
  25. #include <cstddef>  // For offsetof()
  26. #include <string>
  27.  
  28. #include "bitboard.h"
  29. #include "types.h"
  30.  
  31. class Position;
  32. class Thread;
  33.  
  34. namespace PSQT {
  35.  
  36.   extern Score psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
  37.  
  38.   void init();
  39. }
  40.  
  41. /// CheckInfo struct is initialized at constructor time and keeps info used to
  42. /// detect if a move gives check.
  43.  
  44. struct CheckInfo {
  45.  
  46.   explicit CheckInfo(const Position&);
  47.  
  48.   Bitboard dcCandidates;
  49.   Bitboard pinned;
  50.   Bitboard checkSquares[PIECE_TYPE_NB];
  51.   Square   ksq;
  52. };
  53.  
  54.  
  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.
  58.  
  59. struct StateInfo {
  60.  
  61.   // Copied when making a move
  62.   Key    pawnKey;
  63.   Key    materialKey;
  64.   Value  nonPawnMaterial[COLOR_NB];
  65.   int    castlingRights;
  66.   int    rule50;
  67.   int    pliesFromNull;
  68.   Score  psq;
  69.   Square epSquare;
  70.  
  71.   // Not copied when making a move
  72.   Key        key;
  73.   Bitboard   checkersBB;
  74.   PieceType  capturedType;
  75.   StateInfo* previous;
  76. };
  77.  
  78.  
  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.
  83.  
  84. class Position {
  85.  
  86. public:
  87.   static void init();
  88.  
  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
  94.  
  95.   // FEN string input/output
  96.   void set(const std::string& fenStr, bool isChess960, Thread* th);
  97.   const std::string fen() const;
  98.  
  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;
  112.  
  113.   // Castling
  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;
  118.  
  119.   // Checking
  120.   Bitboard checkers() const;
  121.   Bitboard discovered_check_candidates() const;
  122.   Bitboard pinned_pieces(Color c) const;
  123.  
  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;
  130.  
  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;
  140.  
  141.   // Piece specific
  142.   bool pawn_passed(Color c, Square s) const;
  143.   bool opposite_bishops() const;
  144.  
  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();
  150.  
  151.   // Static exchange evaluation
  152.   Value see(Move m) const;
  153.   Value see_sign(Move m) const;
  154.  
  155.   // Accessing hash keys
  156.   Key key() const;
  157.   Key key_after(Move m) const;
  158.   Key exclusion_key() const;
  159.   Key material_key() const;
  160.   Key pawn_key() const;
  161.  
  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;
  174.  
  175.   // Position consistency check, for debugging
  176.   bool pos_is_ok(int* failedStep = nullptr) const;
  177.   void flip();
  178.  
  179. private:
  180.   // Initialization helpers (used while setting up a position)
  181.   void clear();
  182.   void set_castling_right(Color c, Square rfrom);
  183.   void set_state(StateInfo* si) const;
  184.  
  185.   // Other helpers
  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);
  190.   template<bool Do>
  191.   void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
  192.  
  193.   // Data members
  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;
  204.   uint64_t nodes;
  205.   int gamePly;
  206.   Color sideToMove;
  207.   Thread* thisThread;
  208.   StateInfo* st;
  209.   bool chess960;
  210. };
  211.  
  212. extern std::ostream& operator<<(std::ostream& os, const Position& pos);
  213.  
  214. inline Color Position::side_to_move() const {
  215.   return sideToMove;
  216. }
  217.  
  218. inline bool Position::empty(Square s) const {
  219.   return board[s] == NO_PIECE;
  220. }
  221.  
  222. inline Piece Position::piece_on(Square s) const {
  223.   return board[s];
  224. }
  225.  
  226. inline Piece Position::moved_piece(Move m) const {
  227.   return board[from_sq(m)];
  228. }
  229.  
  230. inline Bitboard Position::pieces() const {
  231.   return byTypeBB[ALL_PIECES];
  232. }
  233.  
  234. inline Bitboard Position::pieces(PieceType pt) const {
  235.   return byTypeBB[pt];
  236. }
  237.  
  238. inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
  239.   return byTypeBB[pt1] | byTypeBB[pt2];
  240. }
  241.  
  242. inline Bitboard Position::pieces(Color c) const {
  243.   return byColorBB[c];
  244. }
  245.  
  246. inline Bitboard Position::pieces(Color c, PieceType pt) const {
  247.   return byColorBB[c] & byTypeBB[pt];
  248. }
  249.  
  250. inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
  251.   return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
  252. }
  253.  
  254. template<PieceType Pt> inline int Position::count(Color c) const {
  255.   return pieceCount[c][Pt];
  256. }
  257.  
  258. template<PieceType Pt> inline const Square* Position::squares(Color c) const {
  259.   return pieceList[c][Pt];
  260. }
  261.  
  262. template<PieceType Pt> inline Square Position::square(Color c) const {
  263.   assert(pieceCount[c][Pt] == 1);
  264.   return pieceList[c][Pt][0];
  265. }
  266.  
  267. inline Square Position::ep_square() const {
  268.   return st->epSquare;
  269. }
  270.  
  271. inline int Position::can_castle(CastlingRight cr) const {
  272.   return st->castlingRights & cr;
  273. }
  274.  
  275. inline int Position::can_castle(Color c) const {
  276.   return st->castlingRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
  277. }
  278.  
  279. inline bool Position::castling_impeded(CastlingRight cr) const {
  280.   return byTypeBB[ALL_PIECES] & castlingPath[cr];
  281. }
  282.  
  283. inline Square Position::castling_rook_square(CastlingRight cr) const {
  284.   return castlingRookSquare[cr];
  285. }
  286.  
  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];
  292. }
  293.  
  294. template<>
  295. inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
  296.   return StepAttacksBB[make_piece(c, PAWN)][s];
  297. }
  298.  
  299. inline Bitboard Position::attacks_from(Piece pc, Square s) const {
  300.   return attacks_bb(pc, s, byTypeBB[ALL_PIECES]);
  301. }
  302.  
  303. inline Bitboard Position::attackers_to(Square s) const {
  304.   return attackers_to(s, byTypeBB[ALL_PIECES]);
  305. }
  306.  
  307. inline Bitboard Position::checkers() const {
  308.   return st->checkersBB;
  309. }
  310.  
  311. inline Bitboard Position::discovered_check_candidates() const {
  312.   return check_blockers(sideToMove, ~sideToMove);
  313. }
  314.  
  315. inline Bitboard Position::pinned_pieces(Color c) const {
  316.   return check_blockers(c, c);
  317. }
  318.  
  319. inline bool Position::pawn_passed(Color c, Square s) const {
  320.   return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
  321. }
  322.  
  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;
  326. }
  327.  
  328. inline Key Position::key() const {
  329.   return st->key;
  330. }
  331.  
  332. inline Key Position::pawn_key() const {
  333.   return st->pawnKey;
  334. }
  335.  
  336. inline Key Position::material_key() const {
  337.   return st->materialKey;
  338. }
  339.  
  340. inline Score Position::psq_score() const {
  341.   return st->psq;
  342. }
  343.  
  344. inline Value Position::non_pawn_material(Color c) const {
  345.   return st->nonPawnMaterial[c];
  346. }
  347.  
  348. inline int Position::game_ply() const {
  349.   return gamePly;
  350. }
  351.  
  352. inline int Position::rule50_count() const {
  353.   return st->rule50;
  354. }
  355.  
  356. inline uint64_t Position::nodes_searched() const {
  357.   return nodes;
  358. }
  359.  
  360. inline void Position::set_nodes_searched(uint64_t n) {
  361.   nodes = n;
  362. }
  363.  
  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));
  368. }
  369.  
  370. inline bool Position::is_chess960() const {
  371.   return chess960;
  372. }
  373.  
  374. inline bool Position::capture_or_promotion(Move m) const {
  375.  
  376.   assert(is_ok(m));
  377.   return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
  378. }
  379.  
  380. inline bool Position::capture(Move m) const {
  381.  
  382.   // Castling is encoded as "king captures the rook"
  383.   assert(is_ok(m));
  384.   return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
  385. }
  386.  
  387. inline PieceType Position::captured_piece_type() const {
  388.   return st->capturedType;
  389. }
  390.  
  391. inline Thread* Position::this_thread() const {
  392.   return thisThread;
  393. }
  394.  
  395. inline void Position::put_piece(Color c, PieceType pt, Square s) {
  396.  
  397.   board[s] = make_piece(c, pt);
  398.   byTypeBB[ALL_PIECES] |= s;
  399.   byTypeBB[pt] |= s;
  400.   byColorBB[c] |= s;
  401.   index[s] = pieceCount[c][pt]++;
  402.   pieceList[c][pt][index[s]] = s;
  403.   pieceCount[c][ALL_PIECES]++;
  404. }
  405.  
  406. inline void Position::remove_piece(Color c, PieceType pt, Square s) {
  407.  
  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;
  413.   byTypeBB[pt] ^= s;
  414.   byColorBB[c] ^= 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]--;
  421. }
  422.  
  423. inline void Position::move_piece(Color c, PieceType pt, Square from, Square to) {
  424.  
  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;
  435. }
  436.  
  437. #endif // #ifndef POSITION_H_INCLUDED
  438.