Rev 154 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 154 | Rev 169 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
5 | Copyright (C) 2015- |
5 | Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
6 | 6 | ||
7 | Stockfish is free software: you can redistribute it and/or modify |
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 |
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 |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. |
10 | (at your option) any later version. |
Line 54... | Line 54... | ||
54 | Bitboard blockersForKing[COLOR_NB]; |
54 | Bitboard blockersForKing[COLOR_NB]; |
55 | Bitboard pinnersForKing[COLOR_NB]; |
55 | Bitboard pinnersForKing[COLOR_NB]; |
56 | Bitboard checkSquares[PIECE_TYPE_NB]; |
56 | Bitboard checkSquares[PIECE_TYPE_NB]; |
57 | }; |
57 | }; |
58 | 58 | ||
- | 59 | /// A list to keep track of the position states along the setup moves (from the |
|
- | 60 | /// start position to the position just before the search starts). Needed by |
|
- | 61 | /// 'draw by repetition' detection. Use a std::deque because pointers to |
|
59 | // |
62 | /// elements are not invalidated upon list resizing. |
60 | typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr; |
63 | typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr; |
61 | 64 | ||
62 | 65 | ||
63 | /// Position class stores information regarding the board representation as |
66 | /// Position class stores information regarding the board representation as |
64 | /// pieces, side to move, hash keys, castling info, etc. Important methods are |
67 | /// pieces, side to move, hash keys, castling info, etc. Important methods are |
Line 74... | Line 77... | ||
74 | Position(const Position&) = delete; |
77 | Position(const Position&) = delete; |
75 | Position& operator=(const Position&) = delete; |
78 | Position& operator=(const Position&) = delete; |
76 | 79 | ||
77 | // FEN string input/output |
80 | // FEN string input/output |
78 | Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th); |
81 | Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th); |
- | 82 | Position& set(const std::string& code, Color c, StateInfo* si); |
|
79 | const std::string fen() const; |
83 | const std::string fen() const; |
80 | 84 | ||
81 | // Position representation |
85 | // Position representation |
82 | Bitboard pieces() const; |
86 | Bitboard pieces() const; |
83 | Bitboard pieces(PieceType pt) const; |
87 | Bitboard pieces(PieceType pt) const; |
Line 87... | Line 91... | ||
87 | Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const; |
91 | Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const; |
88 | Piece piece_on(Square s) const; |
92 | Piece piece_on(Square s) const; |
89 | Square ep_square() const; |
93 | Square ep_square() const; |
90 | bool empty(Square s) const; |
94 | bool empty(Square s) const; |
91 | template<PieceType Pt> int count(Color c) const; |
95 | template<PieceType Pt> int count(Color c) const; |
- | 96 | template<PieceType Pt> int count() const; |
|
92 | template<PieceType Pt> const Square* squares(Color c) const; |
97 | template<PieceType Pt> const Square* squares(Color c) const; |
93 | template<PieceType Pt> Square square(Color c) const; |
98 | template<PieceType Pt> Square square(Color c) const; |
94 | 99 | ||
95 | // Castling |
100 | // Castling |
96 | int can_castle(Color c) const; |
101 | int can_castle(Color c) const; |
Line 105... | Line 110... | ||
105 | Bitboard check_squares(PieceType pt) const; |
110 | Bitboard check_squares(PieceType pt) const; |
106 | 111 | ||
107 | // Attacks to/from a given square |
112 | // Attacks to/from a given square |
108 | Bitboard attackers_to(Square s) const; |
113 | Bitboard attackers_to(Square s) const; |
109 | Bitboard attackers_to(Square s, Bitboard occupied) const; |
114 | Bitboard attackers_to(Square s, Bitboard occupied) const; |
110 | Bitboard attacks_from( |
115 | Bitboard attacks_from(PieceType pt, Square s) const; |
111 | template<PieceType> Bitboard attacks_from(Square s) const; |
116 | template<PieceType> Bitboard attacks_from(Square s) const; |
112 | template<PieceType> Bitboard attacks_from(Square s, Color c) const; |
117 | template<PieceType> Bitboard attacks_from(Square s, Color c) const; |
113 | Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const; |
118 | Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const; |
114 | 119 | ||
115 | // Properties of moves |
120 | // Properties of moves |
Line 125... | Line 130... | ||
125 | // Piece specific |
130 | // Piece specific |
126 | bool pawn_passed(Color c, Square s) const; |
131 | bool pawn_passed(Color c, Square s) const; |
127 | bool opposite_bishops() const; |
132 | bool opposite_bishops() const; |
128 | 133 | ||
129 | // Doing and undoing moves |
134 | // Doing and undoing moves |
- | 135 | void do_move(Move m, StateInfo& newSt); |
|
130 | void do_move(Move m, StateInfo& |
136 | void do_move(Move m, StateInfo& newSt, bool givesCheck); |
131 | void undo_move(Move m); |
137 | void undo_move(Move m); |
132 | void do_null_move(StateInfo& |
138 | void do_null_move(StateInfo& newSt); |
133 | void undo_null_move(); |
139 | void undo_null_move(); |
134 | 140 | ||
135 | // Static Exchange Evaluation |
141 | // Static Exchange Evaluation |
136 | bool see_ge(Move m, Value |
142 | bool see_ge(Move m, Value threshold = VALUE_ZERO) const; |
137 | 143 | ||
138 | // Accessing hash keys |
144 | // Accessing hash keys |
139 | Key key() const; |
145 | Key key() const; |
140 | Key key_after(Move m) const; |
146 | Key key_after(Move m) const; |
141 | Key material_key() const; |
147 | Key material_key() const; |
142 | Key pawn_key() const; |
148 | Key pawn_key() const; |
143 | 149 | ||
144 | // Other properties of the position |
150 | // Other properties of the position |
145 | Color side_to_move() const; |
151 | Color side_to_move() const; |
146 | Phase game_phase() const; |
- | |
147 | int game_ply() const; |
152 | int game_ply() const; |
148 | bool is_chess960() const; |
153 | bool is_chess960() const; |
149 | Thread* this_thread() const; |
154 | Thread* this_thread() const; |
150 | uint64_t nodes_searched() const; |
- | |
151 | bool is_draw() const; |
155 | bool is_draw(int ply) const; |
152 | int rule50_count() const; |
156 | int rule50_count() const; |
153 | Score psq_score() const; |
157 | Score psq_score() const; |
154 | Value non_pawn_material(Color c) const; |
158 | Value non_pawn_material(Color c) const; |
- | 159 | Value non_pawn_material() const; |
|
155 | 160 | ||
156 | // Position consistency check, for debugging |
161 | // Position consistency check, for debugging |
157 | bool pos_is_ok( |
162 | bool pos_is_ok() const; |
158 | void flip(); |
163 | void flip(); |
159 | 164 | ||
160 | private: |
165 | private: |
161 | // Initialization helpers (used while setting up a position) |
166 | // Initialization helpers (used while setting up a position) |
162 | void set_castling_right(Color c, Square rfrom); |
167 | void set_castling_right(Color c, Square rfrom); |
Line 178... | Line 183... | ||
178 | Square pieceList[PIECE_NB][16]; |
183 | Square pieceList[PIECE_NB][16]; |
179 | int index[SQUARE_NB]; |
184 | int index[SQUARE_NB]; |
180 | int castlingRightsMask[SQUARE_NB]; |
185 | int castlingRightsMask[SQUARE_NB]; |
181 | Square castlingRookSquare[CASTLING_RIGHT_NB]; |
186 | Square castlingRookSquare[CASTLING_RIGHT_NB]; |
182 | Bitboard castlingPath[CASTLING_RIGHT_NB]; |
187 | Bitboard castlingPath[CASTLING_RIGHT_NB]; |
183 | uint64_t nodes; |
- | |
184 | int gamePly; |
188 | int gamePly; |
185 | Color sideToMove; |
189 | Color sideToMove; |
186 | Thread* thisThread; |
190 | Thread* thisThread; |
187 | StateInfo* st; |
191 | StateInfo* st; |
188 | bool chess960; |
192 | bool chess960; |
Line 230... | Line 234... | ||
230 | return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]); |
234 | return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]); |
231 | } |
235 | } |
232 | 236 | ||
233 | template<PieceType Pt> inline int Position::count(Color c) const { |
237 | template<PieceType Pt> inline int Position::count(Color c) const { |
234 | return pieceCount[make_piece(c, Pt)]; |
238 | return pieceCount[make_piece(c, Pt)]; |
- | 239 | } |
|
- | 240 | ||
- | 241 | template<PieceType Pt> inline int Position::count() const { |
|
- | 242 | return pieceCount[make_piece(WHITE, Pt)] + pieceCount[make_piece(BLACK, Pt)]; |
|
235 | } |
243 | } |
236 | 244 | ||
237 | template<PieceType Pt> inline const Square* Position::squares(Color c) const { |
245 | template<PieceType Pt> inline const Square* Position::squares(Color c) const { |
238 | return pieceList[make_piece(c, Pt)]; |
246 | return pieceList[make_piece(c, Pt)]; |
239 | } |
247 | } |
Line 263... | Line 271... | ||
263 | return castlingRookSquare[cr]; |
271 | return castlingRookSquare[cr]; |
264 | } |
272 | } |
265 | 273 | ||
266 | template<PieceType Pt> |
274 | template<PieceType Pt> |
267 | inline Bitboard Position::attacks_from(Square s) const { |
275 | inline Bitboard Position::attacks_from(Square s) const { |
- | 276 | assert(Pt != PAWN); |
|
268 | return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES]) |
277 | return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES]) |
269 | : Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s) |
278 | : Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s) |
270 | : |
279 | : PseudoAttacks[Pt][s]; |
271 | } |
280 | } |
272 | 281 | ||
273 | template<> |
282 | template<> |
274 | inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const { |
283 | inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const { |
275 | return |
284 | return PawnAttacks[c][s]; |
276 | } |
285 | } |
277 | 286 | ||
278 | inline Bitboard Position::attacks_from( |
287 | inline Bitboard Position::attacks_from(PieceType pt, Square s) const { |
279 | return attacks_bb( |
288 | return attacks_bb(pt, s, byTypeBB[ALL_PIECES]); |
280 | } |
289 | } |
281 | 290 | ||
282 | inline Bitboard Position::attackers_to(Square s) const { |
291 | inline Bitboard Position::attackers_to(Square s) const { |
283 | return attackers_to(s, byTypeBB[ALL_PIECES]); |
292 | return attackers_to(s, byTypeBB[ALL_PIECES]); |
284 | } |
293 | } |
Line 299... | Line 308... | ||
299 | return st->checkSquares[pt]; |
308 | return st->checkSquares[pt]; |
300 | } |
309 | } |
301 | 310 | ||
302 | inline bool Position::pawn_passed(Color c, Square s) const { |
311 | inline bool Position::pawn_passed(Color c, Square s) const { |
303 | return !(pieces(~c, PAWN) & passed_pawn_mask(c, s)); |
312 | return !(pieces(~c, PAWN) & passed_pawn_mask(c, s)); |
304 | } |
313 | } |
305 | 314 | ||
306 | inline bool Position::advanced_pawn_push(Move m) const { |
315 | inline bool Position::advanced_pawn_push(Move m) const { |
307 | return type_of(moved_piece(m)) == PAWN |
316 | return type_of(moved_piece(m)) == PAWN |
308 | && relative_rank(sideToMove, from_sq(m)) > RANK_4; |
317 | && relative_rank(sideToMove, from_sq(m)) > RANK_4; |
309 | } |
318 | } |
310 | 319 | ||
311 | inline Key Position::key() const { |
320 | inline Key Position::key() const { |
312 | return st->key; |
321 | return st->key; |
313 | } |
322 | } |
314 | 323 | ||
315 | inline Key Position::pawn_key() const { |
324 | inline Key Position::pawn_key() const { |
316 | return st->pawnKey; |
325 | return st->pawnKey; |
317 | } |
326 | } |
318 | 327 | ||
319 | inline Key Position::material_key() const { |
328 | inline Key Position::material_key() const { |
320 | return st->materialKey; |
329 | return st->materialKey; |
321 | } |
330 | } |
322 | 331 | ||
323 | inline Score Position::psq_score() const { |
332 | inline Score Position::psq_score() const { |
324 | return st->psq; |
333 | return st->psq; |
325 | } |
334 | } |
326 | 335 | ||
327 | inline Value Position::non_pawn_material(Color c) const { |
336 | inline Value Position::non_pawn_material(Color c) const { |
328 | return st->nonPawnMaterial[c]; |
337 | return st->nonPawnMaterial[c]; |
329 | } |
338 | } |
- | 339 | ||
- | 340 | inline Value Position::non_pawn_material() const { |
|
- | 341 | return st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK]; |
|
- | 342 | } |
|
330 | 343 | ||
331 | inline int Position::game_ply() const { |
344 | inline int Position::game_ply() const { |
332 | return gamePly; |
345 | return gamePly; |
333 | } |
346 | } |
334 | 347 | ||
335 | inline int Position::rule50_count() const { |
348 | inline int Position::rule50_count() const { |
336 | return st->rule50; |
349 | return st->rule50; |
337 | } |
350 | } |
338 | - | ||
339 | inline uint64_t Position::nodes_searched() const { |
- | |
340 | return nodes; |
- | |
341 | } |
- | |
342 | 351 | ||
343 | inline bool Position::opposite_bishops() const { |
352 | inline bool Position::opposite_bishops() const { |
344 | return pieceCount[W_BISHOP] == 1 |
353 | return pieceCount[W_BISHOP] == 1 |
345 | && pieceCount[B_BISHOP] == 1 |
354 | && pieceCount[B_BISHOP] == 1 |
346 | && opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK)); |
355 | && opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK)); |
347 | } |
356 | } |
348 | 357 | ||
349 | inline bool Position::is_chess960() const { |
358 | inline bool Position::is_chess960() const { |
350 | return chess960; |
359 | return chess960; |
351 | } |
360 | } |
352 | 361 | ||
353 | inline bool Position::capture_or_promotion(Move m) const { |
362 | inline bool Position::capture_or_promotion(Move m) const { |
Line 407... | Line 416... | ||
407 | byColorBB[color_of(pc)] ^= from_to_bb; |
416 | byColorBB[color_of(pc)] ^= from_to_bb; |
408 | board[from] = NO_PIECE; |
417 | board[from] = NO_PIECE; |
409 | board[to] = pc; |
418 | board[to] = pc; |
410 | index[to] = index[from]; |
419 | index[to] = index[from]; |
411 | pieceList[pc][index[to]] = to; |
420 | pieceList[pc][index[to]] = to; |
- | 421 | } |
|
- | 422 | ||
- | 423 | inline void Position::do_move(Move m, StateInfo& newSt) { |
|
- | 424 | do_move(m, newSt, gives_check(m)); |
|
412 | } |
425 | } |
413 | 426 | ||
414 | #endif // #ifndef POSITION_H_INCLUDED |
427 | #endif // #ifndef POSITION_H_INCLUDED |