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
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.
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.
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/>.
21 #if !defined(MATERIAL_H_INCLUDED)
22 #define MATERIAL_H_INCLUDED
36 const int MaterialTableSize = 1024;
38 /// MaterialInfo is a class which contains various information about a
39 /// material configuration. It contains a material balance evaluation,
40 /// a function pointer to a special endgame evaluation function (which in
41 /// most cases is NULL, meaning that the standard evaluation function will
42 /// be used), and "scale factors" for black and white.
44 /// The scale factors are used to scale the evaluation score up or down.
45 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
46 /// of 4, which will result in scores of absolute value less than one pawn.
50 friend class MaterialInfoTable;
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;
64 EndgameEvaluationFunctionBase* evaluationFunction;
65 EndgameScalingFunctionBase* scalingFunction[2];
70 /// The MaterialInfoTable class represents a pawn hash table. It is basically
71 /// just an array of MaterialInfo objects and a few methods for accessing these
72 /// objects. The most important method is get_material_info, which looks up a
73 /// position in the table and returns a pointer to a MaterialInfo object.
74 class EndgameFunctions;
76 class MaterialInfoTable {
81 MaterialInfo* get_material_info(const Position& pos);
83 static Phase game_phase(const Position& pos);
86 MaterialInfo* entries;
87 EndgameFunctions* funcs;
96 /// MaterialInfo::material_value simply returns the material balance
97 /// evaluation that is independent from game phase.
99 inline Score MaterialInfo::material_value() const {
101 return make_score(value, value);
104 /// MaterialInfo::scale_factor takes a position and a color as input, and
105 /// returns a scale factor for the given color. We have to provide the
106 /// position in addition to the color, because the scale factor need not
107 /// to be a constant: It can also be a function which should be applied to
108 /// the position. For instance, in KBP vs K endgames, a scaling function
109 /// which checks for draws with rook pawns and wrong-colored bishops.
111 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
113 if (scalingFunction[c] != NULL)
115 ScaleFactor sf = scalingFunction[c]->apply(pos);
116 if (sf != SCALE_FACTOR_NONE)
119 return ScaleFactor(factor[c]);
123 /// MaterialInfo::space_weight() simply returns the weight for the space
124 /// evaluation for this material configuration.
126 inline int MaterialInfo::space_weight() const {
131 /// MaterialInfo::game_phase() returns the game phase according
132 /// to this material configuration.
134 inline Phase MaterialInfo::game_phase() const {
140 /// MaterialInfo::specialized_eval_exists decides whether there is a
141 /// specialized evaluation function for the current material configuration,
142 /// or if the normal evaluation function should be used.
144 inline bool MaterialInfo::specialized_eval_exists() const {
146 return evaluationFunction != NULL;
150 /// MaterialInfo::evaluate applies a specialized evaluation function
151 /// to a given position object. It should only be called when
152 /// specialized_eval_exists() returns 'true'.
154 inline Value MaterialInfo::evaluate(const Position& pos) const {
156 return evaluationFunction->apply(pos);
159 #endif // !defined(MATERIAL_H_INCLUDED)