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-2012 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/>.
20 #if !defined(MATERIAL_H_INCLUDED)
21 #define MATERIAL_H_INCLUDED
28 const int MaterialTableSize = 8192;
30 /// MaterialEntry is a class which contains various information about a
31 /// material configuration. It contains a material balance evaluation,
32 /// a function pointer to a special endgame evaluation function (which in
33 /// most cases is NULL, meaning that the standard evaluation function will
34 /// be used), and "scale factors" for black and white.
36 /// The scale factors are used to scale the evaluation score up or down.
37 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
38 /// of 4, which will result in scores of absolute value less than one pawn.
42 friend struct MaterialTable;
45 Score material_value() const;
46 ScaleFactor scale_factor(const Position& pos, Color c) const;
47 int space_weight() const;
48 Phase game_phase() const;
49 bool specialized_eval_exists() const;
50 Value evaluate(const Position& pos) const;
55 uint8_t factor[COLOR_NB];
56 EndgameBase<Value>* evaluationFunction;
57 EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB];
63 /// The MaterialTable class represents a material hash table. The most important
64 /// method is probe(), which returns a pointer to a MaterialEntry object.
66 struct MaterialTable {
68 MaterialEntry* probe(const Position& pos);
69 static Phase game_phase(const Position& pos);
70 template<Color Us> static int imbalance(const int pieceCount[][PIECE_TYPE_NB]);
72 HashTable<MaterialEntry, MaterialTableSize> entries;
77 /// MaterialEntry::scale_factor takes a position and a color as input, and
78 /// returns a scale factor for the given color. We have to provide the
79 /// position in addition to the color, because the scale factor need not
80 /// to be a constant: It can also be a function which should be applied to
81 /// the position. For instance, in KBP vs K endgames, a scaling function
82 /// which checks for draws with rook pawns and wrong-colored bishops.
84 inline ScaleFactor MaterialEntry::scale_factor(const Position& pos, Color c) const {
86 if (!scalingFunction[c])
87 return ScaleFactor(factor[c]);
89 ScaleFactor sf = (*scalingFunction[c])(pos);
90 return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf;
93 inline Value MaterialEntry::evaluate(const Position& pos) const {
94 return (*evaluationFunction)(pos);
97 inline Score MaterialEntry::material_value() const {
98 return make_score(value, value);
101 inline int MaterialEntry::space_weight() const {
105 inline Phase MaterialEntry::game_phase() const {
109 inline bool MaterialEntry::specialized_eval_exists() const {
110 return evaluationFunction != NULL;
113 #endif // !defined(MATERIAL_H_INCLUDED)