- if (move_is_castle(m))
- do_castle_move(m);
- else if (move_promotion(m))
- do_promotion_move(m);
- else if (move_is_ep(m))
- do_ep_move(m);
- else
- {
- Color us = side_to_move();
- Color them = opposite_color(us);
- Square from = move_from(m);
- Square to = move_to(m);
-
- assert(color_of_piece_on(from) == us);
- assert(color_of_piece_on(to) == them || piece_on(to) == EMPTY);
-
- PieceType piece = type_of_piece_on(from);
-
- st->capture = type_of_piece_on(to);
-
- if (st->capture)
- do_capture_move(st->capture, them, to);
-
- // Move the piece
- clear_bit(&(byColorBB[us]), from);
- clear_bit(&(byTypeBB[piece]), from);
- clear_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares
- set_bit(&(byColorBB[us]), to);
- set_bit(&(byTypeBB[piece]), to);
- set_bit(&(byTypeBB[0]), to); // HACK: byTypeBB[0] == occupied squares
- board[to] = board[from];
- board[from] = EMPTY;
-
- // Update hash key
- st->key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
-
- // Update incremental scores
- st->mgValue -= pst<MidGame>(us, piece, from);
- st->mgValue += pst<MidGame>(us, piece, to);
- st->egValue -= pst<EndGame>(us, piece, from);
- st->egValue += pst<EndGame>(us, piece, to);
-
- // If the moving piece was a king, update the king square
- if (piece == KING)
- kingSquare[us] = to;
-
- // Reset en passant square
- if (st->epSquare != SQ_NONE)
- {
- st->key ^= zobEp[st->epSquare];
- st->epSquare = SQ_NONE;
- }
-
- // If the moving piece was a pawn do some special extra work
- if (piece == PAWN)
- {
- // Reset rule 50 draw counter
- st->rule50 = 0;
-
- // Update pawn hash key
- st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
-
- // Set en passant square, only if moved pawn can be captured
- if (abs(int(to) - int(from)) == 16)
- {
- if ( (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
- || (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
- {
- st->epSquare = Square((int(from) + int(to)) / 2);
- st->key ^= zobEp[st->epSquare];
- }
- }
- }
-
- // Update piece lists
- pieceList[us][piece][index[from]] = to;
- index[to] = index[from];
-
- // Update castle rights
- st->key ^= zobCastle[st->castleRights];
- st->castleRights &= castleRightsMask[from];
- st->castleRights &= castleRightsMask[to];
- st->key ^= zobCastle[st->castleRights];
-
- // Update checkers bitboard, piece must be already moved
- st->checkersBB = EmptyBoardBB;
- Square ksq = king_square(them);
- switch (piece)
- {
- case PAWN: update_checkers<PAWN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case KNIGHT: update_checkers<KNIGHT>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case BISHOP: update_checkers<BISHOP>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case ROOK: update_checkers<ROOK>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case QUEEN: update_checkers<QUEEN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case KING: update_checkers<KING>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- default: assert(false); break;
- }
- }
-
- // Finish
- st->key ^= zobSideToMove;
- sideToMove = opposite_color(sideToMove);
- gamePly++;
-
- st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame;
- st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame;
-
- assert(is_ok());
-}
-
-
-/// Position::do_capture_move() is a private method used to update captured
-/// piece info. It is called from the main Position::do_move function.
-
-void Position::do_capture_move(PieceType capture, Color them, Square to) {
-
- assert(capture != KING);
-
- // Remove captured piece
- clear_bit(&(byColorBB[them]), to);
- clear_bit(&(byTypeBB[capture]), to);
-
- // Update hash key
- st->key ^= zobrist[them][capture][to];
-
- // If the captured piece was a pawn, update pawn hash key
- if (capture == PAWN)
- st->pawnKey ^= zobrist[them][PAWN][to];
-
- // Update incremental scores
- st->mgValue -= pst<MidGame>(them, capture, to);
- st->egValue -= pst<EndGame>(them, capture, to);
-
- // Update material
- if (capture != PAWN)
- npMaterial[them] -= piece_value_midgame(capture);
-
- // Update material hash key
- st->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];
-
- // Reset rule 50 counter
- st->rule50 = 0;
-}
-
-
-/// Position::do_castle_move() is a private method used to make a castling
-/// move. It is called from the main Position::do_move function. Note that
-/// castling moves are encoded as "king captures friendly rook" moves, for
-/// instance white short castling in a non-Chess960 game is encoded as e1h1.
-
-void Position::do_castle_move(Move m) {
-
- assert(is_ok());
- assert(move_is_ok(m));
- assert(move_is_castle(m));
-
- Color us = side_to_move();
- Color them = opposite_color(us);
-
- // 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 kto, rto;
-
- assert(piece_on(kfrom) == piece_of_color_and_type(us, KING));
- assert(piece_on(rfrom) == piece_of_color_and_type(us, ROOK));
-
- // Find destination squares for king and rook
- if (rfrom > kfrom) // O-O
- {
- kto = relative_square(us, SQ_G1);
- rto = relative_square(us, SQ_F1);
- } 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
- board[kfrom] = board[rfrom] = EMPTY;
- board[kto] = piece_of_color_and_type(us, KING);
- board[rto] = piece_of_color_and_type(us, ROOK);
-
- // Update king square
- kingSquare[us] = kto;
-
- // Update piece lists
- pieceList[us][KING][index[kfrom]] = kto;
- pieceList[us][ROOK][index[rfrom]] = rto;
- int tmp = index[rfrom];
- index[kto] = index[kfrom];
- index[rto] = tmp;
-
- // Update incremental scores
- st->mgValue -= pst<MidGame>(us, KING, kfrom);
- st->mgValue += pst<MidGame>(us, KING, kto);
- st->egValue -= pst<EndGame>(us, KING, kfrom);
- st->egValue += pst<EndGame>(us, KING, kto);
- st->mgValue -= pst<MidGame>(us, ROOK, rfrom);
- st->mgValue += pst<MidGame>(us, ROOK, rto);
- st->egValue -= pst<EndGame>(us, ROOK, rfrom);
- st->egValue += pst<EndGame>(us, ROOK, rto);
-
- // Update hash key
- st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
- st->key ^= zobrist[us][ROOK][rfrom] ^ zobrist[us][ROOK][rto];
-
- // Clear en passant square
- if (st->epSquare != SQ_NONE)
- {
- st->key ^= zobEp[st->epSquare];
- st->epSquare = SQ_NONE;
- }
-
- // Update castling rights
- st->key ^= zobCastle[st->castleRights];
- st->castleRights &= castleRightsMask[kfrom];
- st->key ^= zobCastle[st->castleRights];
-
- // Reset rule 50 counter
- st->rule50 = 0;
-
- // Update checkers BB
- st->checkersBB = attacks_to(king_square(them), us);
-}
-
-
-/// Position::do_promotion_move() is a private method used to make a promotion
-/// move. It is called from the main Position::do_move function.
-
-void Position::do_promotion_move(Move m) {
-
- Color us, them;
- Square from, to;
- PieceType 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) == piece_of_color_and_type(us, PAWN));
- assert(color_of_piece_on(to) == them || square_is_empty(to));
-
- st->capture = type_of_piece_on(to);
-
- if (st->capture)
- do_capture_move(st->capture, them, to);
-
- // 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;