Rev 169 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 169 | Rev 185 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
5 | Copyright (C) 2015- |
5 | Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
6 | 6 | ||
7 | Stockfish is free software: you can redistribute it and/or modify |
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 |
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 |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. |
10 | (at your option) any later version. |
Line 62... | Line 62... | ||
62 | }; |
62 | }; |
63 | 63 | ||
64 | 64 | ||
65 | /// Endgame functions can be of two types depending on whether they return a |
65 | /// Endgame functions can be of two types depending on whether they return a |
66 | /// Value or a ScaleFactor. |
66 | /// Value or a ScaleFactor. |
- | 67 | ||
67 | template<EndgameCode E> using |
68 | template<EndgameCode E> using |
68 | eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type; |
69 | eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type; |
69 | 70 | ||
70 | 71 | ||
71 | /// Base and derived functors for endgame evaluation and scaling functions |
72 | /// Base and derived functors for endgame evaluation and scaling functions |
72 | 73 | ||
73 | template<typename T> |
74 | template<typename T> |
74 | struct EndgameBase { |
75 | struct EndgameBase { |
75 | 76 | ||
76 | explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {} |
77 | explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {} |
77 | virtual ~EndgameBase() = default; |
78 | virtual ~EndgameBase() = default; |
Line 86... | Line 87... | ||
86 | 87 | ||
87 | explicit Endgame(Color c) : EndgameBase<T>(c) {} |
88 | explicit Endgame(Color c) : EndgameBase<T>(c) {} |
88 | T operator()(const Position&) const override; |
89 | T operator()(const Position&) const override; |
89 | }; |
90 | }; |
90 | 91 | ||
91 | 92 | ||
92 | /// The Endgames class stores the pointers to endgame evaluation and scaling |
93 | /// The Endgames class stores the pointers to endgame evaluation and scaling |
93 | /// base objects in two std::map. We use polymorphism to invoke the actual |
94 | /// base objects in two std::map. We use polymorphism to invoke the actual |
94 | /// endgame function by calling its virtual operator(). |
95 | /// endgame function by calling its virtual operator(). |
95 | 96 | ||
96 | class Endgames { |
97 | class Endgames { |
Line 101... | Line 102... | ||
101 | template<typename T> |
102 | template<typename T> |
102 | Map<T>& map() { |
103 | Map<T>& map() { |
103 | return std::get<std::is_same<T, ScaleFactor>::value>(maps); |
104 | return std::get<std::is_same<T, ScaleFactor>::value>(maps); |
104 | } |
105 | } |
105 | 106 | ||
106 | template<EndgameCode E, typename T = eg_type<E |
107 | template<EndgameCode E, typename T = eg_type<E>> |
107 | void add(const std::string& code) { |
108 | void add(const std::string& code) { |
108 | 109 | ||
109 | StateInfo st; |
110 | StateInfo st; |
110 | map<T>()[Position().set(code, WHITE, &st).material_key()] = |
111 | map<T>()[Position().set(code, WHITE, &st).material_key()] = Ptr<T>(new Endgame<E>(WHITE)); |
111 | map<T>()[Position().set(code, BLACK, &st).material_key()] = |
112 | map<T>()[Position().set(code, BLACK, &st).material_key()] = Ptr<T>(new Endgame<E>(BLACK)); |
112 | } |
113 | } |
113 | 114 | ||
114 | std::pair<Map<Value>, Map<ScaleFactor>> maps; |
115 | std::pair<Map<Value>, Map<ScaleFactor>> maps; |
115 | 116 | ||
116 | public: |
117 | public: |
117 | Endgames() |
118 | Endgames() { |
- | 119 | ||
- | 120 | add<KPK>("KPK"); |
|
- | 121 | add<KNNK>("KNNK"); |
|
- | 122 | add<KBNK>("KBNK"); |
|
- | 123 | add<KRKP>("KRKP"); |
|
- | 124 | add<KRKB>("KRKB"); |
|
- | 125 | add<KRKN>("KRKN"); |
|
- | 126 | add<KQKP>("KQKP"); |
|
- | 127 | add<KQKR>("KQKR"); |
|
- | 128 | ||
- | 129 | add<KNPK>("KNPK"); |
|
- | 130 | add<KNPKB>("KNPKB"); |
|
- | 131 | add<KRPKR>("KRPKR"); |
|
- | 132 | add<KRPKB>("KRPKB"); |
|
- | 133 | add<KBPKB>("KBPKB"); |
|
- | 134 | add<KBPKN>("KBPKN"); |
|
- | 135 | add<KBPPKB>("KBPPKB"); |
|
- | 136 | add<KRPPKRP>("KRPPKRP"); |
|
- | 137 | } |
|
118 | 138 | ||
119 | template<typename T> |
139 | template<typename T> |
120 | EndgameBase<T>* probe(Key key) { |
140 | const EndgameBase<T>* probe(Key key) { |
121 | return map<T>().count(key) ? map<T>()[key].get() : nullptr; |
141 | return map<T>().count(key) ? map<T>()[key].get() : nullptr; |
122 | } |
142 | } |
123 | }; |
143 | }; |
124 | 144 | ||
125 | #endif // #ifndef ENDGAME_H_INCLUDED |
145 | #endif // #ifndef ENDGAME_H_INCLUDED |