io-chess
UCI chess engine
Loading...
Searching...
No Matches
MovePicker Class Reference

Lazily selects the best move at each step of the search. More...

#include <MovePicker.h>

Public Member Functions

 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.
Move nextMove (const Board &board, const Negamax *search, bool inCheck)
 Returns the next best move according to the current phase.

Private Member Functions

int scoreMove (const Move &move, const Board &board, const Negamax *search, bool isCapture)
void generateAndScoreCaptures (const Board &board, const Negamax *search)
void generateAndScoreKillers (const Board &board, const Negamax *search)
void generateAndScoreQuiets (const Board &board, const Negamax *search)
template<size_t N>
Move selectBestMove (std::array< Move, N > &list, std::array< int, N > &scores, int &nextIdx, int count)

Private Attributes

const MovelistallMoves_
 Reference to all legal moves generated for the current node.
std::array< Move, MAX_MOVES > & captures_
 External buffer for capture moves.
std::array< int, MAX_MOVES > & captureScores_
 External buffer for capture scores.
int captureCount_ = 0
 Number of captures populated.
std::array< Move, 2 > killerMoves_
 Local cache of killer moves for this ply.
int killerCount_ = 0
 Number of valid killer moves found.
std::array< Move, MAX_MOVES > & quiets_
 External buffer for quiet moves.
std::array< int, MAX_MOVES > & quietScores_
 External buffer for quiet scores.
int quietCount_ = 0
 Number of quiets populated.
Move ttMove_
 Best move retrieved from the Transposition Table (searched first).
Move countermove_
 Countermove heuristic response to the opponent's previous move.
const std::array< std::array< Move, 2 >, SearchConstants::MAX_PLY > * killers_
 Pointer to global killer heuristic table.
const std::array< std::array< int, 64 >, 64 > * history_
 Pointer to global history heuristic table.
const std::array< std::array< int, 64 >, 16 > * captureHist_
 Pointer to global capture history table.
int ply_
 Current distance from root.
int threadId_
 Thread ID for diversified move ordering in Lazy SMP.
int nextCapture_ = 0
 Index of the next capture to return.
int nextKiller_ = 0
 Index of the next killer to return.
int nextQuiet_ = 0
 Index of the next quiet move to return.
std::array< Move, MAX_MOVES > & badCaptures_
 External buffer for captures that fail SEE.
int badCaptureCount_ = 0
 Number of bad captures found.
int nextBadCapture_ = 0
 Index of the next bad capture to return.
bool capturesGenerated_ = false
 True if capture moves have been scored and sorted.
bool killersGenerated_ = false
 True if killer moves have been extracted.
bool quietsGenerated_ = false
 True if quiet moves have been scored and sorted.
int nextQueenPromo_ = 0
 Index of the next queen promotion to return.
int queenPromoCount_ = 0
 Number of queen promotions found.
bool countermoveReturned_ = false
 True if the countermove has already been returned.
int phase_ = 0
 Current phase: 0: Promos, 1: TT, 2: Captures, 3: Killers, 4: Countermove, 5: Quiets, 6: Bad Captures.

Static Private Attributes

static constexpr int MAX_MOVES = 256
 Maximum allowed moves, aligned to power of 2 for safety.

Detailed Description

Lazily selects the best move at each step of the search.

Avoids scoring and sorting all moves upfront, which is crucial for cutoff-heavy nodes. Uses a phased approach: Transposition Table move, Queen Promotions, Good Captures (SEE > 0), Killer Moves, Countermoves, Quiet Moves, and finally Bad Captures (SEE < 0).

Constructor & Destructor Documentation

◆ MovePicker()

MovePicker::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) )
inline

Constructs a MovePicker for a specific search node.

Parameters
movesGenerated legal moves.
ttMoveBest move from TT.
killersPointer to killer heuristics.
historyPointer to history heuristics.
captureHistPointer to capture history heuristics.
capturesPre-allocated buffer for captures.
captureScoresPre-allocated buffer for capture scores.
quietsPre-allocated buffer for quiets.
quietScoresPre-allocated buffer for quiet scores.
badCapturesPre-allocated buffer for bad captures.
plyCurrent ply from root.
threadIdThread ID for diversity.
countermoveCandidate countermove.

Member Function Documentation

◆ generateAndScoreCaptures()

void MovePicker::generateAndScoreCaptures ( const Board & board,
const Negamax * search )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateAndScoreKillers()

void MovePicker::generateAndScoreKillers ( const Board & board,
const Negamax * search )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ generateAndScoreQuiets()

