2121#define POSITION_H_INCLUDED
2222
2323#include < cassert>
24- #include < cstddef>
24+ #include < cstddef> // For offsetof()
2525
2626#include " bitboard.h"
2727#include " types.h"
2828
29-
30- // / The checkInfo struct is initialized at c'tor time and keeps info used
31- // / to detect if a move gives check.
3229class Position ;
3330struct Thread ;
3431
32+ // / CheckInfo struct is initialized at c'tor time and keeps info used to detect
33+ // / if a move gives check.
34+
3535struct CheckInfo {
3636
3737 explicit CheckInfo (const Position&);
3838
3939 Bitboard dcCandidates;
4040 Bitboard pinned;
4141 Bitboard checkSq[PIECE_TYPE_NB];
42- Square ksq;
42+ Square ksq;
4343};
4444
4545
46- // / The StateInfo struct stores information needed 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
49- // / object must be passed as a parameter.
46+ // / StateInfo struct stores information needed to restore a Position object to
47+ // / its previous state when we retract a move. Whenever a move is made on the
48+ // / board (by calling Position::do_move), a StateInfo object must be passed.
5049
5150struct StateInfo {
52- Key pawnKey, materialKey;
53- Value nonPawnMaterial[COLOR_NB];
54- int castlingRights, rule50, pliesFromNull;
55- Score psq;
51+
52+ // Copied when making a move
53+ Key pawnKey;
54+ Key materialKey;
55+ Value nonPawnMaterial[COLOR_NB];
56+ int castlingRights;
57+ int rule50;
58+ int pliesFromNull;
59+ Score psq;
5660 Square epSquare;
5761
58- Key key;
59- Bitboard checkersBB;
60- PieceType capturedType;
62+ // Not copied when making a move
63+ Key key;
64+ Bitboard checkersBB;
65+ PieceType capturedType;
6166 StateInfo* previous;
6267};
6368
6469
6570// / 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.
71+ // / the new one. Here we calculate the quad words (64 bit ) needed to be copied.
6772const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof (uint64_t ) + 1 ;
6873
6974
70- // / The Position class stores the information regarding the board representation
71- // / like pieces, side to move, hash keys, castling info, etc. The most important
72- // / methods are do_move() and undo_move(), used by the search to update node info
73- // / when traversing the search tree.
75+ // / Position class stores information regarding the board representation as
76+ // / pieces, side to move, hash keys, castling info, etc. Important methods are
77+ // / do_move() and undo_move(), used by the search to update node info when
78+ // / traversing the search tree.
7479
7580class Position {
7681
7782 friend std::ostream& operator <<(std::ostream&, const Position&);
7883
79- // Disable the default copy constructor
80- Position (const Position&);
84+ Position (const Position&); // Disable the default copy constructor
8185
8286public:
83- Position () {}
87+ static void init ();
88+
89+ Position () {} // To define the global object RootPos
8490 Position (const Position& pos, Thread* th) { *this = pos; thisThread = th; }
8591 Position (const std::string& f, bool c960, Thread* th) { set (f, c960, th); }
86- Position& operator =(const Position&);
87- static void init ();
92+ Position& operator =(const Position&); // To assign RootPos from UCI
8893
8994 // FEN string input/output
9095 void set (const std::string& fenStr, bool isChess960, Thread* th);
@@ -135,7 +140,6 @@ class Position {
135140 // Piece specific
136141 bool pawn_passed (Color c, Square s) const ;
137142 bool pawn_on_7th (Color c) const ;
138- bool bishop_pair (Color c) const ;
139143 bool opposite_bishops () const ;
140144
141145 // Doing and undoing moves
@@ -153,12 +157,8 @@ class Position {
153157 Key key () const ;
154158 Key key_after (Move m) const ;
155159 Key exclusion_key () const ;
156- Key pawn_key () const ;
157160 Key material_key () const ;
158-
159- // Incremental piece-square evaluation
160- Score psq_score () const ;
161- Value non_pawn_material (Color c) const ;
161+ Key pawn_key () const ;
162162
163163 // Other properties of the position
164164 Color side_to_move () const ;
@@ -170,6 +170,8 @@ class Position {
170170 void set_nodes_searched (uint64_t n);
171171 bool is_draw () const ;
172172 int rule50_count () const ;
173+ Score psq_score () const ;
174+ Value non_pawn_material (Color c) const ;
173175
174176 // Position consistency check, for debugging
175177 bool pos_is_ok (int * step = NULL ) const ;
@@ -181,23 +183,21 @@ class Position {
181183 void set_castling_right (Color c, Square rfrom);
182184 void set_state (StateInfo* si) const ;
183185
184- // Helper functions
186+ // Other helpers
185187 Bitboard check_blockers (Color c, Color kingColor) const ;
186188 void put_piece (Square s, Color c, PieceType pt);
187189 void remove_piece (Square s, Color c, PieceType pt);
188190 void move_piece (Square from, Square to, Color c, PieceType pt);
189191 template <bool Do>
190192 void do_castling (Square from, Square& to, Square& rfrom, Square& rto);
191193
192- // Board and pieces
194+ // Data members
193195 Piece board[SQUARE_NB];
194196 Bitboard byTypeBB[PIECE_TYPE_NB];
195197 Bitboard byColorBB[COLOR_NB];
196198 int pieceCount[COLOR_NB][PIECE_TYPE_NB];
197199 Square pieceList[COLOR_NB][PIECE_TYPE_NB][16 ];
198200 int index[SQUARE_NB];
199-
200- // Other info
201201 int castlingRightsMask[SQUARE_NB];
202202 Square castlingRookSquare[CASTLING_RIGHT_NB];
203203 Bitboard castlingPath[CASTLING_RIGHT_NB];
@@ -210,12 +210,12 @@ class Position {
210210 bool chess960;
211211};
212212
213- inline uint64_t Position::nodes_searched () const {
214- return nodes ;
213+ inline Color Position::side_to_move () const {
214+ return sideToMove ;
215215}
216216
217- inline void Position::set_nodes_searched ( uint64_t n) {
218- nodes = n ;
217+ inline bool Position::empty (Square s) const {
218+ return board[s] == NO_PIECE ;
219219}
220220
221221inline Piece Position::piece_on (Square s) const {
@@ -226,14 +226,6 @@ inline Piece Position::moved_piece(Move m) const {
226226 return board[from_sq (m)];
227227}
228228
229- inline bool Position::empty (Square s) const {
230- return board[s] == NO_PIECE;
231- }
232-
233- inline Color Position::side_to_move () const {
234- return sideToMove;
235- }
236-
237229inline Bitboard Position::pieces () const {
238230 return byTypeBB[ALL_PIECES];
239231}
@@ -266,14 +258,14 @@ template<PieceType Pt> inline const Square* Position::list(Color c) const {
266258 return pieceList[c][Pt];
267259}
268260
269- inline Square Position::ep_square () const {
270- return st->epSquare ;
271- }
272-
273261inline Square Position::king_square (Color c) const {
274262 return pieceList[c][KING][0 ];
275263}
276264
265+ inline Square Position::ep_square () const {
266+ return st->epSquare ;
267+ }
268+
277269inline int Position::can_castle (CastlingRight cr) const {
278270 return st->castlingRights & cr;
279271}
@@ -292,7 +284,6 @@ inline Square Position::castling_rook_square(CastlingRight cr) const {
292284
293285template <PieceType Pt>
294286inline Bitboard Position::attacks_from (Square s) const {
295-
296287 return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
297288 : Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
298289 : StepAttacksBB[Pt][s];
@@ -360,19 +351,20 @@ inline int Position::rule50_count() const {
360351 return st->rule50 ;
361352}
362353
363- inline bool Position::opposite_bishops () const {
354+ inline uint64_t Position::nodes_searched () const {
355+ return nodes;
356+ }
357+
358+ inline void Position::set_nodes_searched (uint64_t n) {
359+ nodes = n;
360+ }
364361
362+ inline bool Position::opposite_bishops () const {
365363 return pieceCount[WHITE][BISHOP] == 1
366364 && pieceCount[BLACK][BISHOP] == 1
367365 && opposite_colors (pieceList[WHITE][BISHOP][0 ], pieceList[BLACK][BISHOP][0 ]);
368366}
369367
370- inline bool Position::bishop_pair (Color c) const {
371-
372- return pieceCount[c][BISHOP] >= 2
373- && opposite_colors (pieceList[c][BISHOP][0 ], pieceList[c][BISHOP][1 ]);
374- }
375-
376368inline bool Position::pawn_on_7th (Color c) const {
377369 return pieces (c, PAWN) & rank_bb (relative_rank (c, RANK_7));
378370}
@@ -389,7 +381,7 @@ inline bool Position::capture_or_promotion(Move m) const {
389381
390382inline bool Position::capture (Move m) const {
391383
392- // Note that castling is encoded as "king captures the rook"
384+ // Castling is encoded as "king captures the rook"
393385 assert (is_ok (m));
394386 return (!empty (to_sq (m)) && type_of (m) != CASTLING) || type_of (m) == ENPASSANT;
395387}
@@ -415,8 +407,8 @@ inline void Position::put_piece(Square s, Color c, PieceType pt) {
415407
416408inline void Position::move_piece (Square from, Square to, Color c, PieceType pt) {
417409
418- // index[from] is not updated and becomes stale. This works as long
419- // as index[] is accessed just by known occupied squares.
410+ // index[from] is not updated and becomes stale. This works as long as index[]
411+ // is accessed just by known occupied squares.
420412 Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
421413 byTypeBB[ALL_PIECES] ^= from_to_bb;
422414 byTypeBB[pt] ^= from_to_bb;
@@ -436,7 +428,7 @@ inline void Position::remove_piece(Square s, Color c, PieceType pt) {
436428 byTypeBB[ALL_PIECES] ^= s;
437429 byTypeBB[pt] ^= s;
438430 byColorBB[c] ^= s;
439- /* board[s] = NO_PIECE; */ // Not needed, will be overwritten by capturing
431+ /* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
440432 Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
441433 index[lastSquare] = index[s];
442434 pieceList[c][pt][index[lastSquare]] = lastSquare;
0 commit comments