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