X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fendgame.h;h=e40b4050756fee4b2fb8489b1a599ae6004aba43;hp=dc477288c741a78d09150b5c81e7d65902b2b050;hb=b5d5646c840d63710552fdaf2521a054dd3b8a18;hpb=f178f0a2912082e2e9d07d9b0926031322d78f67 diff --git a/src/endgame.h b/src/endgame.h index dc477288..e40b4050 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -1,263 +1,109 @@ /* 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-2010 Marco Costalba, Joona Kiiski, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + Stockfish is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ - #if !defined(ENDGAME_H_INCLUDED) #define ENDGAME_H_INCLUDED -//// -//// Includes -//// +#include +#include #include "position.h" -#include "scale.h" -#include "value.h" - - -//// -//// Types -//// - -/// Abstract base class for all special endgame evaluation functions: - -class EndgameEvaluationFunction { -public: - EndgameEvaluationFunction(Color c); - virtual ~EndgameEvaluationFunction() { } - - virtual Value apply(const Position &pos) =0; - -protected: - Color strongerSide, weakerSide; -}; - - -/// Subclasses for various concrete endgames: - -// Generic "mate lone king" eval: -class KXKEvaluationFunction : public EndgameEvaluationFunction { -public: - KXKEvaluationFunction(Color c); - Value apply(const Position &pos); -}; - -// KBN vs K: -class KBNKEvaluationFunction : public EndgameEvaluationFunction { -public: - KBNKEvaluationFunction(Color c); - Value apply(const Position &pos); -}; +#include "types.h" -// KP vs K: -class KPKEvaluationFunction : public EndgameEvaluationFunction { -public: - KPKEvaluationFunction(Color c); - Value apply(const Position &pos); -}; -// KR vs KP: -class KRKPEvaluationFunction : public EndgameEvaluationFunction { -public: - KRKPEvaluationFunction(Color c); - Value apply(const Position &pos); -}; +/// EndgameType lists all supported endgames -// KR vs KB: -class KRKBEvaluationFunction : public EndgameEvaluationFunction { -public: - KRKBEvaluationFunction(Color c); - Value apply(const Position &pos); -}; +enum EndgameType { -// KR vs KN: -class KRKNEvaluationFunction : public EndgameEvaluationFunction { -public: - KRKNEvaluationFunction(Color c); - Value apply(const Position &pos); -}; + // Evaluation functions + KXK, // Generic "mate lone king" eval + KBNK, // KBN vs K + KPK, // KP vs K + KRKP, // KR vs KP + KRKB, // KR vs KB + KRKN, // KR vs KN + KQKR, // KQ vs KR + KBBKN, // KBB vs KN + KNNK, // KNN vs K + KmmKm, // K and two minors vs K and one or two minors -// KQ vs KR: -class KQKREvaluationFunction : public EndgameEvaluationFunction { -public: - KQKREvaluationFunction(Color c); - Value apply(const Position &pos); + // Scaling functions + KBPsK, // KB+pawns vs K + KQKRPs, // KQ vs KR+pawns + KRPKR, // KRP vs KR + KRPPKRP, // KRPP vs KRP + KPsK, // King and pawns vs king + KBPKB, // KBP vs KB + KBPPKB, // KBPP vs KB + KBPKN, // KBP vs KN + KNPK, // KNP vs K + KPKP // KP vs KP }; -// KBB vs KN: -class KBBKNEvaluationFunction : public EndgameEvaluationFunction { -public: - KBBKNEvaluationFunction(Color C); - Value apply(const Position &pos); -}; -// K and two minors vs K and one or two minors: -class KmmKmEvaluationFunction : public EndgameEvaluationFunction { -public: - KmmKmEvaluationFunction(Color c); - Value apply(const Position &pos); -}; +/// Base and derived template class for endgame evaluation and scaling functions +template +struct EndgameBase { -/// Abstract base class for all evaluation scaling functions: + typedef EndgameBase Base; -class ScalingFunction { -public: - ScalingFunction(Color c); - virtual ~ScalingFunction() { } - - virtual ScaleFactor apply(const Position &pos) =0; + EndgameBase(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {} + virtual ~EndgameBase() {} + virtual T apply(const Position&) const = 0; + Color color() const { return strongerSide; } protected: Color strongerSide, weakerSide; }; -/// Subclasses for various concrete endgames: +template +struct Endgame : public EndgameBase { -// KBP vs K: -class KBPKScalingFunction : public ScalingFunction { -public: - KBPKScalingFunction(Color c); - ScaleFactor apply(const Position &pos); + explicit Endgame(Color c): EndgameBase(c) {} + T apply(const Position&) const; }; -// KQ vs KRP: -class KQKRPScalingFunction: public ScalingFunction { -public: - KQKRPScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; -// KRP vs KR: -class KRPKRScalingFunction : public ScalingFunction { -public: - KRPKRScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; +/// Endgames class stores in two std::map the pointers to endgame evaluation +/// and scaling base objects. Then we use polymorphism to invoke the actual +/// endgame function calling its apply() method that is virtual. -// KRPP vs KRP: -class KRPPKRPScalingFunction : public ScalingFunction { -public: - KRPPKRPScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; +class Endgames { -// King and pawns vs king: -class KPsKScalingFunction : public ScalingFunction { -public: - KPsKScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; + typedef std::map*> EFMap; + typedef std::map*> SFMap; -// KBP vs KB: -class KBPKBScalingFunction : public ScalingFunction { public: - KBPKBScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; + Endgames(); + ~Endgames(); + template T* get(Key key) const; -// KBP vs KN: -class KBPKNScalingFunction : public ScalingFunction { -public: - KBPKNScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; +private: + template void add(const std::string& keyCode); -// KNP vs K: -class KNPKScalingFunction : public ScalingFunction { -public: - KNPKScalingFunction(Color c); - ScaleFactor apply(const Position &pos); -}; + // Here we store two maps, for evaluate and scaling functions... + std::pair maps; -// KP vs KP: -class KPKPScalingFunction : public ScalingFunction { -public: - KPKPScalingFunction(Color c); - ScaleFactor apply(const Position &pos); + // ...and here is the accessing template function + template const std::map& get() const; }; - -//// -//// Constants and variables -//// - -// Generic "mate lone king" eval: -extern KXKEvaluationFunction EvaluateKXK, EvaluateKKX; - -// KBN vs K: -extern KBNKEvaluationFunction EvaluateKBNK, EvaluateKKBN; - -// KP vs K: -extern KPKEvaluationFunction EvaluateKPK, EvaluateKKP; - -// KR vs KP: -extern KRKPEvaluationFunction EvaluateKRKP, EvaluateKPKR; - -// KR vs KB: -extern KRKBEvaluationFunction EvaluateKRKB, EvaluateKBKR; - -// KR vs KN: -extern KRKNEvaluationFunction EvaluateKRKN, EvaluateKNKR; - -// KQ vs KR: -extern KQKREvaluationFunction EvaluateKQKR, EvaluateKRKQ; - -// KBB vs KN: -extern KBBKNEvaluationFunction EvaluateKBBKN, EvaluateKNKBB; - -// K and two minors vs K and one or two minors: -extern KmmKmEvaluationFunction EvaluateKmmKm; - -// KBP vs K: -extern KBPKScalingFunction ScaleKBPK, ScaleKKBP; - -// KQ vs KRP: -extern KQKRPScalingFunction ScaleKQKRP, ScaleKRPKQ; - -// KRP vs KR: -extern KRPKRScalingFunction ScaleKRPKR, ScaleKRKRP; - -// KRPP vs KRP: -extern KRPPKRPScalingFunction ScaleKRPPKRP, ScaleKRPKRPP; - -// King and pawns vs king: -extern KPsKScalingFunction ScaleKPsK, ScaleKKPs; - -// KBP vs KB: -extern KBPKBScalingFunction ScaleKBPKB, ScaleKBKBP; - -// KBP vs KN: -extern KBPKNScalingFunction ScaleKBPKN, ScaleKNKBP; - -// KNP vs K: -extern KNPKScalingFunction ScaleKNPK, ScaleKKNP; - -// KP vs KP: -extern KPKPScalingFunction ScaleKPKPw, ScaleKPKPb; - - -//// -//// Prototypes -//// - -extern void init_bitbases(); - - #endif // !defined(ENDGAME_H_INCLUDED)