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-2010 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 #if !defined(ENDGAME_H_INCLUDED)
21 #define ENDGAME_H_INCLUDED
30 /// EndgameType lists all supported endgames
34 // Evaluation functions
36 KXK, // Generic "mate lone king" eval
45 KmmKm, // K and two minors vs K and one or two minors
51 KBPsK, // KB+pawns vs K
52 KQKRPs, // KQ vs KR+pawns
54 KRPPKRP, // KRPP vs KRP
55 KPsK, // King and pawns vs king
64 /// Some magic to detect family type of endgame from its enum value
66 template<bool> struct bool_to_type { typedef Value type; };
67 template<> struct bool_to_type<true> { typedef ScaleFactor type; };
68 template<EndgameType E> struct eg_family : public bool_to_type<(E > SCALE_FUNS)> {};
71 /// Base and derived templates for endgame evaluation and scaling functions
76 virtual ~EndgameBase() {}
77 virtual Color color() const = 0;
78 virtual T apply(const Position&) const = 0;
82 template<EndgameType E, typename T = typename eg_family<E>::type>
83 struct Endgame : public EndgameBase<T> {
85 explicit Endgame(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {}
86 Color color() const { return strongerSide; }
87 T apply(const Position&) const;
90 Color strongerSide, weakerSide;
94 /// Endgames class stores in two std::map the pointers to endgame evaluation
95 /// and scaling base objects. Then we use polymorphism to invoke the actual
96 /// endgame function calling its apply() method that is virtual.
101 struct EMap { typedef std::map<Key, EndgameBase<T>*> type; };
105 template<typename T> EndgameBase<T>* get(Key key) const;
108 template<EndgameType E> void add(const std::string& keyCode);
110 // Here we store two maps, for evaluate and scaling functions...
111 std::pair<EMap<Value>::type, EMap<ScaleFactor>::type> maps;
113 // ...and here is the accessing template function
114 template<typename T> const typename EMap<T>::type& map() const;
117 #endif // !defined(ENDGAME_H_INCLUDED)