Rev 169 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
96 | pmbaty | 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-2016 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 | /// EndgameType lists all supported endgames |
||
35 | |||
36 | enum EndgameType { |
||
37 | |||
38 | // Evaluation functions |
||
39 | |||
40 | KNNK, // KNN vs K |
||
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 | |||
51 | // Scaling functions |
||
52 | SCALING_FUNCTIONS, |
||
53 | |||
54 | KBPsK, // KB and pawns vs K |
||
55 | KQKRPs, // KQ vs KR and pawns |
||
56 | KRPKR, // KRP vs KR |
||
57 | KRPKB, // KRP vs KB |
||
58 | KRPPKRP, // KRPP vs KRP |
||
59 | KPsK, // K and pawns vs K |
||
60 | KBPKB, // KBP vs KB |
||
61 | KBPPKB, // KBPP vs KB |
||
62 | KBPKN, // KBP vs KN |
||
63 | KNPK, // KNP vs K |
||
64 | KNPKB, // KNP vs KB |
||
65 | KPKP // KP vs KP |
||
66 | }; |
||
67 | |||
68 | |||
69 | /// Endgame functions can be of two types depending on whether they return a |
||
70 | /// Value or a ScaleFactor. |
||
71 | template<EndgameType E> using |
||
72 | eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type; |
||
73 | |||
74 | |||
75 | /// Base and derived templates for endgame evaluation and scaling functions |
||
76 | |||
77 | template<typename T> |
||
78 | struct EndgameBase { |
||
79 | |||
80 | virtual ~EndgameBase() = default; |
||
81 | virtual Color strong_side() const = 0; |
||
82 | virtual T operator()(const Position&) const = 0; |
||
83 | }; |
||
84 | |||
85 | |||
86 | template<EndgameType E, typename T = eg_type<E>> |
||
87 | struct Endgame : public EndgameBase<T> { |
||
88 | |||
89 | explicit Endgame(Color c) : strongSide(c), weakSide(~c) {} |
||
90 | Color strong_side() const { return strongSide; } |
||
91 | T operator()(const Position&) const; |
||
92 | |||
93 | private: |
||
94 | Color strongSide, weakSide; |
||
95 | }; |
||
96 | |||
97 | |||
98 | /// The Endgames class stores the pointers to endgame evaluation and scaling |
||
99 | /// base objects in two std::map. We use polymorphism to invoke the actual |
||
100 | /// endgame function by calling its virtual operator(). |
||
101 | |||
102 | class Endgames { |
||
103 | |||
104 | template<typename T> using Map = std::map<Key, std::unique_ptr<EndgameBase<T>>>; |
||
105 | |||
106 | template<EndgameType E, typename T = eg_type<E>> |
||
107 | void add(const std::string& code); |
||
108 | |||
109 | template<typename T> |
||
110 | Map<T>& map() { |
||
111 | return std::get<std::is_same<T, ScaleFactor>::value>(maps); |
||
112 | } |
||
113 | |||
114 | std::pair<Map<Value>, Map<ScaleFactor>> maps; |
||
115 | |||
116 | public: |
||
117 | Endgames(); |
||
118 | |||
119 | template<typename T> |
||
120 | EndgameBase<T>* probe(Key key) { |
||
121 | return map<T>().count(key) ? map<T>()[key].get() : nullptr; |
||
122 | } |
||
123 | }; |
||
124 | |||
125 | #endif // #ifndef ENDGAME_H_INCLUDED |