]> git.sesse.net Git - stockfish/blob - src/movepick.h
d3bca28a7ae982aa1cd619c3a581f532711a6020
[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 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.
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<typename T>
40 struct Stats {
41
42   static const Value Max = Value(1<<28);
43
44   const T* operator[](Piece pc) const { return table[pc]; }
45   T* operator[](Piece pc) { 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])
51         table[pc][to] = m;
52   }
53
54   void updateH(Piece pc, Square to, Value v) {
55
56     if (abs(int(v)) >= 324) 
57         return;
58     table[pc][to] -= table[pc][to] * abs(int(v)) / 324;
59     table[pc][to] += int(v) * 32;
60   }
61
62   void updateCMH(Piece pc, Square to, Value v) {
63
64     if (abs(int(v)) >= 324) 
65         return;
66     table[pc][to] -= table[pc][to] * abs(int(v)) / 512;
67     table[pc][to] += int(v) * 64;
68   }
69
70 private:
71   T table[PIECE_NB][SQUARE_NB];
72 };
73
74 typedef Stats<Value> HistoryStats;
75 typedef Stats<Move> MovesStats;
76 typedef Stats<HistoryStats> CounterMovesHistoryStats;
77
78
79 /// MovePicker class is used to pick one pseudo legal move at a time from the
80 /// current position. The most important method is next_move(), which returns a
81 /// new pseudo legal move each time it is called, until there are no moves left,
82 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
83 /// beta algorithm, MovePicker attempts to return the moves which are most likely
84 /// to get a cut-off first.
85
86 class MovePicker {
87 public:
88   MovePicker(const MovePicker&) = delete;
89   MovePicker& operator=(const MovePicker&) = delete;
90
91   MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMovesHistoryStats&, Square);
92   MovePicker(const Position&, Move, const HistoryStats&, const CounterMovesHistoryStats&, Value);
93   MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMovesHistoryStats&, Move, Search::Stack*);
94
95   Move next_move();
96
97 private:
98   template<GenType> void score();
99   void generate_next_stage();
100   ExtMove* begin() { return moves; }
101   ExtMove* end() { return endMoves; }
102
103   const Position& pos;
104   const HistoryStats& history;
105   const CounterMovesHistoryStats& counterMovesHistory;
106   Search::Stack* ss;
107   Move countermove;
108   Depth depth;
109   Move ttMove;
110   ExtMove killers[3];
111   Square recaptureSquare;
112   Value threshold;
113   int stage;
114   ExtMove *endQuiets, *endBadCaptures = moves + MAX_MOVES - 1;
115   ExtMove moves[MAX_MOVES], *cur = moves, *endMoves = moves;
116 };
117
118 #endif // #ifndef MOVEPICK_H_INCLUDED