Key Position::zobSideToMove;
Key Position::zobExclusion;
-Score Position::PieceSquareTable[16][64];
+Score Position::pieceSquareTable[16][64];
// Material values arrays, indexed by Piece
const Value PieceValueMidgame[17] = {
// Various initialisations
chess960 = isChess960;
- find_checkers();
+ st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(opposite_color(sideToMove));
st->key = compute_key();
st->pawnKey = compute_pawn_key();
}
-/// Position::find_checkers() computes the checkersBB bitboard, which
-/// contains a nonzero bit for each checking piece (0, 1 or 2). It
-/// currently works by calling Position::attackers_to, which is probably
-/// inefficient. Consider rewriting this function to use the last move
-/// played, like in non-bitboard versions of Glaurung.
-
-void Position::find_checkers() {
-
- Color us = side_to_move();
- st->checkersBB = attackers_to(king_square(us)) & pieces(opposite_color(us));
-}
-
-
/// Position::pl_move_is_legal() tests whether a pseudo-legal move is legal
bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
// Move the piece
Bitboard move_bb = make_move_bb(from, to);
- do_move_bb(&(byColorBB[us]), move_bb);
- do_move_bb(&(byTypeBB[pt]), move_bb);
- do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
+ do_move_bb(&byColorBB[us], move_bb);
+ do_move_bb(&byTypeBB[pt], move_bb);
+ do_move_bb(&byTypeBB[0], move_bb); // HACK: byTypeBB[0] == occupied squares
board[to] = board[from];
board[from] = PIECE_NONE;
assert(promotion >= KNIGHT && promotion <= QUEEN);
// Insert promoted piece instead of pawn
- clear_bit(&(byTypeBB[PAWN]), to);
- set_bit(&(byTypeBB[promotion]), to);
+ clear_bit(&byTypeBB[PAWN], to);
+ set_bit(&byTypeBB[promotion], to);
board[to] = make_piece(us, promotion);
// Update piece counts
st->npMaterial[them] -= PieceValueMidgame[capture];
// Remove captured piece
- clear_bit(&(byColorBB[them]), capsq);
- clear_bit(&(byTypeBB[capture]), capsq);
- clear_bit(&(byTypeBB[0]), capsq);
+ clear_bit(&byColorBB[them], capsq);
+ clear_bit(&byTypeBB[capture], capsq);
+ clear_bit(&byTypeBB[0], capsq);
// Update hash key
key ^= zobrist[them][capture][capsq];
Color us = side_to_move();
Color them = opposite_color(us);
- // Reset capture field
- st->capturedType = PIECE_TYPE_NONE;
-
// Find source squares for king and rook
Square kfrom = move_from(m);
- Square rfrom = move_to(m); // HACK: See comment at beginning of function
+ Square rfrom = move_to(m);
Square kto, rto;
assert(piece_on(kfrom) == make_piece(us, KING));
{
kto = relative_square(us, SQ_G1);
rto = relative_square(us, SQ_F1);
- } else { // O-O-O
+ }
+ else // O-O-O
+ {
kto = relative_square(us, SQ_C1);
rto = relative_square(us, SQ_D1);
}
- // Remove pieces from source squares:
- clear_bit(&(byColorBB[us]), kfrom);
- clear_bit(&(byTypeBB[KING]), kfrom);
- clear_bit(&(byTypeBB[0]), kfrom); // HACK: byTypeBB[0] == occupied squares
- clear_bit(&(byColorBB[us]), rfrom);
- clear_bit(&(byTypeBB[ROOK]), rfrom);
- clear_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares
-
- // Put pieces on destination squares:
- set_bit(&(byColorBB[us]), kto);
- set_bit(&(byTypeBB[KING]), kto);
- set_bit(&(byTypeBB[0]), kto); // HACK: byTypeBB[0] == occupied squares
- set_bit(&(byColorBB[us]), rto);
- set_bit(&(byTypeBB[ROOK]), rto);
- set_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
-
- // Update board array
+ // Remove pieces from source squares
+ clear_bit(&byColorBB[us], kfrom);
+ clear_bit(&byTypeBB[KING], kfrom);
+ clear_bit(&byTypeBB[0], kfrom);
+ clear_bit(&byColorBB[us], rfrom);
+ clear_bit(&byTypeBB[ROOK], rfrom);
+ clear_bit(&byTypeBB[0], rfrom);
+
+ // Put pieces on destination squares
+ set_bit(&byColorBB[us], kto);
+ set_bit(&byTypeBB[KING], kto);
+ set_bit(&byTypeBB[0], kto);
+ set_bit(&byColorBB[us], rto);
+ set_bit(&byTypeBB[ROOK], rto);
+ set_bit(&byTypeBB[0], rto);
+
+ // Update board
Piece king = make_piece(us, KING);
Piece rook = make_piece(us, ROOK);
board[kfrom] = board[rfrom] = PIECE_NONE;
// Update piece lists
pieceList[us][KING][index[kfrom]] = kto;
pieceList[us][ROOK][index[rfrom]] = rto;
- int tmp = index[rfrom]; // In Chess960 could be rto == kfrom
+ int tmp = index[rfrom]; // In Chess960 could be kto == rfrom
index[kto] = index[kfrom];
index[rto] = tmp;
+ // Reset capture field
+ st->capturedType = PIECE_TYPE_NONE;
+
// Update incremental scores
st->value += pst_delta(king, kfrom, kto);
st->value += pst_delta(rook, rfrom, rto);
assert(piece_on(to) == make_piece(us, promotion));
// Replace promoted piece with a pawn
- clear_bit(&(byTypeBB[promotion]), to);
- set_bit(&(byTypeBB[PAWN]), to);
+ clear_bit(&byTypeBB[promotion], to);
+ set_bit(&byTypeBB[PAWN], to);
// Update piece counts
pieceCount[us][promotion]--;
// Put the piece back at the source square
Bitboard move_bb = make_move_bb(to, from);
- do_move_bb(&(byColorBB[us]), move_bb);
- do_move_bb(&(byTypeBB[pt]), move_bb);
- do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
+ do_move_bb(&byColorBB[us], move_bb);
+ do_move_bb(&byTypeBB[pt], move_bb);
+ do_move_bb(&byTypeBB[0], move_bb); // HACK: byTypeBB[0] == occupied squares
board[from] = make_piece(us, pt);
board[to] = PIECE_NONE;
assert(!ep || square_is_empty(capsq));
// Restore the captured piece
- set_bit(&(byColorBB[them]), capsq);
- set_bit(&(byTypeBB[st->capturedType]), capsq);
- set_bit(&(byTypeBB[0]), capsq);
+ set_bit(&byColorBB[them], capsq);
+ set_bit(&byTypeBB[st->capturedType], capsq);
+ set_bit(&byTypeBB[0], capsq);
board[capsq] = make_piece(them, st->capturedType);
assert(move_is_castle(m));
// When we have arrived here, some work has already been done by
- // Position::undo_move. In particular, the side to move has been switched,
+ // Position::undo_move. In particular, the side to move has been switched,
// so the code below is correct.
Color us = side_to_move();
// Find source squares for king and rook
Square kfrom = move_from(m);
- Square rfrom = move_to(m); // HACK: See comment at beginning of function
+ Square rfrom = move_to(m);
Square kto, rto;
// Find destination squares for king and rook
{
kto = relative_square(us, SQ_G1);
rto = relative_square(us, SQ_F1);
- } else { // O-O-O
+ }
+ else // O-O-O
+ {
kto = relative_square(us, SQ_C1);
rto = relative_square(us, SQ_D1);
}
assert(piece_on(kto) == make_piece(us, KING));
assert(piece_on(rto) == make_piece(us, ROOK));
- // Remove pieces from destination squares:
- clear_bit(&(byColorBB[us]), kto);
- clear_bit(&(byTypeBB[KING]), kto);
- clear_bit(&(byTypeBB[0]), kto); // HACK: byTypeBB[0] == occupied squares
- clear_bit(&(byColorBB[us]), rto);
- clear_bit(&(byTypeBB[ROOK]), rto);
- clear_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
-
- // Put pieces on source squares:
- set_bit(&(byColorBB[us]), kfrom);
- set_bit(&(byTypeBB[KING]), kfrom);
- set_bit(&(byTypeBB[0]), kfrom); // HACK: byTypeBB[0] == occupied squares
- set_bit(&(byColorBB[us]), rfrom);
- set_bit(&(byTypeBB[ROOK]), rfrom);
- set_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares
+ // Remove pieces from destination squares
+ clear_bit(&byColorBB[us], kto);
+ clear_bit(&byTypeBB[KING], kto);
+ clear_bit(&byTypeBB[0], kto);
+ clear_bit(&byColorBB[us], rto);
+ clear_bit(&byTypeBB[ROOK], rto);
+ clear_bit(&byTypeBB[0], rto);
+
+ // Put pieces on source squares
+ set_bit(&byColorBB[us], kfrom);
+ set_bit(&byTypeBB[KING], kfrom);
+ set_bit(&byTypeBB[0], kfrom);
+ set_bit(&byColorBB[us], rfrom);
+ set_bit(&byTypeBB[ROOK], rfrom);
+ set_bit(&byTypeBB[0], rfrom);
// Update board
- board[rto] = board[kto] = PIECE_NONE;
- board[rfrom] = make_piece(us, ROOK);
- board[kfrom] = make_piece(us, KING);
+ Piece king = make_piece(us, KING);
+ Piece rook = make_piece(us, ROOK);
+ board[kto] = board[rto] = PIECE_NONE;
+ board[kfrom] = king;
+ board[rfrom] = rook;
// Update piece lists
pieceList[us][KING][index[kto]] = kfrom;
for (Square s = SQ_A1; s <= SQ_H8; s++)
for (Piece p = WP; p <= WK; p++)
- PieceSquareTable[p][s] = make_score(MgPST[p][s], EgPST[p][s]);
+ pieceSquareTable[p][s] = make_score(MgPST[p][s], EgPST[p][s]);
for (Square s = SQ_A1; s <= SQ_H8; s++)
for (Piece p = BP; p <= BK; p++)
- PieceSquareTable[p][s] = -PieceSquareTable[p-8][flip_square(s)];
+ pieceSquareTable[p][s] = -pieceSquareTable[p-8][flip_square(s)];
}
st->epSquare = flip_square(pos.st->epSquare);
// Checkers
- find_checkers();
+ st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(opposite_color(sideToMove));
// Hash keys
st->key = compute_key();