Subversion Repositories Games.Chess Giants

Rev

Rev 169 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 169 Rev 185
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-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
5
  Copyright (C) 2015-2019 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 41... Line 41...
41
  Key    materialKey;
41
  Key    materialKey;
42
  Value  nonPawnMaterial[COLOR_NB];
42
  Value  nonPawnMaterial[COLOR_NB];
43
  int    castlingRights;
43
  int    castlingRights;
44
  int    rule50;
44
  int    rule50;
45
  int    pliesFromNull;
45
  int    pliesFromNull;
46
  Score  psq;
-
 
47
  Square epSquare;
46
  Square epSquare;
48
 
47
 
49
  // Not copied when making a move (will be recomputed anyhow)
48
  // Not copied when making a move (will be recomputed anyhow)
50
  Key        key;
49
  Key        key;
51
  Bitboard   checkersBB;
50
  Bitboard   checkersBB;
52
  Piece      capturedPiece;
51
  Piece      capturedPiece;
53
  StateInfo* previous;
52
  StateInfo* previous;
54
  Bitboard   blockersForKing[COLOR_NB];
53
  Bitboard   blockersForKing[COLOR_NB];
55
  Bitboard   pinnersForKing[COLOR_NB];
54
  Bitboard   pinners[COLOR_NB];
56
  Bitboard   checkSquares[PIECE_TYPE_NB];
55
  Bitboard   checkSquares[PIECE_TYPE_NB];
57
};
56
};
58
 
57
 
59
/// A list to keep track of the position states along the setup moves (from the
58
/// 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
59
/// start position to the position just before the search starts). Needed by
Line 103... Line 102...
103
  bool castling_impeded(CastlingRight cr) const;
102
  bool castling_impeded(CastlingRight cr) const;
104
  Square castling_rook_square(CastlingRight cr) const;
103
  Square castling_rook_square(CastlingRight cr) const;
105
 
104
 
106
  // Checking
105
  // Checking
107
  Bitboard checkers() const;
106
  Bitboard checkers() const;
108
  Bitboard discovered_check_candidates() const;
-
 
109
  Bitboard pinned_pieces(Color c) const;
107
  Bitboard blockers_for_king(Color c) const;
110
  Bitboard check_squares(PieceType pt) const;
108
  Bitboard check_squares(PieceType pt) const;
111
 
109
 
112
  // Attacks to/from a given square
110
  // Attacks to/from a given square
113
  Bitboard attackers_to(Square s) const;
111
  Bitboard attackers_to(Square s) const;
114
  Bitboard attackers_to(Square s, Bitboard occupied) const;
112
  Bitboard attackers_to(Square s, Bitboard occupied) const;
Line 151... Line 149...
151
  Color side_to_move() const;
149
  Color side_to_move() const;
152
  int game_ply() const;
150
  int game_ply() const;
153
  bool is_chess960() const;
151
  bool is_chess960() const;
154
  Thread* this_thread() const;
152
  Thread* this_thread() const;
155
  bool is_draw(int ply) const;
153
  bool is_draw(int ply) const;
-
 
154
  bool has_game_cycle(int ply) const;
-
 
155
  bool has_repeated() const;
156
  int rule50_count() const;
156
  int rule50_count() const;
157
  Score psq_score() const;
157
  Score psq_score() const;
158
  Value non_pawn_material(Color c) const;
158
  Value non_pawn_material(Color c) const;
159
  Value non_pawn_material() const;
159
  Value non_pawn_material() const;
160
 
160
 
Line 185... Line 185...
185
  int castlingRightsMask[SQUARE_NB];
185
  int castlingRightsMask[SQUARE_NB];
186
  Square castlingRookSquare[CASTLING_RIGHT_NB];
186
  Square castlingRookSquare[CASTLING_RIGHT_NB];
187
  Bitboard castlingPath[CASTLING_RIGHT_NB];
187
  Bitboard castlingPath[CASTLING_RIGHT_NB];
188
  int gamePly;
188
  int gamePly;
189
  Color sideToMove;
189
  Color sideToMove;
-
 
190
  Score psq;
190
  Thread* thisThread;
191
  Thread* thisThread;
191
  StateInfo* st;
192
  StateInfo* st;
