]> git.sesse.net Git - stockfish/blob - src/endgame.h
Use templates for end game evaluation functions
[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 Marco Costalba
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #if !defined(ENDGAME_H_INCLUDED)
22 #define ENDGAME_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "position.h"
29 #include "scale.h"
30 #include "value.h"
31
32
33 ////
34 //// Types
35 ////
36
37 /// Abstract base class for all special endgame evaluation functions
38
39 class EndgameEvaluationFunction {
40 public:
41   EndgameEvaluationFunction(Color c);
42   virtual ~EndgameEvaluationFunction() { }
43
44   virtual Value apply(const Position &pos) = 0;
45
46 protected:
47   Color strongerSide, weakerSide;
48 };
49
50
51 /// Template subclass for various concrete endgames
52
53 enum EndgameType {
54     KXK,   // Generic "mate lone king" eval
55     KBNK,  // KBN vs K
56     KPK,   // KP vs K
57     KRKP,  // KR vs KP
58     KRKB,  // KR vs KB
59     KRKN,  // KR vs KN
60     KQKR,  // KQ vs KR
61     KBBKN, // KBB vs KN
62     KmmKm  // K and two minors vs K and one or two minors
63 };
64
65 template<EndgameType>
66 class EvaluationFunction : public EndgameEvaluationFunction {
67 public:
68   explicit EvaluationFunction(Color c): EndgameEvaluationFunction(c) {}
69   Value apply(const Position& pos);
70 };
71
72 /// Abstract base class for all evaluation scaling functions:
73
74 class ScalingFunction {
75 public:
76   ScalingFunction(Color c);
77   virtual ~ScalingFunction() { }
78
79   virtual ScaleFactor apply(const Position &pos) =0;
80
81 protected:
82   Color strongerSide, weakerSide;
83 };
84
85
86 /// Subclasses for various concrete endgames:
87
88 // KBP vs K:
89 class KBPKScalingFunction : public ScalingFunction {
90 public:
91   KBPKScalingFunction(Color c);
92   ScaleFactor apply(const Position &pos);
93 };
94
95 // KQ vs KRP:
96 class KQKRPScalingFunction: public ScalingFunction {
97 public:
98   KQKRPScalingFunction(Color c);
99   ScaleFactor apply(const Position &pos);
100 };
101
102 // KRP vs KR:
103 class KRPKRScalingFunction : public ScalingFunction {
104 public:
105   KRPKRScalingFunction(Color c);
106   ScaleFactor apply(const Position &pos);
107 };
108
109 // KRPP vs KRP:
110 class KRPPKRPScalingFunction : public ScalingFunction {
111 public:
112   KRPPKRPScalingFunction(Color c);
113   ScaleFactor apply(const Position &pos);
114 };
115
116 // King and pawns vs king:
117 class KPsKScalingFunction : public ScalingFunction {
118 public:
119   KPsKScalingFunction(Color c);
120   ScaleFactor apply(const Position &pos);
121 };
122
123 // KBP vs KB:
124 class KBPKBScalingFunction : public ScalingFunction {
125 public:
126   KBPKBScalingFunction(Color c);
127   ScaleFactor apply(const Position &pos);
128 };
129
130 // KBP vs KN:
131 class KBPKNScalingFunction : public ScalingFunction {
132 public:
133   KBPKNScalingFunction(Color c);
134   ScaleFactor apply(const Position &pos);
135 };
136
137 // KNP vs K:
138 class KNPKScalingFunction : public ScalingFunction {
139 public:
140   KNPKScalingFunction(Color c);
141   ScaleFactor apply(const Position &pos);
142 };
143
144 // KP vs KP:
145 class KPKPScalingFunction : public ScalingFunction {
146 public:
147   KPKPScalingFunction(Color c);
148   ScaleFactor apply(const Position &pos);
149 };
150
151
152 ////
153 //// Constants and variables
154 ////
155
156 extern EvaluationFunction<KXK> EvaluateKXK, EvaluateKKX;       // Generic "mate lone king" eval
157 extern EvaluationFunction<KBNK> EvaluateKBNK, EvaluateKKBN;    // KBN vs K
158 extern EvaluationFunction<KPK> EvaluateKPK, EvaluateKKP;       // KP vs K
159 extern EvaluationFunction<KRKP> EvaluateKRKP, EvaluateKPKR;    // KR vs KP
160 extern EvaluationFunction<KRKB> EvaluateKRKB, EvaluateKBKR;    // KR vs KB
161 extern EvaluationFunction<KRKN> EvaluateKRKN, EvaluateKNKR;    // KR vs KN
162 extern EvaluationFunction<KQKR> EvaluateKQKR, EvaluateKRKQ;    // KQ vs KR
163 extern EvaluationFunction<KBBKN> EvaluateKBBKN, EvaluateKNKBB; // KBB vs KN
164 extern EvaluationFunction<KmmKm> EvaluateKmmKm; // K and two minors vs K and one or two minors:
165
166 // KBP vs K:
167 extern KBPKScalingFunction ScaleKBPK, ScaleKKBP;
168
169 // KQ vs KRP:
170 extern KQKRPScalingFunction ScaleKQKRP, ScaleKRPKQ;
171
172 // KRP vs KR:
173 extern KRPKRScalingFunction ScaleKRPKR, ScaleKRKRP;
174
175 // KRPP vs KRP:
176 extern KRPPKRPScalingFunction ScaleKRPPKRP, ScaleKRPKRPP;
177
178 // King and pawns vs king:
179 extern KPsKScalingFunction ScaleKPsK, ScaleKKPs;
180
181 // KBP vs KB:
182 extern KBPKBScalingFunction ScaleKBPKB, ScaleKBKBP;
183
184 // KBP vs KN:
185 extern KBPKNScalingFunction ScaleKBPKN, ScaleKNKBP;
186
187 // KNP vs K:
188 extern KNPKScalingFunction ScaleKNPK, ScaleKKNP;
189
190 // KP vs KP:
191 extern KPKPScalingFunction ScaleKPKPw, ScaleKPKPb;
192
193
194 ////
195 //// Prototypes
196 ////
197
198 extern void init_bitbases();
199
200
201 #endif // !defined(ENDGAME_H_INCLUDED)