]> git.sesse.net Git - stockfish/blob - src/movepick.h
Increased mobility array
[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-2013 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 MOVEPICK_H_INCLUDED
21 #define MOVEPICK_H_INCLUDED
22
23 #include <algorithm> // For std::max
24 #include <cstring>   // For 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 Refutations statistics. History records
34 /// how often different moves have been successful or unsuccessful during the
35 /// current search and is used for reduction and move ordering decisions. Gains
36 /// records the move's best evaluation gain from one ply to the next and is used
37 /// for pruning decisions. Refutations store the move that refute a previous one.
38 /// Entries are stored according only to moving piece and destination square, in
39 /// particular two moves with different origin but same destination and same piece
40 /// will be considered identical.
41 template<bool Gain, typename T>
42 struct Stats {
43
44   static const Value Max = Value(2000);
45
46   const T* operator[](Piece p) const { return &table[p][0]; }
47   void clear() { memset(table, 0, sizeof(table)); }
48
49   void update(Piece p, Square to, Move m) { table[p][to] = m; }
50   void update(Piece p, Square to, Value v) {
51
52     if (Gain)
53         table[p][to] = std::max(v, table[p][to] - 1);
54
55     else if (abs(table[p][to] + v) < Max)
56         table[p][to] +=  v;
57   }
58
59 private:
60   T table[PIECE_NB][SQUARE_NB];
61 };
62
63 typedef Stats<true, Value> Gains;
64 typedef Stats<false, Value> History;
65 typedef Stats<false, Move> Refutations;
66
67
68 /// MovePicker class is used to pick one pseudo legal move at a time from the
69 /// current position. The most important method is next_move(), which returns a
70 /// new pseudo legal move each time it is called, until there are no moves left,
71 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
72 /// beta algorithm, MovePicker attempts to return the moves which are most likely
73 /// to get a cut-off first.
74
75 class MovePicker {
76
77   MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
78
79 public:
80   MovePicker(const Position&, Move, Depth, const History&, const Refutations&, Search::Stack*, Value);
81   MovePicker(const Position&, Move, Depth, const History&, Square);
82   MovePicker(const Position&, Move, const History&, PieceType);
83   template<bool SpNode> Move next_move();
84
85 private:
86   template<GenType> void score();
87   void generate_next();
88
89   const Position& pos;
90   const History& Hist;
91   Search::Stack* ss;
92   Depth depth;
93   Move ttMove;
94   MoveStack killers[3];
95   Square recaptureSquare;
96   int captureThreshold, phase;
97   MoveStack *cur, *end, *endQuiets, *endBadCaptures;
98   MoveStack moves[MAX_MOVES];
99 };
100
101 #endif // !defined(MOVEPICK_H_INCLUDED)