]> git.sesse.net Git - stockfish/blob - src/endgame.h
Remove FutilityMoveCounts array. (#2024)
[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   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef ENDGAME_H_INCLUDED
22 #define ENDGAME_H_INCLUDED
23
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <type_traits>
28 #include <utility>
29
30 #include "position.h"
31 #include "types.h"
32
33
34 /// EndgameCode lists all supported endgame functions by corresponding codes
35
36 enum EndgameCode {
37
38   EVALUATION_FUNCTIONS,
39   KNNK,  // KNN vs K
40   KNNKP, // KNN vs KP
41   KXK,   // Generic "mate lone king" eval
42   KBNK,  // KBN vs K
43   KPK,   // KP vs K
44   KRKP,  // KR vs KP
45   KRKB,  // KR vs KB
46   KRKN,  // KR vs KN
47   KQKP,  // KQ vs KP
48   KQKR,  // KQ vs KR
49
50   SCALING_FUNCTIONS,
51   KBPsK,   // KB and pawns vs K
52   KQKRPs,  // KQ vs KR and pawns
53   KRPKR,   // KRP vs KR
54   KRPKB,   // KRP vs KB
55   KRPPKRP, // KRPP vs KRP
56   KPsK,    // K and pawns vs K
57   KBPKB,   // KBP vs KB
58   KBPPKB,  // KBPP vs KB
59   KBPKN,   // KBP vs KN
60   KNPK,    // KNP vs K
61   KNPKB,   // KNP vs KB
62   KPKP     // KP vs KP
63 };
64
65
66 /// Endgame functions can be of two types depending on whether they return a
67 /// Value or a ScaleFactor.
68
69 template<EndgameCode E> using
70 eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
71
72
73 /// Base and derived functors for endgame evaluation and scaling functions
74
75 template<typename T>
76 struct EndgameBase {
77
78   explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {}
79   virtual ~EndgameBase() = default;
80   virtual T operator()(const Position&) const = 0;
81
82   const Color strongSide, weakSide;
83 };
84
85
86 template<EndgameCode E, typename T = eg_type<E>>
87 struct Endgame : public EndgameBase<T> {
88
89   explicit Endgame(Color c) : EndgameBase<T>(c) {}
90   T operator()(const Position&) const override;
91 };
92
93
94 /// The Endgames class stores the pointers to endgame evaluation and scaling
95 /// base objects in two std::map. We use polymorphism to invoke the actual
96 /// endgame function by calling its virtual operator().
97
98 class Endgames {
99
100   template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
101   template<typename T> using Map = std::map<Key, Ptr<T>>;
102
103   template<typename T>
104   Map<T>& map() {
105     return std::get<std::is_same<T, ScaleFactor>::value>(maps);
106   }
107
108   template<EndgameCode E, typename T = eg_type<E>>
109   void add(const std::string& code) {
110
111     StateInfo st;
112     map<T>()[Position().set(code, WHITE, &st).material_key()] = Ptr<T>(new Endgame<E>(WHITE));
113     map<T>()[Position().set(code, BLACK, &st).material_key()] = Ptr<T>(new Endgame<E>(BLACK));
114   }
115
116   std::pair<Map<Value>, Map<ScaleFactor>> maps;
117
118 public:
119   Endgames() {
120
121     add<KPK>("KPK");
122     add<KNNK>("KNNK");
123     add<KBNK>("KBNK");
124     add<KRKP>("KRKP");
125     add<KRKB>("KRKB");
126     add<KRKN>("KRKN");
127     add<KQKP>("KQKP");
128     add<KQKR>("KQKR");
129     add<KNNKP>("KNNKP");
130
131     add<KNPK>("KNPK");
132     add<KNPKB>("KNPKB");
133     add<KRPKR>("KRPKR");
134     add<KRPKB>("KRPKB");
135     add<KBPKB>("KBPKB");
136     add<KBPKN>("KBPKN");
137     add<KBPPKB>("KBPPKB");
138     add<KRPPKRP>("KRPPKRP");
139   }
140
141   template<typename T>
142   const EndgameBase<T>* probe(Key key) {
143     return map<T>().count(key) ? map<T>()[key].get() : nullptr;
144   }
145 };
146
147 #endif // #ifndef ENDGAME_H_INCLUDED