]> git.sesse.net Git - stockfish/blob - src/movepick.h
Sync with master
[stockfish] / src / movepick.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-2015 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 #ifndef MOVEPICK_H_INCLUDED
21 #define MOVEPICK_H_INCLUDED
22
23 #include <algorithm> // For std::max
24 #include <cstring>   // For std::memset
25
26 #include "movegen.h"
27 #include "position.h"
28 #include "search.h"
29 #include "types.h"
30
31
32 /// The Stats struct stores moves statistics. According to the template parameter
33 /// the class can store History, Gains and Countermoves. History records how often
34 /// different moves have been successful or unsuccessful during the current search
35 /// and is used for reduction and move ordering decisions. Gains records the move's
36 /// best evaluation gain from one ply to the next and is used for pruning decisions.
37 /// Countermoves store the move that refute a previous one. Entries are stored
38 /// using only the moving piece and destination square, hence two moves with
39 /// different origin but same destination and piece will be considered identical.
40 template<bool Gain, typename T>
41 struct Stats {
42
43   static const Value Max = Value(250);
44
45   const T* operator[](Piece pc) const { return table[pc]; }
46   void clear() { std::memset(table, 0, sizeof(table)); }
47
48   void update(Piece pc, Square to, Move m) {
49
50     if (m == table[pc][to].first)
51         return;
52
53     table[pc][to].second = table[pc][to].first;
54     table[pc][to].first = m;
55   }
56
57   void update(Piece pc, Square to, Value v) {
58
59     if (Gain)
60         table[pc][to] = std::max(v, table[pc][to] - 1);
61
62     else if (abs(table[pc][to] + v) < Max)
63         table[pc][to] +=  v;
64   }
65
66 private:
67   T table[PIECE_NB][SQUARE_NB];
68 };
69
70 typedef Stats< true, Value> GainsStats;
71 typedef Stats<false, Value> HistoryStats;
72 typedef Stats<false, std::pair<Move, Move> > MovesStats;
73
74
75 /// MovePicker class is used to pick one pseudo legal move at a time from the
76 /// current position. The most important method is next_move(), which returns a
77 /// new pseudo legal move each time it is called, until there are no moves left,
78 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
79 /// beta algorithm, MovePicker attempts to return the moves which are most likely
80 /// to get a cut-off first.
81
82 class MovePicker {
83 public:
84   MovePicker(const MovePicker&) = delete;
85   MovePicker& operator=(const MovePicker&) = delete;
86
87   MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
88   MovePicker(const Position&, Move, const HistoryStats&, PieceType);
89   MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Move*, Search::Stack*);
90
91   template<bool SpNode> Move next_move();
92
93 private:
94   template<GenType> void score();
95   void generate_next_stage();
96   ExtMove* begin() { return moves; }
97   ExtMove* end() { return endMoves; }
98
99   const Position& pos;
100   const HistoryStats& history;
101   Search::Stack* ss;
102   Move* countermoves;
103   Move* followupmoves;
104   Depth depth;
105   Move ttMove;
106   ExtMove killers[6];
107   Square recaptureSquare;
108   Value captureThreshold;
109   int stage;
110   ExtMove *endQuiets, *endBadCaptures;
111   ExtMove moves[MAX_MOVES], *cur = moves, *endMoves = moves;
112 };
113
114 #endif // #ifndef MOVEPICK_H_INCLUDED