void MovePicker::generateAndScoreQuiets ( const Board & board,
const Negamax * search )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nextMove()

Move MovePicker::nextMove ( const Board & board,
const Negamax * search,
bool inCheck )

Returns the next best move according to the current phase.

Lazily processes and sorts moves only when a phase transition occurs.

Parameters
boardThe current board state.
searchPointer to the active Negamax search instance for SEE evaluation.
inCheckTrue if the side to move is currently in check.
Returns
The next Move to evaluate, or Move(0) if all moves are exhausted.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ scoreMove()

int MovePicker::scoreMove ( const Move & move,
const Board & board,
const Negamax * search,
bool isCapture )
private
Here is the caller graph for this function:

◆ selectBestMove()

template<size_t N>
Move MovePicker::selectBestMove ( std::array< Move, N > & list,
std::array< int, N > & scores,
int & nextIdx,
int count )
private
Here is the caller graph for this function:

Member Data Documentation

◆ allMoves_

const Movelist& MovePicker::allMoves_
private

Reference to all legal moves generated for the current node.

◆ badCaptureCount_

int MovePicker::badCaptureCount_ = 0
private

Number of bad captures found.

◆ badCaptures_

std::array<Move, MAX_MOVES>& MovePicker::badCaptures_
private

External buffer for captures that fail SEE.

◆ captureCount_

int MovePicker::captureCount_ = 0
private

Number of captures populated.

◆ captureHist_

const std::array<std::array<int, 64>, 16>* MovePicker::captureHist_
private

Pointer to global capture history table.

◆ captures_

std::array<Move, MAX_MOVES>& MovePicker::captures_
private

External buffer for capture moves.

◆ captureScores_

std::array<int, MAX_MOVES>& MovePicker::captureScores_
private

External buffer for capture scores.

◆ capturesGenerated_

bool MovePicker::capturesGenerated_ = false
private

True if capture moves have been scored and sorted.

◆ countermove_

Move MovePicker::countermove_
private

Countermove heuristic response to the opponent's previous move.

◆ countermoveReturned_

bool MovePicker::countermoveReturned_ = false
private

True if the countermove has already been returned.

◆ history_

const std::array<std::array<int, 64>, 64>* MovePicker::history_
private

Pointer to global history heuristic table.

◆ killerCount_

int MovePicker::killerCount_ = 0
private

Number of valid killer moves found.

◆ killerMoves_

std::array<Move, 2> MovePicker::killerMoves_
private

Local cache of killer moves for this ply.

◆ killers_

const std::array<std::array<Move, 2>, SearchConstants::MAX_PLY>* MovePicker::killers_
private

Pointer to global killer heuristic table.

◆ killersGenerated_

bool MovePicker::killersGenerated_ = false
private

True if killer moves have been extracted.

◆ MAX_MOVES

int MovePicker::MAX_MOVES = 256
staticconstexprprivate

Maximum allowed moves, aligned to power of 2 for safety.

◆ nextBadCapture_

int MovePicker::nextBadCapture_ = 0
private

Index of the next bad capture to return.

◆ nextCapture_

int MovePicker::nextCapture_ = 0
private

Index of the next capture to return.

◆ nextKiller_

int MovePicker::nextKiller_ = 0
private

Index of the next killer to return.

◆ nextQueenPromo_

int MovePicker::nextQueenPromo_ = 0
private

Index of the next queen promotion to return.

◆ nextQuiet_

int MovePicker::nextQuiet_ = 0
private

Index of the next quiet move to return.

◆ phase_

int MovePicker::phase_ = 0
private

Current phase: 0: Promos, 1: TT, 2: Captures, 3: Killers, 4: Countermove, 5: Quiets, 6: Bad Captures.

◆ ply_

int MovePicker::ply_
private

Current distance from root.

◆ queenPromoCount_

int MovePicker::queenPromoCount_ = 0
private

Number of queen promotions found.

◆ quietCount_

int MovePicker::quietCount_ = 0
private

Number of quiets populated.

◆ quiets_

std::array<Move, MAX_MOVES>& MovePicker::quiets_
private

External buffer for quiet moves.

◆ quietScores_

std::array<int, MAX_MOVES>& MovePicker::quietScores_
private

External buffer for quiet scores.

◆ quietsGenerated_

bool MovePicker::quietsGenerated_ = false
private

True if quiet moves have been scored and sorted.

◆ threadId_

int MovePicker::threadId_
private

Thread ID for diversified move ordering in Lazy SMP.

◆ ttMove_

Move MovePicker::ttMove_
private

Best move retrieved from the Transposition Table (searched first).


The documentation for this class was generated from the following files: