]> git.sesse.net Git - stockfish/blob - src/endgame.h
5b4128b7b6130f7c55ef8cad99b2ec8416090c7d
[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-2010 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 <string>
24 #include <map>
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   KXK,   // Generic "mate lone king" eval
36   KBNK,  // KBN vs K
37   KPK,   // KP vs K
38   KRKP,  // KR vs KP
39   KRKB,  // KR vs KB
40   KRKN,  // KR vs KN
41   KQKR,  // KQ vs KR
42   KBBKN, // KBB vs KN
43   KNNK,  // KNN vs K
44   KmmKm, // K and two minors vs K and one or two minors
45
46   // Scaling functions
47   KBPsK,   // KB+pawns vs K
48   KQKRPs,  // KQ vs KR+pawns
49   KRPKR,   // KRP vs KR
50   KRPPKRP, // KRPP vs KRP
51   KPsK,    // King and pawns vs king
52   KBPKB,   // KBP vs KB
53   KBPPKB,  // KBPP vs KB
54   KBPKN,   // KBP vs KN
55   KNPK,    // KNP vs K
56   KPKP     // KP vs KP
57 };
58
59
60 /// Base and derived template class for endgame evaluation and scaling functions
61
62 template<typename T>
63 struct EndgameBase {
64
65   typedef EndgameBase<T> Base;
66
67   virtual ~EndgameBase() {}
68   virtual Color color() const = 0;
69   virtual T apply(const Position&) const = 0;
70 };
71
72
73 template<typename T, EndgameType>
74 struct Endgame : public EndgameBase<T> {
75
76   explicit Endgame(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {}
77   Color color() const { return strongerSide; }
78   T apply(const Position&) const;
79
80 private:
81   Color strongerSide, weakerSide;
82 };
83
84
85 /// Endgames class stores in two std::map the pointers to endgame evaluation
86 /// and scaling base objects. Then we use polymorphism to invoke the actual
87 /// endgame function calling its apply() method that is virtual.
88
89 class Endgames {
90
91   typedef std::map<Key, EndgameBase<Value>*> EFMap;
92   typedef std::map<Key, EndgameBase<ScaleFactor>*> SFMap;
93
94 public:
95   Endgames();
96   ~Endgames();
97   template<class T> T* get(Key key) const;
98
99 private:
100   template<class T> void add(const std::string& keyCode);
101
102   // Here we store two maps, for evaluate and scaling functions...
103   std::pair<EFMap, SFMap> maps;
104
105   // ...and here is the accessing template function
106   template<typename T> const std::map<Key, T*>& get() const;
107 };
108
109 #endif // !defined(ENDGAME_H_INCLUDED)