1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 09:13:08 +00:00

Assorted clean up in endgames

No functional change.
This commit is contained in:
Marco Costalba 2013-09-01 06:58:58 -07:00
parent 3e4dcaa06e
commit 5e8bc6ac2a

View file

@ -29,9 +29,9 @@ using std::string;
namespace { namespace {
// Table used to drive the defending king towards the edge of the board // Table used to drive the king towards the edge of the board
// in KX vs K and KQ vs KR endgames. // in KX vs K and KQ vs KR endgames.
const int MateTable[SQUARE_NB] = { const int PushToEdges[SQUARE_NB] = {
100, 90, 80, 70, 70, 80, 90, 100, 100, 90, 80, 70, 70, 80, 90, 100,
90, 70, 60, 50, 50, 60, 70, 90, 90, 70, 60, 50, 50, 60, 70, 90,
80, 60, 40, 30, 30, 40, 60, 80, 80, 60, 40, 30, 30, 40, 60, 80,
@ -42,9 +42,9 @@ namespace {
100, 90, 80, 70, 70, 80, 90, 100, 100, 90, 80, 70, 70, 80, 90, 100,
}; };
// Table used to drive the defending king towards a corner square of the // Table used to drive the king towards a corner square of the
// right color in KBN vs K endgames. // right color in KBN vs K endgames.
const int KBNKMateTable[SQUARE_NB] = { const int PushToCorners[SQUARE_NB] = {
200, 190, 180, 170, 160, 150, 140, 130, 200, 190, 180, 170, 160, 150, 140, 130,
190, 180, 170, 160, 150, 140, 130, 140, 190, 180, 170, 160, 150, 140, 130, 140,
180, 170, 155, 140, 140, 125, 140, 150, 180, 170, 155, 140, 140, 125, 140, 150,
@ -55,9 +55,9 @@ namespace {
130, 140, 150, 160, 170, 180, 190, 200 130, 140, 150, 160, 170, 180, 190, 200
}; };
// The attacking side is given a descending bonus based on distance between // Tables used to drive a piece towards or away from another piece
// the two kings in basic endgames. const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
const int DistanceBonus[8] = { 0, 0, 100, 80, 60, 40, 20, 10 }; const int PushAway [8] = { 0, 10, 14, 20, 30, 42, 58, 80 };
// Get the material key of a Position out of the given endgame key code // Get the material key of a Position out of the given endgame key code
// like "KBPKN". The trick here is to first forge an ad-hoc fen string // like "KBPKN". The trick here is to first forge an ad-hoc fen string
@ -142,8 +142,8 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
Value result = pos.non_pawn_material(strongerSide) Value result = pos.non_pawn_material(strongerSide)
+ pos.count<PAWN>(strongerSide) * PawnValueEg + pos.count<PAWN>(strongerSide) * PawnValueEg
+ MateTable[loserKSq] + PushToEdges[loserKSq]
+ DistanceBonus[square_distance(winnerKSq, loserKSq)]; + PushClose[square_distance(winnerKSq, loserKSq)];
if ( pos.count<QUEEN>(strongerSide) if ( pos.count<QUEEN>(strongerSide)
|| pos.count<ROOK>(strongerSide) || pos.count<ROOK>(strongerSide)
@ -180,8 +180,8 @@ Value Endgame<KBNK>::operator()(const Position& pos) const {
} }
Value result = VALUE_KNOWN_WIN Value result = VALUE_KNOWN_WIN
+ DistanceBonus[square_distance(winnerKSq, loserKSq)] + PushClose[square_distance(winnerKSq, loserKSq)]
+ KBNKMateTable[loserKSq]; + PushToCorners[loserKSq];
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -196,35 +196,30 @@ Value Endgame<KPK>::operator()(const Position& pos) const {
assert(pos.count<PAWN>(strongerSide) == 1); assert(pos.count<PAWN>(strongerSide) == 1);
assert(pos.count<PAWN>(weakerSide ) == 0); assert(pos.count<PAWN>(weakerSide ) == 0);
Square wksq, bksq, wpsq; Square wksq = pos.king_square(strongerSide);
Color us; Square bksq = pos.king_square(weakerSide);
Square psq = pos.list<PAWN>(strongerSide)[0];
Color us = pos.side_to_move();
if (strongerSide == WHITE) if (strongerSide == BLACK)
{ {
wksq = pos.king_square(WHITE); wksq = ~wksq;
bksq = pos.king_square(BLACK); bksq = ~bksq;
wpsq = pos.list<PAWN>(WHITE)[0]; psq = ~psq;
us = pos.side_to_move(); us = ~us;
}
else
{
wksq = ~pos.king_square(BLACK);
bksq = ~pos.king_square(WHITE);
wpsq = ~pos.list<PAWN>(BLACK)[0];
us = ~pos.side_to_move();
} }
if (file_of(wpsq) >= FILE_E) if (file_of(psq) >= FILE_E)
{ {
wksq = mirror(wksq); wksq = mirror(wksq);
bksq = mirror(bksq); bksq = mirror(bksq);
wpsq = mirror(wpsq); psq = mirror(psq);
} }
if (!Bitbases::probe_kpk(wksq, wpsq, bksq, us)) if (!Bitbases::probe_kpk(wksq, psq, bksq, us))
return VALUE_DRAW; return VALUE_DRAW;
Value result = VALUE_KNOWN_WIN + PawnValueEg + Value(rank_of(wpsq)); Value result = VALUE_KNOWN_WIN + PawnValueEg + Value(rank_of(psq));
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -242,48 +237,45 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
assert(pos.count<PAWN>(strongerSide) == 0); assert(pos.count<PAWN>(strongerSide) == 0);
assert(pos.count<PAWN>(weakerSide ) == 1); assert(pos.count<PAWN>(weakerSide ) == 1);
Square wksq, wrsq, bksq, bpsq; Square wksq = pos.king_square(strongerSide);
int tempo = (pos.side_to_move() == strongerSide); Square bksq = pos.king_square(weakerSide);
Square rsq = pos.list<ROOK>(strongerSide)[0];
wksq = pos.king_square(strongerSide); Square psq = pos.list<PAWN>(weakerSide)[0];
bksq = pos.king_square(weakerSide);
wrsq = pos.list<ROOK>(strongerSide)[0];
bpsq = pos.list<PAWN>(weakerSide)[0];
if (strongerSide == BLACK) if (strongerSide == BLACK)
{ {
wksq = ~wksq; wksq = ~wksq;
wrsq = ~wrsq;
bksq = ~bksq; bksq = ~bksq;
bpsq = ~bpsq; rsq = ~rsq;
psq = ~psq;
} }
Square queeningSq = file_of(bpsq) | RANK_1; Square queeningSq = file_of(psq) | RANK_1;
Value result; Value result;
// If the stronger side's king is in front of the pawn, it's a win // If the stronger side's king is in front of the pawn, it's a win
if (wksq < bpsq && file_of(wksq) == file_of(bpsq)) if (wksq < psq && file_of(wksq) == file_of(psq))
result = RookValueEg - Value(square_distance(wksq, bpsq)); result = RookValueEg - Value(square_distance(wksq, psq));
// If the weaker side's king is too far from the pawn and the rook, // If the weaker side's king is too far from the pawn and the rook,
// it's a win // it's a win.
else if ( square_distance(bksq, bpsq) - (tempo ^ 1) >= 3 else if ( square_distance(bksq, psq) >= 3 + (pos.side_to_move() == weakerSide)
&& square_distance(bksq, wrsq) >= 3) && square_distance(bksq, rsq) >= 3)
result = RookValueEg - Value(square_distance(wksq, bpsq)); result = RookValueEg - Value(square_distance(wksq, psq));
// If the pawn is far advanced and supported by the defending king, // If the pawn is far advanced and supported by the defending king,
// the position is drawish // the position is drawish
else if ( rank_of(bksq) <= RANK_3 else if ( rank_of(bksq) <= RANK_3
&& square_distance(bksq, bpsq) == 1 && square_distance(bksq, psq) == 1
&& rank_of(wksq) >= RANK_4 && rank_of(wksq) >= RANK_4
&& square_distance(wksq, bpsq) - tempo > 2) && square_distance(wksq, psq) > 2 + (pos.side_to_move() == strongerSide))
result = Value(80 - square_distance(wksq, bpsq) * 8); result = Value(80 - square_distance(wksq, psq) * 8);
else else
result = Value(200) result = Value(200)
- Value(square_distance(wksq, bpsq + DELTA_S) * 8) - Value(square_distance(wksq, psq + DELTA_S) * 8)
+ Value(square_distance(bksq, bpsq + DELTA_S) * 8) + Value(square_distance(bksq, psq + DELTA_S) * 8)
+ Value(square_distance(bpsq, queeningSq) * 8); + Value(square_distance(psq, queeningSq) * 8);
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -300,7 +292,7 @@ Value Endgame<KRKB>::operator()(const Position& pos) const {
assert(pos.count< PAWN>(weakerSide ) == 0); assert(pos.count< PAWN>(weakerSide ) == 0);
assert(pos.count< PAWN>(strongerSide) == 0); assert(pos.count< PAWN>(strongerSide) == 0);
Value result = Value(MateTable[pos.king_square(weakerSide)]); Value result = Value(PushToEdges[pos.king_square(weakerSide)]);
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -316,11 +308,9 @@ Value Endgame<KRKN>::operator()(const Position& pos) const {
assert(pos.count< PAWN>(weakerSide ) == 0); assert(pos.count< PAWN>(weakerSide ) == 0);
assert(pos.count< PAWN>(strongerSide) == 0); assert(pos.count< PAWN>(strongerSide) == 0);
const int penalty[8] = { 0, 10, 14, 20, 30, 42, 58, 80 };
Square bksq = pos.king_square(weakerSide); Square bksq = pos.king_square(weakerSide);
Square bnsq = pos.list<KNIGHT>(weakerSide)[0]; Square bnsq = pos.list<KNIGHT>(weakerSide)[0];
Value result = Value(MateTable[bksq] + penalty[square_distance(bksq, bnsq)]); Value result = Value(PushToEdges[bksq] + PushAway[square_distance(bksq, bnsq)]);
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -340,18 +330,13 @@ Value Endgame<KQKP>::operator()(const Position& pos) const {
Square loserKSq = pos.king_square(weakerSide); Square loserKSq = pos.king_square(weakerSide);
Square pawnSq = pos.list<PAWN>(weakerSide)[0]; Square pawnSq = pos.list<PAWN>(weakerSide)[0];
Value result = QueenValueEg Value result = Value(PushClose[square_distance(winnerKSq, loserKSq)]);
- PawnValueEg
+ DistanceBonus[square_distance(winnerKSq, loserKSq)];
if ( square_distance(loserKSq, pawnSq) == 1 if ( relative_rank(weakerSide, pawnSq) != RANK_7
&& relative_rank(weakerSide, pawnSq) == RANK_7) || square_distance(loserKSq, pawnSq) != 1
{ || !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq))
File f = file_of(pawnSq); result += QueenValueEg - PawnValueEg;
if (f == FILE_A || f == FILE_C || f == FILE_F || f == FILE_H)
result = Value(DistanceBonus[square_distance(winnerKSq, loserKSq)]);
}
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -374,8 +359,8 @@ Value Endgame<KQKR>::operator()(const Position& pos) const {
Value result = QueenValueEg Value result = QueenValueEg
- RookValueEg - RookValueEg
+ MateTable[loserKSq] + PushToEdges[loserKSq]
+ DistanceBonus[square_distance(winnerKSq, loserKSq)]; + PushClose[square_distance(winnerKSq, loserKSq)];
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -389,19 +374,14 @@ Value Endgame<KBBKN>::operator()(const Position& pos) const {
assert(pos.count<KNIGHT>(weakerSide ) == 1); assert(pos.count<KNIGHT>(weakerSide ) == 1);
assert(!pos.pieces(PAWN)); assert(!pos.pieces(PAWN));
Value result = BishopValueEg;
Square wksq = pos.king_square(strongerSide); Square wksq = pos.king_square(strongerSide);
Square bksq = pos.king_square(weakerSide); Square bksq = pos.king_square(weakerSide);
Square nsq = pos.list<KNIGHT>(weakerSide)[0]; Square nsq = pos.list<KNIGHT>(weakerSide)[0];
// Bonus for attacking king close to defending king Value result = BishopValueEg
result += Value(DistanceBonus[square_distance(wksq, bksq)]); + PushClose[square_distance(wksq, bksq)]
+ square_distance(bksq, nsq) * 32
// Bonus for driving the defending king and knight apart + (8 - popcount<Max15>(pos.attacks_from<KNIGHT>(nsq))) * 8;
result += Value(square_distance(bksq, nsq) * 32);
// Bonus for restricting the knight's mobility
result += Value((8 - popcount<Max15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@ -490,16 +470,16 @@ ScaleFactor Endgame<KQKRPs>::operator()(const Position& pos) const {
assert(pos.count< PAWN>(weakerSide ) >= 1); assert(pos.count< PAWN>(weakerSide ) >= 1);
Square kingSq = pos.king_square(weakerSide); Square kingSq = pos.king_square(weakerSide);
Square rsq = pos.list<ROOK>(weakerSide)[0];
if ( relative_rank(weakerSide, kingSq) <= RANK_2 if ( relative_rank(weakerSide, kingSq) <= RANK_2
&& relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4 && relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4
&& (pos.pieces(weakerSide, ROOK) & rank_bb(relative_rank(weakerSide, RANK_3))) && (pos.pieces(weakerSide, ROOK) & rank_bb(relative_rank(weakerSide, RANK_3)))
&& (pos.pieces(weakerSide, PAWN) & rank_bb(relative_rank(weakerSide, RANK_2))) && (pos.pieces(weakerSide, PAWN) & rank_bb(relative_rank(weakerSide, RANK_2)))
&& (pos.attacks_from<KING>(kingSq) & pos.pieces(weakerSide, PAWN))) && (pos.attacks_from<KING>(kingSq) & pos.pieces(weakerSide, PAWN))
{ && (pos.attacks_from<PAWN>(rsq, strongerSide) & pos.pieces(weakerSide, PAWN)))
Square rsq = pos.list<ROOK>(weakerSide)[0];
if (pos.attacks_from<PAWN>(rsq, strongerSide) & pos.pieces(weakerSide, PAWN))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
}
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
} }
@ -534,6 +514,7 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
bksq = ~bksq; bksq = ~bksq;
brsq = ~brsq; brsq = ~brsq;
} }
if (file_of(wpsq) > FILE_D) if (file_of(wpsq) > FILE_D)
{ {
wksq = mirror(wksq); wksq = mirror(wksq);
@ -917,31 +898,30 @@ ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
Square wksq = pos.king_square(strongerSide); Square wksq = pos.king_square(strongerSide);
Square bksq = pos.king_square(weakerSide); Square bksq = pos.king_square(weakerSide);
Square wpsq = pos.list<PAWN>(strongerSide)[0]; Square psq = pos.list<PAWN>(strongerSide)[0];
Color us = pos.side_to_move(); Color us = pos.side_to_move();
if (strongerSide == BLACK) if (strongerSide == BLACK)
{ {
wksq = ~wksq; wksq = ~wksq;
bksq = ~bksq; bksq = ~bksq;
wpsq = ~wpsq; psq = ~psq;
us = ~us; us = ~us;
} }
if (file_of(wpsq) >= FILE_E) if (file_of(psq) >= FILE_E)
{ {
wksq = mirror(wksq); wksq = mirror(wksq);
bksq = mirror(bksq); bksq = mirror(bksq);
wpsq = mirror(wpsq); psq = mirror(psq);
} }
// If the pawn has advanced to the fifth rank or further, and is not a // If the pawn has advanced to the fifth rank or further, and is not a
// rook pawn, it's too dangerous to assume that it's at least a draw. // rook pawn, it's too dangerous to assume that it's at least a draw.
if ( rank_of(wpsq) >= RANK_5 if (rank_of(psq) >= RANK_5 && file_of(psq) != FILE_A)
&& file_of(wpsq) != FILE_A)
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
// Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw, // Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw,
// it's probably at least a draw even with the pawn. // it's probably at least a draw even with the pawn.
return Bitbases::probe_kpk(wksq, wpsq, bksq, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW; return Bitbases::probe_kpk(wksq, psq, bksq, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
} }