2011-11-12 20:44:06 +01:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2015-02-01 01:46:09 +01:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
#ifndef MOVEPICK_H_INCLUDED
|
2011-11-12 20:44:06 +01:00
|
|
|
#define MOVEPICK_H_INCLUDED
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
#include <algorithm> // For std::max
|
2013-08-20 20:02:33 +02:00
|
|
|
#include <cstring> // For std::memset
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
#include "movegen.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
#include "position.h"
|
2012-01-01 01:52:19 +01:00
|
|
|
#include "search.h"
|
2011-11-12 20:44:06 +01:00
|
|
|
#include "types.h"
|
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
/// The Stats struct stores moves statistics. According to the template parameter
|
2015-10-23 22:58:14 +02:00
|
|
|
/// the class can store History and Countermoves. History records how often
|
2013-08-20 20:02:33 +02:00
|
|
|
/// different moves have been successful or unsuccessful during the current search
|
2015-10-23 22:58:14 +02:00
|
|
|
/// and is used for reduction and move ordering decisions.
|
2013-08-20 20:02:33 +02:00
|
|
|
/// Countermoves store the move that refute a previous one. Entries are stored
|
2014-05-31 14:23:03 +02:00
|
|
|
/// using only the moving piece and destination square, hence two moves with
|
2013-08-20 20:02:33 +02:00
|
|
|
/// different origin but same destination and piece will be considered identical.
|
2015-12-27 23:25:14 +01:00
|
|
|
template<typename T, bool CM = false>
|
2013-05-03 19:03:42 +02:00
|
|
|
struct Stats {
|
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
static const Value Max = Value(1 << 28);
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
const T* operator[](Piece pc) const { return table[pc]; }
|
2015-10-23 22:58:14 +02:00
|
|
|
T* operator[](Piece pc) { return table[pc]; }
|
2013-08-20 20:02:33 +02:00
|
|
|
void clear() { std::memset(table, 0, sizeof(table)); }
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
void update(Piece pc, Square to, Move m) {
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
if (m != table[pc][to])
|
|
|
|
table[pc][to] = m;
|
2013-08-20 20:02:33 +02:00
|
|
|
}
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
void update(Piece pc, Square to, Value v) {
|
2015-10-23 22:58:14 +02:00
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
if (abs(int(v)) >= 324)
|
2015-10-23 22:58:14 +02:00
|
|
|
return;
|
2013-05-03 19:03:42 +02:00
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
table[pc][to] -= table[pc][to] * abs(int(v)) / (CM ? 512 : 324);
|
|
|
|
table[pc][to] += int(v) * (CM ? 64 : 32);
|
2013-05-03 19:03:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-20 20:02:33 +02:00
|
|
|
T table[PIECE_NB][SQUARE_NB];
|
2013-05-03 19:03:42 +02:00
|
|
|
};
|
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
typedef Stats<Move> MovesStats;
|
2015-12-27 23:25:14 +01:00
|
|
|
typedef Stats<Value, false> HistoryStats;
|
|
|
|
typedef Stats<Value, true> CounterMovesStats;
|
|
|
|
typedef Stats<CounterMovesStats> CounterMovesHistoryStats;
|
2013-05-03 19:03:42 +02:00
|
|
|
|
|
|
|
|
2012-06-04 18:25:51 +02:00
|
|
|
/// MovePicker class is used to pick one pseudo legal move at a time from the
|
|
|
|
/// current position. The most important method is next_move(), which returns a
|
|
|
|
/// new pseudo legal move each time it is called, until there are no moves left,
|
|
|
|
/// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
|
|
|
|
/// beta algorithm, MovePicker attempts to return the moves which are most likely
|
|
|
|
/// to get a cut-off first.
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
class MovePicker {
|
|
|
|
public:
|
2015-10-23 22:58:14 +02:00
|
|
|
MovePicker(const MovePicker&) = delete;
|
|
|
|
MovePicker& operator=(const MovePicker&) = delete;
|
|
|
|
|
2015-12-27 23:25:14 +01:00
|
|
|
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
|
|
|
|
MovePicker(const Position&, Move, const HistoryStats&, Value);
|
|
|
|
MovePicker(const Position&, Move, Depth, const HistoryStats&, const CounterMovesStats&, Move, Search::Stack*);
|
2013-08-20 20:02:33 +02:00
|
|
|
|
2015-10-23 22:58:14 +02:00
|
|
|
Move next_move();
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
private:
|
2013-05-03 19:03:42 +02:00
|
|
|
template<GenType> void score();
|
2014-05-31 14:23:03 +02:00
|
|
|
void generate_next_stage();
|
2015-10-23 22:58:14 +02:00
|
|
|
ExtMove* begin() { return moves; }
|
|
|
|
ExtMove* end() { return endMoves; }
|
2011-11-12 20:44:06 +01:00
|
|
|
|
|
|
|
const Position& pos;
|
2013-08-20 20:02:33 +02:00
|
|
|
const HistoryStats& history;
|
2015-12-27 23:25:14 +01:00
|
|
|
const CounterMovesStats* counterMovesHistory;
|
2012-09-16 17:16:15 +02:00
|
|
|
Search::Stack* ss;
|
2015-10-23 22:58:14 +02:00
|
|
|
Move countermove;
|
2012-01-01 01:52:19 +01:00
|
|
|
Depth depth;
|
|
|
|
Move ttMove;
|
2015-10-23 22:58:14 +02:00
|
|
|
ExtMove killers[3];
|
2012-01-01 01:52:19 +01:00
|
|
|
Square recaptureSquare;
|
2015-10-23 22:58:14 +02:00
|
|
|
Value threshold;
|
2014-05-31 14:23:03 +02:00
|
|
|
int stage;
|
2015-10-23 22:58:14 +02:00
|
|
|
ExtMove *endQuiets, *endBadCaptures = moves + MAX_MOVES - 1;
|
|
|
|
ExtMove moves[MAX_MOVES], *cur = moves, *endMoves = moves;
|
2011-11-12 20:44:06 +01:00
|
|
|
};
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
#endif // #ifndef MOVEPICK_H_INCLUDED
|