]> git.sesse.net Git - stockfish/blob - src/endgame.h
424a3591cb80e6a67b22c784f524621186d28a98
[stockfish] / src / endgame.h
1 /*
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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
5
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.
10
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.
15
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/>.
18 */
19
20 #if !defined(ENDGAME_H_INCLUDED)
21 #define ENDGAME_H_INCLUDED
22
23 #include <map>
24 #include <string>
25
26 #include "position.h"
27 #include "types.h"
28
29
30 /// EndgameType lists all supported endgames
31
32 enum EndgameType {
33
34   // Evaluation functions
35
36   KXK,   // Generic "mate lone king" eval
37   KBNK,  // KBN vs K
38   KPK,   // KP vs K
39   KRKP,  // KR vs KP
40   KRKB,  // KR vs KB
41   KRKN,  // KR vs KN
42   KQKR,  // KQ vs KR
43   KBBKN, // KBB vs KN
44   KNNK,  // KNN vs K
45   KmmKm, // K and two minors vs K and one or two minors
46
47
48   // Scaling functions
49   SCALE_FUNS,
50
51   KBPsK,   // KB+pawns vs K
52   KQKRPs,  // KQ vs KR+pawns
53   KRPKR,   // KRP vs KR
54   KRPPKRP, // KRPP vs KRP
55   KPsK,    // King and pawns vs king
56   KBPKB,   // KBP vs KB
57   KBPPKB,  // KBPP vs KB
58   KBPKN,   // KBP vs KN
59   KNPK,    // KNP vs K
60   KPKP     // KP vs KP
61 };
62
63
64 /// Endgame functions can be of two types according if return a Value or a
65 /// ScaleFactor. Type eg_fun<int>::type equals to either ScaleFactor or Value
66 /// depending if the template parameter is 0 or 1.
67
68 template<int> struct eg_fun { typedef Value type; };
69 template<> struct eg_fun<1> { typedef ScaleFactor type; };
70
71
72 /// Base and derived templates for endgame evaluation and scaling functions
73
74 template<typename T>
75 struct EndgameBase {
76
77   virtual ~EndgameBase() {}
78   virtual Color color() const = 0;
79   virtual T operator()(const Position&) const = 0;
80 };
81
82
83 template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
84 struct Endgame : public EndgameBase<T> {
85
86   explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {}
87   Color color() const { return strongerSide; }
88   T operator()(const Position&) const;
89
90 private:
91   Color strongerSide, weakerSide;
92 };
93
94
95 /// Endgames class stores in two std::map the pointers to endgame evaluation
96 /// and scaling base objects. Then we use polymorphism to invoke the actual
97 /// endgame function calling its operator() method that is virtual.
98
99 class Endgames {
100
101   typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
102   typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
103
104   M1 m1;
105   M2 m2;
106
107   M1& map(M1::value_type::second_type) { return m1; }
108   M2& map(M2::value_type::second_type) { return m2; }
109
110   template<EndgameType E> void add(const std::string& code);
111
112 public:
113   Endgames();
114   ~Endgames();
115
116   template<typename T> EndgameBase<T>* probe(Key key) {
117     return map((EndgameBase<T>*)0).count(key) ? map((EndgameBase<T>*)0)[key] : NULL;
118   }
119 };
120
121 #endif // !defined(ENDGAME_H_INCLUDED)