]> git.sesse.net Git - stockfish/blob - src/endgame.h
Restrict queen mobility to safe squares
[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-2014 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 #ifndef 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   KNNK,  // KNN vs K
37   KXK,   // Generic "mate lone king" eval
38   KBNK,  // KBN vs K
39   KPK,   // KP vs K
40   KRKP,  // KR vs KP
41   KRKB,  // KR vs KB
42   KRKN,  // KR vs KN
43   KQKP,  // KQ vs KP
44   KQKR,  // KQ vs KR
45
46
47   // Scaling functions
48   SCALE_FUNS,
49
50   KBPsK,   // KB and pawns vs K
51   KQKRPs,  // KQ vs KR and pawns
52   KRPKR,   // KRP vs KR
53   KRPKB,   // KRP vs KB
54   KRPPKRP, // KRPP vs KRP
55   KPsK,    // K and pawns vs K
56   KBPKB,   // KBP vs KB
57   KBPPKB,  // KBPP vs KB
58   KBPKN,   // KBP vs KN
59   KNPK,    // KNP vs K
60   KNPKB,   // KNP vs KB
61   KPKP     // KP vs KP
62 };
63
64
65 /// Endgame functions can be of two types depending on whether they return a
66 /// Value or a ScaleFactor. Type eg_fun<int>::type returns either ScaleFactor
67 /// or Value depending on whether the template parameter is 0 or 1.
68
69 template<int> struct eg_fun { typedef Value type; };
70 template<> struct eg_fun<1> { typedef ScaleFactor type; };
71
72
73 /// Base and derived templates for endgame evaluation and scaling functions
74
75 template<typename T>
76 struct EndgameBase {
77
78   virtual ~EndgameBase() {}
79   virtual Color color() const = 0;
80   virtual T operator()(const Position&) const = 0;
81 };
82
83
84 template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
85 struct Endgame : public EndgameBase<T> {
86
87   explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
88   Color color() const { return strongSide; }
89   T operator()(const Position&) const;
90
91 private:
92   const Color strongSide, weakSide;
93 };
94
95
96 /// The Endgames class stores the pointers to endgame evaluation and scaling
97 /// base objects in two std::map typedefs. We then use polymorphism to invoke
98 /// the actual endgame function by calling its virtual operator().
99
100 class Endgames {
101
102   typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
103   typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
104
105   M1 m1;
106   M2 m2;
107
108   M1& map(M1::mapped_type) { return m1; }
109   M2& map(M2::mapped_type) { return m2; }
110
111   template<EndgameType E> void add(const std::string& code);
112
113 public:
114   Endgames();
115  ~Endgames();
116
117   template<typename T> T probe(Key key, T& eg)
118   { return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
119 };
120
121 #endif // #ifndef ENDGAME_H_INCLUDED