]> git.sesse.net Git - stockfish/blob - src/endgame.h
Add / remove leaves from search tree ttPv
[stockfish] / src / endgame.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef ENDGAME_H_INCLUDED
20 #define ENDGAME_H_INCLUDED
21
22 #include <memory>
23 #include <string>
24 #include <type_traits>
25 #include <unordered_map>
26 #include <utility>
27
28 #include "position.h"
29 #include "types.h"
30
31
32 /// EndgameCode lists all supported endgame functions by corresponding codes
33
34 enum EndgameCode {
35
36   EVALUATION_FUNCTIONS,
37   KNNK,  // KNN vs K
38   KNNKP, // KNN vs KP
39   KXK,   // Generic "mate lone king" eval
40   KBNK,  // KBN vs K
41   KPK,   // KP vs K
42   KRKP,  // KR vs KP
43   KRKB,  // KR vs KB
44   KRKN,  // KR vs KN
45   KQKP,  // KQ vs KP
46   KQKR,  // KQ vs KR
47
48   SCALING_FUNCTIONS,
49   KBPsK,   // KB and pawns vs K
50   KQKRPs,  // KQ vs KR and pawns
51   KRPKR,   // KRP vs KR
52   KRPKB,   // KRP vs KB
53   KRPPKRP, // KRPP vs KRP
54   KPsK,    // K and pawns vs K
55   KBPKB,   // KBP vs KB
56   KBPPKB,  // KBPP vs KB
57   KBPKN,   // KBP vs KN
58   KPKP     // KP vs KP
59 };
60
61
62 /// Endgame functions can be of two types depending on whether they return a
63 /// Value or a ScaleFactor.
64
65 template<EndgameCode E> using
66 eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
67
68
69 /// Base and derived functors for endgame evaluation and scaling functions
70
71 template<typename T>
72 struct EndgameBase {
73
74   explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {}
75   virtual ~EndgameBase() = default;
76   virtual T operator()(const Position&) const = 0;
77
78   const Color strongSide, weakSide;
79 };
80
81
82 template<EndgameCode E, typename T = eg_type<E>>
83 struct Endgame : public EndgameBase<T> {
84
85   explicit Endgame(Color c) : EndgameBase<T>(c) {}
86   T operator()(const Position&) const override;
87 };
88
89
90 /// The Endgames namespace handles the pointers to endgame evaluation and scaling
91 /// base objects in two std::map. We use polymorphism to invoke the actual
92 /// endgame function by calling its virtual operator().
93
94 namespace Endgames {
95
96   template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
97   template<typename T> using Map = std::unordered_map<Key, Ptr<T>>;
98
99   extern std::pair<Map<Value>, Map<ScaleFactor>> maps;
100
101   void init();
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   template<typename T>
117   const EndgameBase<T>* probe(Key key) {
118     auto it = map<T>().find(key);
119     return it != map<T>().end() ? it->second.get() : nullptr;
120   }
121 }
122
123 #endif // #ifndef ENDGAME_H_INCLUDED