]> git.sesse.net Git - stockfish/blob - src/material.h
Introduce enum SquareColor
[stockfish] / src / material.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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
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(MATERIAL_H_INCLUDED)
22 #define MATERIAL_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "endgame.h"
29 #include "position.h"
30
31
32 ////
33 //// Types
34 ////
35
36 /// MaterialInfo is a class which contains various information about a
37 /// material configuration. It contains a material balance evaluation,
38 /// a function pointer to a special endgame evaluation function (which in
39 /// most cases is NULL, meaning that the standard evaluation function will
40 /// be used), and "scale factors" for black and white.
41 ///
42 /// The scale factors are used to scale the evaluation score up or down.
43 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
44 /// of 4, which will result in scores of absolute value less than one pawn.
45
46 class MaterialInfo {
47
48   friend class MaterialInfoTable;
49
50 public:
51   MaterialInfo() : key(0) { clear(); }
52
53   Score material_value() const;
54   ScaleFactor scale_factor(const Position& pos, Color c) const;
55   int space_weight() const;
56   Phase game_phase() const;
57   bool specialized_eval_exists() const;
58   Value evaluate(const Position& pos) const;
59
60 private:
61   inline void clear();
62
63   Key key;
64   int16_t value;
65   uint8_t factor[2];
66   EndgameEvaluationFunctionBase* evaluationFunction;
67   EndgameScalingFunctionBase* scalingFunction[2];
68   int spaceWeight;
69   Phase gamePhase;
70 };
71
72 /// The MaterialInfoTable class represents a pawn hash table. It is basically
73 /// just an array of MaterialInfo objects and a few methods for accessing these
74 /// objects. The most important method is get_material_info, which looks up a
75 /// position in the table and returns a pointer to a MaterialInfo object.
76 class EndgameFunctions;
77
78 class MaterialInfoTable {
79
80 public:
81   MaterialInfoTable(unsigned numOfEntries);
82   ~MaterialInfoTable();
83   MaterialInfo* get_material_info(const Position& pos);
84
85   static Phase game_phase(const Position& pos);
86
87 private:
88   unsigned size;
89   MaterialInfo* entries;
90   EndgameFunctions* funcs;
91 };
92
93
94 ////
95 //// Inline functions
96 ////
97
98
99 /// MaterialInfo::material_value simply returns the material balance
100 /// evaluation that is independent from game phase.
101
102 inline Score MaterialInfo::material_value() const {
103
104   return make_score(value, value);
105 }
106
107
108 /// MaterialInfo::clear() resets a MaterialInfo object to an empty state,
109 /// with all slots at their default values but the key.
110
111 inline void MaterialInfo::clear() {
112
113   value = 0;
114   factor[WHITE] = factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
115   evaluationFunction = NULL;
116   scalingFunction[WHITE] = scalingFunction[BLACK] = NULL;
117   spaceWeight = 0;
118 }
119
120
121 /// MaterialInfo::scale_factor takes a position and a color as input, and
122 /// returns a scale factor for the given color. We have to provide the
123 /// position in addition to the color, because the scale factor need not
124 /// to be a constant: It can also be a function which should be applied to
125 /// the position. For instance, in KBP vs K endgames, a scaling function
126 /// which checks for draws with rook pawns and wrong-colored bishops.
127
128 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
129
130   if (scalingFunction[c] != NULL)
131   {
132       ScaleFactor sf = scalingFunction[c]->apply(pos);
133       if (sf != SCALE_FACTOR_NONE)
134           return sf;
135   }
136   return ScaleFactor(factor[c]);
137 }
138
139
140 /// MaterialInfo::space_weight() simply returns the weight for the space
141 /// evaluation for this material configuration.
142
143 inline int MaterialInfo::space_weight() const {
144
145   return spaceWeight;
146 }
147
148 /// MaterialInfo::game_phase() returns the game phase according
149 /// to this material configuration.
150
151 inline Phase MaterialInfo::game_phase() const {
152
153   return gamePhase;
154 }
155
156
157 /// MaterialInfo::specialized_eval_exists decides whether there is a
158 /// specialized evaluation function for the current material configuration,
159 /// or if the normal evaluation function should be used.
160
161 inline bool MaterialInfo::specialized_eval_exists() const {
162
163   return evaluationFunction != NULL;
164 }
165
166
167 /// MaterialInfo::evaluate applies a specialized evaluation function
168 /// to a given position object. It should only be called when
169 /// specialized_eval_exists() returns 'true'.
170
171 inline Value MaterialInfo::evaluate(const Position& pos) const {
172
173   return evaluationFunction->apply(pos);
174 }
175
176 #endif // !defined(MATERIAL_H_INCLUDED)