Personally, I get the bestmove in the alphabeta routine.
To return the besmove from the search I store the list of moves to search at root apart, and sort the best move here.
So When I need to return the bestmove I just return the fist move of root moves.
Here is the pseudocode. The lines with "//<- HERE" are what you want.
Code: Select all
typedef struct Search {
Board *board;
Eval eval;
MoveArray root_moves; // <- HERE
int ply;
...
} Search;
Move search_bestmove(const Search *search) {
return search->root_moves.move[0];
}
int search_alphabeta(Search *search, int alpha, const int beta, const int depth) {
int score, bestscore = -SCORE_MATE;
int move, bestmove;
MoveArray movearray_;
MoveArray *movearray = search->ply == 0 ? &search->root_moves : &movearray_; // <- HERE
if (search_abort(search)) return alpha;
if (depth <= 0) return search_qs(search, alpha, beta);
if (search->ply > PLY_MAX) return eval(&search->eval, search->board);
++search->pvs_nodes;
movearray_generate(movearray, search->board);
// loop over all moves
foreach (move, movearray) {
search_update(search, move);
score = -search_alphabeta(search, -beta, -alpha, depth - 1);
search_restore(search, move);
if (search->stop) return alpha;
if (score > bestscore) {
bestscore = score
if (score > alpha) {
alpha = bestscore;
bestmove = move;
if (search->ply == 0) movearray_set_best(&search->root_moves, bestmove); // and <-- HERE
if (alpha >= beta) break;
}
}
}
return bestscore;
}