192
  bool chess960;
193
  bool chess960;
193
};
194
};
194
 
195
 
-
 
196
namespace PSQT {
-
 
197
  extern Score psq[PIECE_NB][SQUARE_NB];
-
 
198
}
-
 
199
 
195
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
200
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
196
 
201
 
197
inline Color Position::side_to_move() const {
202
inline Color Position::side_to_move() const {
198
  return sideToMove;
203
  return sideToMove;
199
}
204
}
200
 
205
 
201
inline bool Position::empty(Square s) const {
206
inline bool Position::empty(Square s) const {
202
  return board[s] == NO_PIECE;
207
  return board[s] == NO_PIECE;
203
}
208
}
204
 
209
 
205
inline Piece Position::piece_on(Square s) const {
210
inline Piece Position::piece_on(Square s) const {
206
  return board[s];
211
  return board[s];
207
}
212
}
208
 
213
 
209
inline Piece Position::moved_piece(Move m) const {
214
inline Piece Position::moved_piece(Move m) const {
210
  return board[from_sq(m)];
215
  return board[from_sq(m)];
211
}
216
}
212
 
217
 
213
inline Bitboard Position::pieces() const {
218
inline Bitboard Position::pieces() const {
214
  return byTypeBB[ALL_PIECES];
219
  return byTypeBB[ALL_PIECES];
215
}
220
}
216
 
221
 
217
inline Bitboard Position::pieces(PieceType pt) const {
222
inline Bitboard Position::pieces(PieceType pt) const {
218
  return byTypeBB[pt];
223
  return byTypeBB[pt];
219
}
224
}
220
 
225
 
221
inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
226
inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
222
  return byTypeBB[pt1] | byTypeBB[pt2];
227
  return byTypeBB[pt1] | byTypeBB[pt2];
223
}
228
}
224
 
229
 
225
inline Bitboard Position::pieces(Color c) const {
230
inline Bitboard Position::pieces(Color c) const {
Line 267... Line 272...
267
  return byTypeBB[ALL_PIECES] & castlingPath[cr];
272
  return byTypeBB[ALL_PIECES] & castlingPath[cr];
268
}
273
}
269
 
274
 
270
inline Square Position::castling_rook_square(CastlingRight cr) const {
275
inline Square Position::castling_rook_square(CastlingRight cr) const {
271
  return castlingRookSquare[cr];
276
  return castlingRookSquare[cr];
272
}
277
}
273
 
278
 
274
template<PieceType Pt>
279
template<PieceType Pt>
275
inline Bitboard Position::attacks_from(Square s) const {
280
inline Bitboard Position::attacks_from(Square s) const {
276
  assert(Pt != PAWN);
281
  assert(Pt != PAWN);
277
  return  Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
282
  return  Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
Line 280... Line 285...
280
}
285
}
281
 
286
 
282
template<>
287
template<>
283
inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
288
inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
284
  return PawnAttacks[c][s];
289
  return PawnAttacks[c][s];
285
}
290
}
286
 
291
 
287
inline Bitboard Position::attacks_from(PieceType pt, Square s) const {
292
inline Bitboard Position::attacks_from(PieceType pt, Square s) const {
288
  return attacks_bb(pt, s, byTypeBB[ALL_PIECES]);
293
  return attacks_bb(pt, s, byTypeBB[ALL_PIECES]);
289
}
294
}
290
 
295
 
291
inline Bitboard Position::attackers_to(Square s) const {
296
inline Bitboard Position::attackers_to(Square s) const {
292
  return attackers_to(s, byTypeBB[ALL_PIECES]);
297
  return attackers_to(s, byTypeBB[ALL_PIECES]);
293
}
298
}
294
 
299
 
295
inline Bitboard Position::checkers() const {
300
inline Bitboard Position::checkers() const {
296
  return st->checkersBB;
301
  return st->checkersBB;
297
}
302
}
298
 
303
 
299
inline Bitboard Position::discovered_check_candidates() const {
-
 
300
  return st->blockersForKing[~sideToMove] & pieces(sideToMove);
-
 
301
}
-
 
302
 
-
 
