]> git.sesse.net Git - stockfish/blob - src/pawns.h
Assorted code style and comments in pawns.cpp and pawns.h
[stockfish] / src / pawns.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 #if !defined(PAWNS_H_INCLUDED)
21 #define PAWNS_H_INCLUDED
22
23 #include "position.h"
24 #include "tt.h"
25 #include "types.h"
26
27 const int PawnTableSize = 16384;
28
29 /// PawnInfo is a class which contains various information about a pawn
30 /// structure. Currently, it only includes a middle game and an end game
31 /// pawn structure evaluation, and a bitboard of passed pawns. We may want
32 /// to add further information in the future. A lookup to the pawn hash table
33 /// (performed by calling the get_pawn_info method in a PawnInfoTable object)
34 /// returns a pointer to a PawnInfo object.
35 class PawnInfo {
36
37   friend class PawnInfoTable;
38
39 public:
40   Score pawns_value() const;
41   Bitboard pawn_attacks(Color c) const;
42   Bitboard passed_pawns(Color c) const;
43   int file_is_half_open(Color c, File f) const;
44   int has_open_file_to_left(Color c, File f) const;
45   int has_open_file_to_right(Color c, File f) const;
46
47   template<Color Us>
48   Score king_shelter(const Position& pos, Square ksq);
49
50 private:
51   template<Color Us>
52   Score updateShelter(const Position& pos, Square ksq);
53
54   Key key;
55   Bitboard passedPawns[2];
56   Bitboard pawnAttacks[2];
57   Square kingSquares[2];
58   Score value;
59   int halfOpenFiles[2];
60   Score kingShelters[2];
61 };
62
63
64 /// The PawnInfoTable class represents a pawn hash table. The most important
65 /// method is get_pawn_info, which returns a pointer to a PawnInfo object.
66
67 class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
68 public:
69   PawnInfo* get_pawn_info(const Position& pos) const;
70
71 private:
72   template<Color Us>
73   Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
74 };
75
76
77 inline Score PawnInfo::pawns_value() const {
78   return value;
79 }
80
81 inline Bitboard PawnInfo::pawn_attacks(Color c) const {
82   return pawnAttacks[c];
83 }
84
85 inline Bitboard PawnInfo::passed_pawns(Color c) const {
86   return passedPawns[c];
87 }
88
89 inline int PawnInfo::file_is_half_open(Color c, File f) const {
90   return halfOpenFiles[c] & (1 << int(f));
91 }
92
93 inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
94   return halfOpenFiles[c] & ((1 << int(f)) - 1);
95 }
96
97 inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
98   return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
99 }
100
101 template<Color Us>
102 inline Score PawnInfo::king_shelter(const Position& pos, Square ksq) {
103   return kingSquares[Us] == ksq ? kingShelters[Us] : updateShelter<Us>(pos, ksq);
104 }
105
106 #endif // !defined(PAWNS_H_INCLUDED)