| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef ENDGAME_H_INCLUDED |
| #define ENDGAME_H_INCLUDED |
|
|
| #include <memory> |
| #include <string> |
| #include <type_traits> |
| #include <unordered_map> |
| #include <utility> |
|
|
| #include "position.h" |
| #include "types.h" |
|
|
| namespace Stockfish { |
|
|
| |
|
|
| enum EndgameCode { |
|
|
| EVALUATION_FUNCTIONS, |
| KNNK, |
| KNNKP, |
| KXK, |
| KBNK, |
| KPK, |
| KRKP, |
| KRKB, |
| KRKN, |
| KQKP, |
| KQKR, |
|
|
| SCALING_FUNCTIONS, |
| KBPsK, |
| KQKRPs, |
| KRPKR, |
| KRPKB, |
| KRPPKRP, |
| KPsK, |
| KBPKB, |
| KBPPKB, |
| KBPKN, |
| KPKP |
| }; |
|
|
|
|
| |
| |
|
|
| template<EndgameCode E> using |
| eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type; |
|
|
|
|
| |
|
|
| template<typename T> |
| struct EndgameBase { |
|
|
| explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {} |
| virtual ~EndgameBase() = default; |
| virtual T operator()(const Position&) const = 0; |
|
|
| const Color strongSide, weakSide; |
| }; |
|
|
|
|
| template<EndgameCode E, typename T = eg_type<E>> |
| struct Endgame : public EndgameBase<T> { |
|
|
| explicit Endgame(Color c) : EndgameBase<T>(c) {} |
| T operator()(const Position&) const override; |
| }; |
|
|
|
|
| |
| |
| |
|
|
| namespace Endgames { |
|
|
| template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>; |
| template<typename T> using Map = std::unordered_map<Key, Ptr<T>>; |
|
|
| extern std::pair<Map<Value>, Map<ScaleFactor>> maps; |
|
|
| void init(); |
|
|
| template<typename T> |
| Map<T>& map() { |
| return std::get<std::is_same<T, ScaleFactor>::value>(maps); |
| } |
|
|
| template<EndgameCode E, typename T = eg_type<E>> |
| void add(const std::string& code) { |
|
|
| StateInfo st; |
| map<T>()[Position().set(code, WHITE, &st).material_key()] = Ptr<T>(new Endgame<E>(WHITE)); |
| map<T>()[Position().set(code, BLACK, &st).material_key()] = Ptr<T>(new Endgame<E>(BLACK)); |
| } |
|
|
| template<typename T> |
| const EndgameBase<T>* probe(Key key) { |
| auto it = map<T>().find(key); |
| return it != map<T>().end() ? it->second.get() : nullptr; |
| } |
| } |
|
|
| } |
|
|
| #endif |
|
|