-void Position::do_promotion_move(Move m, UndoInfo &u) {
-
- Color us, them;
- Square from, to;
- PieceType capture, promotion;
-
- assert(is_ok());
- assert(move_is_ok(m));
- assert(move_promotion(m));
-
- us = side_to_move();
- them = opposite_color(us);
- from = move_from(m);
- to = move_to(m);
-
- assert(relative_rank(us, to) == RANK_8);
- assert(piece_on(from) == pawn_of_color(us));
- assert(color_of_piece_on(to) == them || square_is_empty(to));
-
- capture = type_of_piece_on(to);
-
- if (capture)
- {
- assert(capture != KING);
-
- // Remove captured piece
- clear_bit(&(byColorBB[them]), to);
- clear_bit(&(byTypeBB[capture]), to);
-
- // Update hash key
- key ^= zobrist[them][capture][to];
-
- // Update incremental scores
- mgValue -= mg_pst(them, capture, to);
- egValue -= eg_pst(them, capture, to);
-
- // Update material. Because our move is a promotion, we know that the
- // captured piece is not a pawn.
- assert(capture != PAWN);
- npMaterial[them] -= piece_value_midgame(capture);
-
- // Update material hash key
- materialKey ^= zobMaterial[them][capture][pieceCount[them][capture]];
-
- // Update piece count
- pieceCount[them][capture]--;
-
- // Update piece list
- pieceList[them][capture][index[to]] = pieceList[them][capture][pieceCount[them][capture]];
- index[pieceList[them][capture][index[to]]] = index[to];
-
- // Remember the captured piece, in order to be able to undo the move correctly
- u.capture = capture;
- }
-
- // Remove pawn
- clear_bit(&(byColorBB[us]), from);
- clear_bit(&(byTypeBB[PAWN]), from);
- clear_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares
- board[from] = EMPTY;
-
- // Insert promoted piece
- promotion = move_promotion(m);
- assert(promotion >= KNIGHT && promotion <= QUEEN);
- set_bit(&(byColorBB[us]), to);
- set_bit(&(byTypeBB[promotion]), to);
- set_bit(&(byTypeBB[0]), to); // HACK: byTypeBB[0] == occupied squares
- board[to] = piece_of_color_and_type(us, promotion);
-
- // Update hash key
- key ^= zobrist[us][PAWN][from] ^ zobrist[us][promotion][to];
-
- // Update pawn hash key
- pawnKey ^= zobrist[us][PAWN][from];
-
- // Update material key
- materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
- materialKey ^= zobMaterial[us][promotion][pieceCount[us][promotion]+1];
-
- // Update piece counts
- pieceCount[us][PAWN]--;
- pieceCount[us][promotion]++;
-
- // Update piece lists
- pieceList[us][PAWN][index[from]] = pieceList[us][PAWN][pieceCount[us][PAWN]];
- index[pieceList[us][PAWN][index[from]]] = index[from];
- pieceList[us][promotion][pieceCount[us][promotion] - 1] = to;
- index[to] = pieceCount[us][promotion] - 1;
-
- // Update incremental scores
- mgValue -= mg_pst(us, PAWN, from);
- mgValue += mg_pst(us, promotion, to);
- egValue -= eg_pst(us, PAWN, from);
- egValue += eg_pst(us, promotion, to);
-
- // Update material
- npMaterial[us] += piece_value_midgame(promotion);
-
- // Clear the en passant square
- if (epSquare != SQ_NONE)
- {
- key ^= zobEp[epSquare];
- epSquare = SQ_NONE;
- }
-
- // Update castle rights
- key ^= zobCastle[castleRights];
- castleRights &= castleRightsMask[to];
- key ^= zobCastle[castleRights];
-
- // Reset rule 50 counter
- rule50 = 0;
-
- // Update checkers BB
- checkersBB = attacks_to(king_square(them), us);
-}
-
-
-/// Position::do_ep_move() is a private method used to make an en passant
-/// capture. It is called from the main Position::do_move function. Because
-/// the captured piece is always a pawn, we don't need to pass an UndoInfo
-/// object in which to store the captured piece.
-
-void Position::do_ep_move(Move m) {
-
- Color us, them;
- Square from, to, capsq;
-
- assert(is_ok());
- assert(move_is_ok(m));
- assert(move_is_ep(m));
-
- us = side_to_move();
- them = opposite_color(us);
- from = move_from(m);
- to = move_to(m);
- capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
-
- assert(to == epSquare);
- assert(relative_rank(us, to) == RANK_6);
- assert(piece_on(to) == EMPTY);
- assert(piece_on(from) == pawn_of_color(us));
- assert(piece_on(capsq) == pawn_of_color(them));
-
- // Remove captured piece
- clear_bit(&(byColorBB[them]), capsq);
- clear_bit(&(byTypeBB[PAWN]), capsq);
- clear_bit(&(byTypeBB[0]), capsq); // HACK: byTypeBB[0] == occupied squares
- board[capsq] = EMPTY;
-
- // Remove moving piece from source square
- clear_bit(&(byColorBB[us]), from);
- clear_bit(&(byTypeBB[PAWN]), from);
- clear_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares
-
- // Put moving piece on destination square
- set_bit(&(byColorBB[us]), to);
- set_bit(&(byTypeBB[PAWN]), to);
- set_bit(&(byTypeBB[0]), to); // HACK: byTypeBB[0] == occupied squares
- board[to] = board[from];
- board[from] = EMPTY;
-
- // Update material hash key
- materialKey ^= zobMaterial[them][PAWN][pieceCount[them][PAWN]];
-
- // Update piece count
- pieceCount[them][PAWN]--;
-
- // Update piece list
- pieceList[us][PAWN][index[from]] = to;
- index[to] = index[from];
- pieceList[them][PAWN][index[capsq]] = pieceList[them][PAWN][pieceCount[them][PAWN]];
- index[pieceList[them][PAWN][index[capsq]]] = index[capsq];
-
- // Update hash key
- key ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
- key ^= zobrist[them][PAWN][capsq];
- key ^= zobEp[epSquare];
-
- // Update pawn hash key
- pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
- pawnKey ^= zobrist[them][PAWN][capsq];
-
- // Update incremental scores
- mgValue -= mg_pst(them, PAWN, capsq);
- mgValue -= mg_pst(us, PAWN, from);
- mgValue += mg_pst(us, PAWN, to);
- egValue -= eg_pst(them, PAWN, capsq);
- egValue -= eg_pst(us, PAWN, from);
- egValue += eg_pst(us, PAWN, to);
-
- // Reset en passant square
- epSquare = SQ_NONE;
-
- // Reset rule 50 counter
- rule50 = 0;
-
- // Update checkers BB
- checkersBB = attacks_to(king_square(them), us);
-}
-
-
-/// Position::undo_move() unmakes a move. When it returns, the position should
-/// be restored to exactly the same state as before the move was made. It is
-/// important that Position::undo_move is called with the same move and UndoInfo
-/// object as the earlier call to Position::do_move.
-
-void Position::undo_move(Move m, const UndoInfo &u) {