From cee6336515d5772370d061aa54cf3ec9f9a0d3c2 Mon Sep 17 00:00:00 2001 From: Gary Linscott Date: Sat, 27 Oct 2012 22:27:19 -0400 Subject: [PATCH] Detect drawish KQKP endings Based off of the rules from the wikipedia page, here: http://en.wikipedia.org/wiki/Queen_versus_pawn_endgame. bench does not change: 5312693 but patch is real, has been tested on specific positions. --- src/endgame.cpp | 36 ++++++++++++++++++++++++++++++++++-- src/endgame.h | 1 + 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/endgame.cpp b/src/endgame.cpp index fd283ad9..c0d317b7 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -95,6 +95,7 @@ Endgames::Endgames() { add("KRKP"); add("KRKB"); add("KRKN"); + add("KQKP"); add("KQKR"); add("KBBKN"); @@ -169,12 +170,12 @@ Value Endgame::operator()(const Position& pos) const { Square winnerKSq = pos.king_square(strongerSide); Square loserKSq = pos.king_square(weakerSide); - Square bishopSquare = pos.piece_list(strongerSide, BISHOP)[0]; + Square bishopSq = pos.piece_list(strongerSide, BISHOP)[0]; // kbnk_mate_table() tries to drive toward corners A1 or H8, // if we have a bishop that cannot reach the above squares we // mirror the kings so to drive enemy toward corners A8 or H1. - if (opposite_colors(bishopSquare, SQ_A1)) + if (opposite_colors(bishopSq, SQ_A1)) { winnerKSq = mirror(winnerKSq); loserKSq = mirror(loserKSq); @@ -326,6 +327,37 @@ Value Endgame::operator()(const Position& pos) const { } +/// KQ vs KP. In general, a win for the stronger side, however, there are a few +/// important exceptions. Pawn on 7th rank, A,C,F or H file, with king next can +/// be a draw, so we scale down to distance between kings only. +template<> +Value Endgame::operator()(const Position& pos) const { + + assert(pos.non_pawn_material(strongerSide) == QueenValueMg); + assert(pos.piece_count(strongerSide, PAWN) == 0); + assert(pos.non_pawn_material(weakerSide) == 0); + assert(pos.piece_count(weakerSide, PAWN) == 1); + + Square winnerKSq = pos.king_square(strongerSide); + Square loserKSq = pos.king_square(weakerSide); + Square pawnSq = pos.piece_list(weakerSide, PAWN)[0]; + + Value result = QueenValueEg + - PawnValueEg + + DistanceBonus[square_distance(winnerKSq, loserKSq)]; + + if ( square_distance(loserKSq, pawnSq) == 1 + && relative_rank(weakerSide, pawnSq) == RANK_7) + { + File f = file_of(pawnSq); + + 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; +} + + /// KQ vs KR. This is almost identical to KX vs K: We give the attacking /// king a bonus for having the kings close together, and for forcing the /// defending king towards the edge. If we also take care to avoid null move diff --git a/src/endgame.h b/src/endgame.h index 0d7c063f..78b31d82 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -39,6 +39,7 @@ enum EndgameType { KRKP, // KR vs KP KRKB, // KR vs KB KRKN, // KR vs KN + KQKP, // KQ vs KP KQKR, // KQ vs KR KBBKN, // KBB vs KN KNNK, // KNN vs K -- 2.39.2