2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef ENDGAME_H_INCLUDED
21 #define ENDGAME_H_INCLUDED
30 /// EndgameType lists all supported endgames
34 // Evaluation functions
40 KXK, // Generic "mate lone king" eval
49 KmmKm, // K and two minors vs K and one or two minors
55 KBPsK, // KB+pawns vs K
56 KQKRPs, // KQ vs KR+pawns
58 KRPPKRP, // KRPP vs KRP
59 KPsK, // King and pawns vs king
69 /// Endgame functions can be of two types according if return a Value or a
70 /// ScaleFactor. Type eg_fun<int>::type equals to either ScaleFactor or Value
71 /// depending if the template parameter is 0 or 1.
73 template<int> struct eg_fun { typedef Value type; };
74 template<> struct eg_fun<1> { typedef ScaleFactor type; };
77 /// Base and derived templates for endgame evaluation and scaling functions
82 virtual ~EndgameBase() {}
83 virtual Color color() const = 0;
84 virtual T operator()(const Position&) const = 0;
88 template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
89 struct Endgame : public EndgameBase<T> {
91 explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {}
92 Color color() const { return strongerSide; }
93 T operator()(const Position&) const;
96 Color strongerSide, weakerSide;
100 /// Endgames class stores in two std::map the pointers to endgame evaluation
101 /// and scaling base objects. Then we use polymorphism to invoke the actual
102 /// endgame function calling its operator() that is virtual.
106 typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
107 typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
112 M1& map(M1::mapped_type) { return m1; }
113 M2& map(M2::mapped_type) { return m2; }
115 template<EndgameType E> void add(const std::string& code);
121 template<typename T> T probe(Key key, T& eg)
122 { return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
125 #endif // #ifndef ENDGAME_H_INCLUDED