303
inline Bitboard Position::pinned_pieces(Color c) const {
304
inline Bitboard Position::blockers_for_king(Color c) const {
304
  return st->blockersForKing[c] & pieces(c);
305
  return st->blockersForKing[c];
305
}
306
}
306
 
307
 
307
inline Bitboard Position::check_squares(PieceType pt) const {
308
inline Bitboard Position::check_squares(PieceType pt) const {
308
  return st->checkSquares[pt];
309
  return st->checkSquares[pt];
309
}
310
}
Line 328... Line 329...
328
inline Key Position::material_key() const {
329
inline Key Position::material_key() const {
329
  return st->materialKey;
330
  return st->materialKey;
330
}
331
}
331
 
332
 
332
inline Score Position::psq_score() const {
333
inline Score Position::psq_score() const {
333
  return st->psq;
334
  return psq;
334
}
335
}
335
 
336
 
336
inline Value Position::non_pawn_material(Color c) const {
337
inline Value Position::non_pawn_material(Color c) const {
337
  return st->nonPawnMaterial[c];
338
  return st->nonPawnMaterial[c];
338
}
339
}
Line 385... Line 386...
385
  byTypeBB[type_of(pc)] |= s;
386
  byTypeBB[type_of(pc)] |= s;
386
  byColorBB[color_of(pc)] |= s;
387
  byColorBB[color_of(pc)] |= s;
387
  index[s] = pieceCount[pc]++;
388
  index[s] = pieceCount[pc]++;
388
  pieceList[pc][index[s]] = s;
389
  pieceList[pc][index[s]] = s;
389
  pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
390
  pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
-
 
391
  psq += PSQT::psq[pc][s];
390
}
392
}
391
 
393
 
392
inline void Position::remove_piece(Piece pc, Square s) {
394
inline void Position::remove_piece(Piece pc, Square s) {
393
 
395
 
394
  // WARNING: This is not a reversible operation. If we remove a piece in
396
  // WARNING: This is not a reversible operation. If we remove a piece in
Line 402... Line 404...
402
  Square lastSquare = pieceList[pc][--pieceCount[pc]];
404
  Square lastSquare = pieceList[pc][--pieceCount[pc]];
403
  index[lastSquare] = index[s];
405
  index[lastSquare] = index[s];
404
  pieceList[pc][index[lastSquare]] = lastSquare;
406
  pieceList[pc][index[lastSquare]] = lastSquare;
405
  pieceList[pc][pieceCount[pc]] = SQ_NONE;
407
  pieceList[pc][pieceCount[pc]] = SQ_NONE;
406
  pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
408
  pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
-
 
409
  psq -= PSQT::psq[pc][s];
407
}
410
}
408
 
411
 
409
inline void Position::move_piece(Piece pc, Square from, Square to) {
412
inline void Position::move_piece(Piece pc, Square from, Square to) {
410
 
413
 
411
  // index[from] is not updated and becomes stale. This works as long as index[]
414
  // index[from] is not updated and becomes stale. This works as long as index[]
412
  // is accessed just by known occupied squares.
415
  // is accessed just by known occupied squares.
413
  Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
416
  Bitboard fromTo = SquareBB[from] ^ SquareBB[to];
414
  byTypeBB[ALL_PIECES] ^= from_to_bb;
417
  byTypeBB[ALL_PIECES] ^= fromTo;
415
  byTypeBB[type_of(pc)] ^= from_to_bb;
418
  byTypeBB[type_of(pc)] ^= fromTo;
416
  byColorBB[color_of(pc)] ^= from_to_bb;
419
  byColorBB[color_of(pc)] ^= fromTo;
417
  board[from] = NO_PIECE;
420
  board[from] = NO_PIECE;
418
  board[to] = pc;
421
  board[to] = pc;
419
  index[to] = index[from];
422
  index[to] = index[from];
420
  pieceList[pc][index[to]] = to;
423
  pieceList[pc][index[to]] = to;
-
 
424
  psq += PSQT::psq[pc][to] - PSQT::psq[pc][from];
421
}
425
}
422
 
426
 
423
inline void Position::do_move(Move m, StateInfo& newSt) {
427
inline void Position::do_move(Move m, StateInfo& newSt) {
424
  do_move(m, newSt, gives_check(m));
428
  do_move(m, newSt, gives_check(m));
425
}
429
}