1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 08:43:09 +00:00

Unify do_promotion_move()

Integrate do_promotion_move() in do_move() this reduces line count
and code readibility.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-08-16 14:49:15 +01:00
parent cb506d3b16
commit ec14fb1b33
2 changed files with 47 additions and 99 deletions

View file

@ -725,8 +725,6 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
if (move_is_castle(m)) if (move_is_castle(m))
do_castle_move(m); do_castle_move(m);
else if (move_is_promotion(m))
do_promotion_move(m);
else else
{ {
Color us = side_to_move(); Color us = side_to_move();
@ -734,14 +732,16 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
Square from = move_from(m); Square from = move_from(m);
Square to = move_to(m); Square to = move_to(m);
bool ep = move_is_ep(m); bool ep = move_is_ep(m);
bool pm = move_is_promotion(m);
assert(color_of_piece_on(from) == us);
assert(color_of_piece_on(to) == them || piece_on(to) == EMPTY);
assert(!ep || piece_on(from) == piece_of_color_and_type(us, PAWN));
Piece piece = piece_on(from); Piece piece = piece_on(from);
PieceType pt = type_of_piece(piece); PieceType pt = type_of_piece(piece);
assert(color_of_piece_on(from) == us);
assert(color_of_piece_on(to) == them || square_is_empty(to));
assert(!(ep || pm) || piece == piece_of_color_and_type(us, PAWN));
assert(!pm || relative_rank(us, to) == RANK_8);
st->capture = type_of_piece_on(to); st->capture = type_of_piece_on(to);
if (st->capture || ep) if (st->capture || ep)
@ -811,8 +811,47 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
pieceList[us][pt][index[from]] = to; pieceList[us][pt][index[from]] = to;
index[to] = index[from]; index[to] = index[from];
if (pm)
{
PieceType promotion = move_promotion_piece(m);
assert(promotion >= KNIGHT && promotion <= QUEEN);
// Insert promoted piece instead of pawn
clear_bit(&(byTypeBB[PAWN]), to);
set_bit(&(byTypeBB[promotion]), to);
board[to] = piece_of_color_and_type(us, promotion);
// Partially revert hash keys update
st->key ^= zobrist[us][PAWN][to] ^ zobrist[us][promotion][to];
st->pawnKey ^= zobrist[us][PAWN][to];
// Update material key
st->materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
st->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;
// Partially revert and update incremental scores
st->mgValue -= pst<MidGame>(us, PAWN, to);
st->mgValue += pst<MidGame>(us, promotion, to);
st->egValue -= pst<EndGame>(us, PAWN, to);
st->egValue += pst<EndGame>(us, promotion, to);
// Update material
st->npMaterial[us] += piece_value_midgame(promotion);
}
// Update checkers bitboard, piece must be already moved // Update checkers bitboard, piece must be already moved
if (ep) if (ep || pm)
st->checkersBB = attacks_to(king_square(them), us); st->checkersBB = attacks_to(king_square(them), us);
else else
{ {
@ -857,7 +896,7 @@ void Position::do_capture_move(PieceType capture, Color them, Square to, bool ep
capsq = (them == BLACK)? (to - DELTA_N) : (to - DELTA_S); capsq = (them == BLACK)? (to - DELTA_N) : (to - DELTA_S);
assert(to == st->epSquare); assert(to == st->epSquare);
assert(relative_rank(us, to) == RANK_6); assert(relative_rank(opposite_color(them), to) == RANK_6);
assert(piece_on(to) == EMPTY); assert(piece_on(to) == EMPTY);
//assert(piece_on(from) == piece_of_color_and_type(us, PAWN)); //assert(piece_on(from) == piece_of_color_and_type(us, PAWN));
assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN)); assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN));
@ -990,96 +1029,6 @@ void Position::do_castle_move(Move m) {
} }
/// 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_is_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, false);
// 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_piece(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
st->key ^= zobrist[us][PAWN][from] ^ zobrist[us][promotion][to];
// Update pawn hash key
st->pawnKey ^= zobrist[us][PAWN][from];
// Update material key
st->materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
st->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
st->mgValue -= pst<MidGame>(us, PAWN, from);
st->mgValue += pst<MidGame>(us, promotion, to);
st->egValue -= pst<EndGame>(us, PAWN, from);
st->egValue += pst<EndGame>(us, promotion, to);
// Update material
st->npMaterial[us] += piece_value_midgame(promotion);
// Clear the en passant square
if (st->epSquare != SQ_NONE)
{
st->key ^= zobEp[st->epSquare];
st->epSquare = SQ_NONE;
}
// Update castle rights
st->key ^= zobCastle[st->castleRights];
st->castleRights &= castleRightsMask[to];
st->key ^= zobCastle[st->castleRights];
// Reset rule 50 counter
st->rule50 = 0;
// Update checkers BB
st->checkersBB = attacks_to(king_square(them), us);
}
/// Position::undo_move() unmakes a move. When it returns, the position should /// 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. /// be restored to exactly the same state as before the move was made.

View file

@ -312,7 +312,6 @@ private:
// Helper functions for doing and undoing moves // Helper functions for doing and undoing moves
void do_capture_move(PieceType capture, Color them, Square to, bool ep); void do_capture_move(PieceType capture, Color them, Square to, bool ep);
void do_castle_move(Move m); void do_castle_move(Move m);
void do_promotion_move(Move m);
void undo_castle_move(Move m); void undo_castle_move(Move m);
void undo_promotion_move(Move m); void undo_promotion_move(Move m);
void undo_ep_move(Move m); void undo_ep_move(Move m);