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 Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef MOVEPICK_H_INCLUDED
22 #define MOVEPICK_H_INCLUDED
30 /// StatBoards is a generic 2-dimensional array used to store various statistics
31 template<int Size1, int Size2, typename T = int16_t>
32 struct StatBoards : public std::array<std::array<T, Size2>, Size1> {
34 void fill(const T& v) {
35 T* p = &(*this)[0][0];
36 std::fill(p, p + sizeof(*this) / sizeof(*p), v);
39 void update(T& entry, int bonus, const int D) {
42 int v = entry + bonus * 32 - entry * abs(bonus) / D;
43 return INT16_MIN < v && v < INT16_MAX;
46 assert(abs(bonus) <= D); // Consistency check for below formula
48 entry += bonus * 32 - entry * abs(bonus) / D;
50 assert(abs(entry) <= 32 * D);
54 /// ButterflyBoards are 2 tables (one for each color) indexed by the move's from
55 /// and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards
56 typedef StatBoards<COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyBoards;
58 /// PieceToBoards are addressed by a move's [piece][to] information
59 typedef StatBoards<PIECE_NB, SQUARE_NB> PieceToBoards;
61 /// ButterflyHistory records how often quiet moves have been successful or
62 /// unsuccessful during the current search, and is used for reduction and move
63 /// ordering decisions. It uses ButterflyBoards as backing store.
64 struct ButterflyHistory : public ButterflyBoards {
66 void update(Color c, Move m, int bonus) {
67 StatBoards::update((*this)[c][from_to(m)], bonus, 324);
71 /// PieceToHistory is like ButterflyHistory, but is based on PieceToBoards
72 struct PieceToHistory : public PieceToBoards {
74 void update(Piece pc, Square to, int bonus) {
75 StatBoards::update((*this)[pc][to], bonus, 936);
79 /// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
80 /// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
81 typedef StatBoards<PIECE_NB, SQUARE_NB, Move> CounterMoveHistory;
83 /// ContinuationHistory is the history of a given pair of moves, usually the
84 /// current one given a previous one. History table is based on PieceToBoards
85 /// instead of ButterflyBoards.
86 typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> ContinuationHistory;
89 /// MovePicker class is used to pick one pseudo legal move at a time from the
90 /// current position. The most important method is next_move(), which returns a
91 /// new pseudo legal move each time it is called, until there are no moves left,
92 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
93 /// beta algorithm, MovePicker attempts to return the moves which are most likely
94 /// to get a cut-off first.
98 MovePicker(const MovePicker&) = delete;
99 MovePicker& operator=(const MovePicker&) = delete;
100 MovePicker(const Position&, Move, Value);
101 MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Square);
102 MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Move, Move*);
103 Move next_move(bool skipQuiets = false);
106 template<GenType> void score();
107 ExtMove* begin() { return cur; }
108 ExtMove* end() { return endMoves; }
111 const ButterflyHistory* mainHistory;
112 const PieceToHistory** contHistory;
113 Move ttMove, countermove, killers[2];
114 ExtMove *cur, *endMoves, *endBadCaptures;
116 Square recaptureSquare;
119 ExtMove moves[MAX_MOVES];
122 #endif // #ifndef MOVEPICK_H_INCLUDED