From fe7e0a425eef79c56ff27a48ea4f26f32d880c6e Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Mon, 26 Apr 2010 11:42:46 +0200 Subject: [PATCH] Cleanup material distribution detectors No functional change (verified each function) Signed-off-by: Marco Costalba --- src/material.cpp | 60 +++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/material.cpp b/src/material.cpp index 65c8c862..d3483980 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -66,6 +66,29 @@ namespace { typedef EndgameEvaluationFunctionBase EF; typedef EndgameScalingFunctionBase SF; + + // Helper templates used to detect a given material distribution + template bool is_KXK(const Position& pos) { + const Color Them = (Us == WHITE ? BLACK : WHITE); + return pos.non_pawn_material(Them) == Value(0) + && pos.piece_count(Them, PAWN) == 0 + && pos.non_pawn_material(Us) >= RookValueMidgame; + } + + template bool is_KBPsK(const Position& pos) { + return pos.non_pawn_material(Us) == BishopValueMidgame + && pos.piece_count(Us, BISHOP) == 1 + && pos.piece_count(Us, PAWN) >= 1; + } + + template bool is_KQKRPs(const Position& pos) { + const Color Them = (Us == WHITE ? BLACK : WHITE); + return pos.piece_count(Us, PAWN) == 0 + && pos.non_pawn_material(Us) == QueenValueMidgame + && pos.piece_count(Us, QUEEN) == 1 + && pos.piece_count(Them, ROOK) == 1 + && pos.piece_count(Them, PAWN) >= 1; + } } @@ -181,22 +204,13 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) { if ((mi->evaluationFunction = funcs->get(key)) != NULL) return mi; - else if ( pos.non_pawn_material(BLACK) == Value(0) - && pos.piece_count(BLACK, PAWN) == 0 - && pos.non_pawn_material(WHITE) >= RookValueMidgame) - { - mi->evaluationFunction = &EvaluateKXK; - return mi; - } - else if ( pos.non_pawn_material(WHITE) == Value(0) - && pos.piece_count(WHITE, PAWN) == 0 - && pos.non_pawn_material(BLACK) >= RookValueMidgame) + else if (is_KXK(pos) || is_KXK(pos)) { - mi->evaluationFunction = &EvaluateKKX; + mi->evaluationFunction = is_KXK(pos) ? &EvaluateKXK : &EvaluateKKX; return mi; } - else if ( pos.pieces(PAWN) == EmptyBoardBB - && pos.pieces(ROOK) == EmptyBoardBB + else if ( pos.pieces(PAWN) == EmptyBoardBB + && pos.pieces(ROOK) == EmptyBoardBB && pos.pieces(QUEEN) == EmptyBoardBB) { // Minor piece endgame with at least one minor piece per side and @@ -230,28 +244,16 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) { // Generic scaling functions that refer to more then one material // distribution. Should be probed after the specialized ones. // Note that these ones don't return after setting the function. - if ( pos.non_pawn_material(WHITE) == BishopValueMidgame - && pos.piece_count(WHITE, BISHOP) == 1 - && pos.piece_count(WHITE, PAWN) >= 1) + if (is_KBPsK(pos)) mi->scalingFunction[WHITE] = &ScaleKBPsK; - if ( pos.non_pawn_material(BLACK) == BishopValueMidgame - && pos.piece_count(BLACK, BISHOP) == 1 - && pos.piece_count(BLACK, PAWN) >= 1) + if (is_KBPsK(pos)) mi->scalingFunction[BLACK] = &ScaleKKBPs; - if ( pos.piece_count(WHITE, PAWN) == 0 - && pos.non_pawn_material(WHITE) == QueenValueMidgame - && pos.piece_count(WHITE, QUEEN) == 1 - && pos.piece_count(BLACK, ROOK) == 1 - && pos.piece_count(BLACK, PAWN) >= 1) + if (is_KQKRPs(pos)) mi->scalingFunction[WHITE] = &ScaleKQKRPs; - else if ( pos.piece_count(BLACK, PAWN) == 0 - && pos.non_pawn_material(BLACK) == QueenValueMidgame - && pos.piece_count(BLACK, QUEEN) == 1 - && pos.piece_count(WHITE, ROOK) == 1 - && pos.piece_count(WHITE, PAWN) >= 1) + else if (is_KQKRPs(pos)) mi->scalingFunction[BLACK] = &ScaleKRPsKQ; if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == Value(0)) -- 2.39.2