X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fendgame.cpp;h=abbb0876c64bd35b77be6f919ca66b793d02c4cb;hp=01f4101ed8866ce63748d72cd8da177b78a229e1;hb=aa925a0e2905042a978860a75798398cb79b2600;hpb=67375f4693c97fb1321864bb4d143812cd824f9b diff --git a/src/endgame.cpp b/src/endgame.cpp index 01f4101e..abbb0876 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -1,7 +1,7 @@ /* Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) - Copyright (C) 2008 Marco Costalba + Copyright (C) 2008-2009 Marco Costalba Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,43 +25,10 @@ #include #include "bitbase.h" +#include "bitcount.h" #include "endgame.h" -//// -//// Constants and variables -//// - -/// Evaluation functions - -// Generic "mate lone king" eval -EvaluationFunction EvaluateKXK(WHITE), EvaluateKKX(BLACK); - -// K and two minors vs K and one or two minors -EvaluationFunction EvaluateKmmKm(WHITE); - -EvaluationFunction EvaluateKBNK(WHITE), EvaluateKKBN(BLACK); // KBN vs K -EvaluationFunction EvaluateKPK(WHITE), EvaluateKKP(BLACK); // KP vs K -EvaluationFunction EvaluateKRKP(WHITE), EvaluateKPKR(BLACK); // KR vs KP -EvaluationFunction EvaluateKRKB(WHITE), EvaluateKBKR(BLACK); // KR vs KB -EvaluationFunction EvaluateKRKN(WHITE), EvaluateKNKR(BLACK); // KR vs KN -EvaluationFunction EvaluateKQKR(WHITE), EvaluateKRKQ(BLACK); // KQ vs KR -EvaluationFunction EvaluateKBBKN(WHITE), EvaluateKNKBB(BLACK); // KBB vs KN - - -/// Scaling functions - -ScalingFunction ScaleKBPK(WHITE), ScaleKKBP(BLACK); // KBP vs K -ScalingFunction ScaleKQKRP(WHITE), ScaleKRPKQ(BLACK); // KQ vs KRP -ScalingFunction ScaleKRPKR(WHITE), ScaleKRKRP(BLACK); // KRP vs KR -ScalingFunction ScaleKRPPKRP(WHITE), ScaleKRPKRPP(BLACK); // KRPP vs KRP -ScalingFunction ScaleKPsK(WHITE), ScaleKKPs(BLACK); // King and pawns vs king -ScalingFunction ScaleKBPKB(WHITE), ScaleKBKBP(BLACK); // KBP vs KB -ScalingFunction ScaleKBPKN(WHITE), ScaleKNKBP(BLACK); // KBP vs KN -ScalingFunction ScaleKNPK(WHITE), ScaleKKNP(BLACK); // KNP vs K -ScalingFunction ScaleKPKPw(WHITE), ScaleKPKPb(BLACK); // KPKP - - //// //// Local definitions //// @@ -381,11 +348,18 @@ Value EvaluationFunction::apply(const Position& pos) { return (strongerSide == pos.side_to_move() ? result : -result); } + +/// K and two minors vs K and one or two minors or K and two knights against +/// king alone are always draw. template<> -Value EvaluationFunction::apply(const Position &pos) { +Value EvaluationFunction::apply(const Position&) { return Value(0); } +template<> +Value EvaluationFunction::apply(const Position&) { + return Value(0); +} /// KBPKScalingFunction scales endgames where the stronger side has king, /// bishop and one or more pawns. It checks for draws with rook pawns and a @@ -732,6 +706,80 @@ ScaleFactor ScalingFunction::apply(const Position &pos) { } +/// KBPPKBScalingFunction scales KBPP vs KB endgames. It detects a few basic +/// draws with opposite-colored bishops. +template<> +ScaleFactor ScalingFunction::apply(const Position& pos) { + + assert(pos.non_pawn_material(strongerSide) == BishopValueMidgame); + assert(pos.piece_count(strongerSide, BISHOP) == 1); + assert(pos.piece_count(strongerSide, PAWN) == 2); + assert(pos.non_pawn_material(weakerSide) == BishopValueMidgame); + assert(pos.piece_count(weakerSide, BISHOP) == 1); + assert(pos.piece_count(weakerSide, PAWN) == 0); + + Square wbsq = pos.piece_list(strongerSide, BISHOP, 0); + Square bbsq = pos.piece_list(weakerSide, BISHOP, 0); + + if (square_color(wbsq) == square_color(bbsq)) + // Not opposite-colored bishops, no scaling + return SCALE_FACTOR_NONE; + + Square ksq = pos.king_square(weakerSide); + Square psq1 = pos.piece_list(strongerSide, PAWN, 0); + Square psq2 = pos.piece_list(strongerSide, PAWN, 1); + Rank r1 = square_rank(psq1); + Rank r2 = square_rank(psq2); + Square blockSq1, blockSq2; + + if (relative_rank(strongerSide, psq1) > relative_rank(strongerSide, psq2)) + { + blockSq1 = psq1 + pawn_push(strongerSide); + blockSq2 = make_square(square_file(psq2), square_rank(psq1)); + } + else + { + blockSq1 = psq2 + pawn_push(strongerSide); + blockSq2 = make_square(square_file(psq1), square_rank(psq2)); + } + + switch (file_distance(psq1, psq2)) + { + case 0: + // Both pawns are on the same file. Easy draw if defender firmly controls + // some square in the frontmost pawn's path. + if ( square_file(ksq) == square_file(blockSq1) + && relative_rank(strongerSide, ksq) >= relative_rank(strongerSide, blockSq1) + && square_color(ksq) != square_color(wbsq)) + return ScaleFactor(0); + else + return SCALE_FACTOR_NONE; + + case 1: + // Pawns on neighboring files. Draw if defender firmly controls the square + // in front of the frontmost pawn's path, and the square diagonally behind + // this square on the file of the other pawn. + if ( ksq == blockSq1 + && square_color(ksq) != square_color(wbsq) + && ( bbsq == blockSq2 + || (pos.piece_attacks(blockSq2) & pos.bishops(weakerSide)) + || rank_distance(r1, r2) >= 2)) + return ScaleFactor(0); + else if ( ksq == blockSq2 + && square_color(ksq) != square_color(wbsq) + && ( bbsq == blockSq1 + || (pos.piece_attacks(blockSq1) & pos.bishops(weakerSide)))) + return ScaleFactor(0); + else + return SCALE_FACTOR_NONE; + + default: + // The pawns are not on the same file or adjacent files. No scaling. + return SCALE_FACTOR_NONE; + } +} + + /// KBPKNScalingFunction scales KBP vs KN endgames. There is a single rule: /// If the defending king is somewhere along the path of the pawn, and the /// square of the king is not of the same color as the stronger side's bishop,