io-chess
UCI chess engine
Loading...
Searching...
No Matches
MovePicker.h
Go to the documentation of this file.
1#pragma once
2
10
11#include "ISearch.h"
12#include <array>
13
14// Forward declarations
15class Negamax;
16
27private:
29
30 // === OPTIMIZATION: Fixed-size arrays to avoid dynamic allocations ===
31 // Max legal moves in any chess position is 218, but typical positions have <50
32 static constexpr int MAX_MOVES = 256;
33
34 std::array<Move, MAX_MOVES> &captures_;
35 std::array<int, MAX_MOVES> &captureScores_;
36 int captureCount_ = 0;
37
38 std::array<Move, 2> killerMoves_;
39 int killerCount_ = 0;
40
41 std::array<Move, MAX_MOVES> &quiets_;
42 std::array<int, MAX_MOVES> &quietScores_;
43 int quietCount_ = 0;
44
47
48 const std::array<std::array<Move, 2>, SearchConstants::MAX_PLY> *killers_;
49 const std::array<std::array<int, 64>, 64> *history_;
50 const std::array<std::array<int, 64>, 16> *captureHist_;
51
52 int ply_;
54
55 int nextCapture_ = 0;
56 int nextKiller_ = 0;
57 int nextQuiet_ = 0;
58
59 std::array<Move, MAX_MOVES> &badCaptures_;
62
63 bool capturesGenerated_ = false;
64 bool killersGenerated_ = false;
65 bool quietsGenerated_ = false;
66
69
70 bool countermoveReturned_ = false;
71 int phase_ = 0;
72
73public:
92 const Movelist &moves, Move ttMove,
93 const std::array<std::array<Move, 2>, SearchConstants::MAX_PLY> *killers,
94 const std::array<std::array<int, 64>, 64> *history,
95 const std::array<std::array<int, 64>, 16> *captureHist,
96 std::array<Move, MAX_MOVES> &captures,
97 std::array<int, MAX_MOVES> &captureScores,
98 std::array<Move, MAX_MOVES> &quiets,
99 std::array<int, MAX_MOVES> &quietScores,
100 std::array<Move, MAX_MOVES> &badCaptures,
101 int ply, int threadId = 0, Move countermove = Move(0))
102 : allMoves_(moves), captures_(captures), captureScores_(captureScores),
103 quiets_(quiets), quietScores_(quietScores),
104 badCaptures_(badCaptures), ttMove_(ttMove),
105 countermove_(countermove),
106 killers_(killers), history_(history), captureHist_(captureHist),
107 ply_(ply), threadId_(threadId) {}
108
119 Move nextMove(const Board &board, const Negamax *search, bool inCheck);
120
121private:
122 int scoreMove(const Move &move, const Board &board, const Negamax *search,
123 bool isCapture);
124 void generateAndScoreCaptures(const Board &board, const Negamax *search);
125 void generateAndScoreKillers(const Board &board, const Negamax *search);
126 void generateAndScoreQuiets(const Board &board, const Negamax *search);
127
128 template<size_t N>
129 Move selectBestMove(std::array<Move, N> &list, std::array<int, N> &scores,
130 int &nextIdx, int count);
131};
Search interface and shared data structures.
chess::Move Move
Alias for chess::Move.
Definition Types.h:15
chess::Movelist Movelist
Alias for chess::Movelist.
Definition Types.h:16
chess::Board Board
Alias for chess::Board.
Definition Types.h:14
int nextQueenPromo_
Index of the next queen promotion to return.
Definition MovePicker.h:67
std::array< Move, MAX_MOVES > & quiets_
External buffer for quiet moves.
Definition MovePicker.h:41
int threadId_
Thread ID for diversified move ordering in Lazy SMP.
Definition MovePicker.h:53
void generateAndScoreQuiets(const Board &board, const Negamax *search)
Definition MovePicker.cpp:286
bool quietsGenerated_
True if quiet moves have been scored and sorted.
Definition MovePicker.h:65
bool capturesGenerated_
True if capture moves have been scored and sorted.
Definition MovePicker.h:63
std::array< int, MAX_MOVES > & quietScores_
External buffer for quiet scores.
Definition MovePicker.h:42
std::array< Move, MAX_MOVES > & captures_
External buffer for capture moves.
Definition MovePicker.h:34
int scoreMove(const Move &move, const Board &board, const Negamax *search, bool isCapture)
Definition MovePicker.cpp:17
int ply_
Current distance from root.
Definition MovePicker.h:52
void generateAndScoreKillers(const Board &board, const Negamax *search)
Definition MovePicker.cpp:278
Move ttMove_
Best move retrieved from the Transposition Table (searched first).
Definition MovePicker.h:45
const Movelist & allMoves_
Reference to all legal moves generated for the current node.
Definition MovePicker.h:28
int captureCount_
Number of captures populated.
Definition MovePicker.h:36
int nextQuiet_
Index of the next quiet move to return.
Definition MovePicker.h:57
bool killersGenerated_
True if killer moves have been extracted.
Definition MovePicker.h:64
int nextKiller_
Index of the next killer to return.
Definition MovePicker.h:56
void generateAndScoreCaptures(const Board &board, const Negamax *search)
Definition MovePicker.cpp:220
int quietCount_
Number of quiets populated.
Definition MovePicker.h:43
Move countermove_
Countermove heuristic response to the opponent's previous move.
Definition MovePicker.h:46
int nextBadCapture_
Index of the next bad capture to return.
Definition MovePicker.h:61
Move nextMove(const Board &board, const Negamax *search, bool inCheck)
Returns the next best move according to the current phase.
Definition MovePicker.cpp:90
int killerCount_
Number of valid killer moves found.
Definition MovePicker.h:39
const std::array< std::array< Move, 2 >, SearchConstants::MAX_PLY > * killers_
Pointer to global killer heuristic table.
Definition MovePicker.h:48
const std::array< std::array< int, 64 >, 16 > * captureHist_
Pointer to global capture history table.
Definition MovePicker.h:50
int phase_
Current phase: 0: Promos, 1: TT, 2: Captures, 3: Killers, 4: Countermove, 5: Quiets,...
Definition MovePicker.h:71
std::array< Move, MAX_MOVES > & badCaptures_
External buffer for captures that fail SEE.
Definition MovePicker.h:59
int badCaptureCount_
Number of bad captures found.
Definition MovePicker.h:60
const std::array< std::array< int, 64 >, 64 > * history_
Pointer to global history heuristic table.
Definition MovePicker.h:49
std::array< int, MAX_MOVES > & captureScores_
External buffer for capture scores.
Definition MovePicker.h:35
MovePicker(const Movelist &moves, Move ttMove, const std::array< std::array< Move, 2 >, SearchConstants::MAX_PLY > *killers, const std::array< std::array< int, 64 >, 64 > *history, const std::array< std::array< int, 64 >, 16 > *captureHist, std::array< Move, MAX_MOVES > &captures, std::array< int, MAX_MOVES > &captureScores, std::array< Move, MAX_MOVES > &quiets, std::array< int, MAX_MOVES > &quietScores, std::array< Move, MAX_MOVES > &badCaptures, int ply, int threadId=0, Move countermove=Move(0))
Constructs a MovePicker for a specific search node.
Definition MovePicker.h:91
int queenPromoCount_
Number of queen promotions found.
Definition MovePicker.h:68
std::array< Move, 2 > killerMoves_
Local cache of killer moves for this ply.
Definition MovePicker.h:38
bool countermoveReturned_
True if the countermove has already been returned.
Definition MovePicker.h:70
int nextCapture_
Index of the next capture to return.
Definition MovePicker.h:55
static constexpr int MAX_MOVES
Maximum allowed moves, aligned to power of 2 for safety.
Definition MovePicker.h:32
Move selectBestMove(std::array< Move, N > &list, std::array< int, N > &scores, int &nextIdx, int count)
Definition MovePicker.cpp:297
Implementation of the main Alpha-Beta search algorithm.
Definition Negamax.h:33
constexpr int MAX_PLY
Maximum supported plies from root.
Definition Types.h:49