]> git.sesse.net Git - stockfish/blob - src/endgame.h
Revert previous patches due to bug
[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
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
50   KBPsK,   // KB+pawns vs K
51   KQKRPs,  // KQ vs KR+pawns
52   KRPKR,   // KRP vs KR
53   KRPPKRP, // KRPP vs KRP
54   KPsK,    // King and pawns vs king
55   KBPKB,   // KBP vs KB
56   KBPPKB,  // KBPP vs KB
57   KBPKN,   // KBP vs KN
58   KNPK,    // KNP vs K
59   KPKP     // KP vs KP
60 };
61
62
63 /// Base and derived templates for endgame evaluation and scaling functions
64
65 template<typename T>
66 struct EndgameBase {
67
68   typedef EndgameBase<T> Base;
69
70   virtual ~EndgameBase() {}
71   virtual Color color() const = 0;
72   virtual T apply(const Position&) const = 0;
73 };
74
75
76 template<typename T, EndgameType>
77 struct Endgame : public EndgameBase<T> {
78
79   explicit Endgame(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {}
80   Color color() const { return strongerSide; }
81   T apply(const Position&) const;
82
83 private:
84   Color strongerSide, weakerSide;
85 };
86
87
88 /// Endgames class stores in two std::map the pointers to endgame evaluation
89 /// and scaling base objects. Then we use polymorphism to invoke the actual
90 /// endgame function calling its apply() method that is virtual.
91
92 class Endgames {
93
94   typedef std::map<Key, EndgameBase<Value>* > EFMap;
95   typedef std::map<Key, EndgameBase<ScaleFactor>* > SFMap;
96
97 public:
98   Endgames();
99   ~Endgames();
100   template<class T> T* get(Key key) const;
101
102 private:
103   template<class T> void add(const std::string& keyCode);
104
105   // Here we store two maps, for evaluate and scaling functions...
106   std::pair<EFMap, SFMap> maps;
107
108   // ...and here is the accessing template function
109   template<typename T> const std::map<Key, T*>& get() const;
110 };
111
112 #endif // !defined(ENDGAME_H_INCLUDED)