]> git.sesse.net Git - stockfish/blob - src/endgame.h
Update Makefile for Mac OS X compilation
[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-2015 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 <memory>
25 #include <string>
26 #include <type_traits>
27 #include <utility>
28
29 #include "position.h"
30 #include "types.h"
31
32
33 /// EndgameType lists all supported endgames
34
35 enum EndgameType {
36
37   // Evaluation functions
38
39   KNNK,  // KNN vs K
40   KXK,   // Generic "mate lone king" eval
41   KBNK,  // KBN vs K
42   KPK,   // KP vs K
43   KRKP,  // KR vs KP
44   KRKB,  // KR vs KB
45   KRKN,  // KR vs KN
46   KQKP,  // KQ vs KP
47   KQKR,  // KQ vs KR
48
49
50   // Scaling functions
51   SCALING_FUNCTIONS,
52
53   KBPsK,   // KB and pawns vs K
54   KQKRPs,  // KQ vs KR and pawns
55   KRPKR,   // KRP vs KR
56   KRPKB,   // KRP vs KB
57   KRPPKRP, // KRPP vs KRP
58   KPsK,    // K and pawns vs K
59   KBPKB,   // KBP vs KB
60   KBPPKB,  // KBPP vs KB
61   KBPKN,   // KBP vs KN
62   KNPK,    // KNP vs K
63   KNPKB,   // KNP vs KB
64   KPKP     // KP vs KP
65 };
66
67
68 /// Endgame functions can be of two types depending on whether they return a
69 /// Value or a ScaleFactor.
70 template<EndgameType E> using
71 eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
72
73
74 /// Base and derived templates for endgame evaluation and scaling functions
75
76 template<typename T>
77 struct EndgameBase {
78
79   virtual ~EndgameBase() {}
80   virtual Color strong_side() const = 0;
81   virtual T operator()(const Position&) const = 0;
82 };
83
84
85 template<EndgameType E, typename T = eg_type<E>>
86 struct Endgame : public EndgameBase<T> {
87
88   explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
89   Color strong_side() const { return strongSide; }
90   T operator()(const Position&) const;
91
92 private:
93   Color strongSide, weakSide;
94 };
95
96
97 /// The Endgames class stores the pointers to endgame evaluation and scaling
98 /// base objects in two std::map. We use polymorphism to invoke the actual
99 /// endgame function by calling its virtual operator().
100
101 class Endgames {
102
103   template<typename T> using Map = std::map<Key, std::unique_ptr<EndgameBase<T>>>;
104
105   template<EndgameType E, typename T = eg_type<E>>
106   void add(const std::string& code);
107
108   template<typename T>
109   Map<T>& map() {
110     return std::get<std::is_same<T, ScaleFactor>::value>(maps);
111   }
112
113   std::pair<Map<Value>, Map<ScaleFactor>> maps;
114
115 public:
116   Endgames();
117
118   template<typename T>
119   EndgameBase<T>* probe(Key key) {
120     return map<T>().count(key) ? map<T>()[key].get() : nullptr;
121   }
122 };
123
124 #endif // #ifndef ENDGAME_H_INCLUDED