Skip to content

Commit b2f6976

Browse files
authored
Merge branch 'main' into levenshtein-distance
2 parents 6501eb6 + 43ef137 commit b2f6976

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

libbenbot/include/libbenbot/search/Callbacks.hpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
#pragma once
2121

2222
#include <functional>
23+
#include <libchess/moves/Move.hpp>
2324
#include <string>
2425

25-
namespace chess::moves {
26-
struct Move;
27-
}
28-
2926
namespace ben_bot::search {
3027

3128
struct Options;
3229
struct Result;
3330

31+
using chess::moves::Move;
32+
3433
/** This struct encapsulates a set of functions that will be called to
3534
process search progress and results. Search results are always
3635
retrieved through these callbacks.
@@ -53,6 +52,12 @@ struct Callbacks final {
5352
*/
5453
Callback onIteration;
5554

55+
/** Function object that will be invoked with each root move being searched.
56+
The second argument is the index of the current move in the list of moves
57+
being searched.
58+
*/
59+
std::function<void(Move, size_t)> onRootMove;
60+
5661
/** Can be safely called without checking if ``onSearchStart`` is null. */
5762
void search_start(const Options& options) const
5863
{
@@ -78,14 +83,22 @@ struct Callbacks final {
7883
}
7984
}
8085

86+
/** Can be safely called without checking if ``onRootMove`` is null. */
87+
void root_move(const Move move, const size_t idx) const
88+
{
89+
if (onRootMove != nullptr) {
90+
onRootMove(move, idx);
91+
}
92+
}
93+
8194
/** Creates a set of callbacks that print UCI-formatted information and bestmove
8295
output to standard output.
8396
8497
@param isDebugMode Function object that should return true if debug information
8598
should be included in the information output.
8699
*/
87100
[[nodiscard]] static auto make_uci_printer(
88-
std::function<bool()>&& isDebugMode)
101+
std::function<bool()> isDebugMode)
89102
-> Callbacks;
90103

91104
/** Creates a set of callbacks that print search information in a human-readable
@@ -97,7 +110,7 @@ struct Callbacks final {
97110
@note The output produced by these callbacks does not conform to the UCI protocol!
98111
*/
99112
[[nodiscard]] static auto make_pretty_printer(
100-
std::function<std::string(chess::moves::Move)>&& printMove)
113+
std::function<std::string(Move)>&& printMove)
101114
-> Callbacks;
102115
};
103116

libbenbot/src/search/Callbacks.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,27 @@
3737
namespace ben_bot::search {
3838

3939
auto Callbacks::make_uci_printer(
40-
std::function<bool()>&& isDebugMode)
40+
std::function<bool()> isDebugMode)
4141
-> Callbacks
4242
{
43-
auto printInfo = [isDebug = std::move(isDebugMode)](const Result& res) {
44-
search_info(res.to_libchess(isDebug()));
43+
namespace uci_printing = chess::uci::printing;
44+
45+
auto printInfo = [isDebugMode](const Result& res) {
46+
search_info(res.to_libchess(isDebugMode()));
4547
};
4648

4749
return {
4850
.onSearchStart = nullptr,
4951
.onSearchComplete = [printInfo](const Result& res) {
5052
printInfo(res);
51-
chess::uci::printing::best_move(res.best_move(), res.ponder_move()); },
52-
.onIteration = printInfo
53+
uci_printing::best_move(res.best_move(), res.ponder_move()); },
54+
.onIteration = printInfo,
55+
.onRootMove = [isDebugMode](const Move move, const size_t idx) {
56+
if (isDebugMode()) {
57+
uci_printing::currmove_info(
58+
move,
59+
idx + 1uz); // convert 0-based -> 1-based index
60+
} }
5361
};
5462
}
5563

@@ -314,7 +322,8 @@ auto Callbacks::make_pretty_printer(
314322
print_table_header();
315323
},
316324
.onSearchComplete = printIteration,
317-
.onIteration = printIteration
325+
.onIteration = printIteration,
326+
.onRootMove = nullptr
318327
};
319328
}
320329

libbenbot/src/search/Search.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ namespace {
362362
const Position& position,
363363
TranspositionTable& transTable,
364364
Interrupter& interrupter,
365-
KillerMoves& killerMoves)
365+
KillerMoves& killerMoves,
366+
const Callbacks& callbacks)
366367
-> RootSearchResult
367368
{
368369
const Timer timer;
@@ -377,8 +378,11 @@ namespace {
377378
MoveList pv;
378379

379380
bool foundPV = false;
381+
auto moveIdx = 0uz;
380382

381383
for (const auto move : options.movesToSearch) {
384+
callbacks.root_move(move, moveIdx++);
385+
382386
PvList childPV;
383387

384388
auto create_context = [depth, &transTable, &interrupter, &stats, &childPV, &killerMoves](const Bounds boundsToUse, const Position& pos) {
@@ -485,7 +489,8 @@ void Context::search() // NOLINT(readability-function-cognitive-complexity)
485489
if (interrupter.should_abort(0uz))
486490
break;
487491

488-
const auto res = root_search(depth, options, position, transTable, interrupter, killerMoves);
492+
const auto res = root_search(
493+
depth, options, position, transTable, interrupter, killerMoves, callbacks);
489494

490495
if (interrupter.was_aborted())
491496
break;

libchess/include/libchess/uci/Printing.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,22 @@ void best_move(
5656
Move bestMove,
5757
std::optional<Move> ponderMove);
5858

59+
/** Informs the GUI that the engine is currently searching the given move.
60+
61+
@note For the first move, ``moveNum`` should be 1, not 0!
62+
63+
@ingroup uci
64+
*/
65+
void currmove_info(
66+
Move currentMove, size_t moveNum);
67+
5968
/** This POD struct encapsulates the various information that can be printed
6069
about a search.
6170
6271
@ingroup uci
6372
@see search_info()
6473
65-
@todo ``multipv``, ``currmove``, ``currmovenumber``, ``refutation``, ``currline``
74+
@todo ``refutation``, ``currline``
6675
*/
6776
struct SearchInfo final {
6877
/** Represents the engine's evaluation of the line it is currently searching. */

libchess/src/uci/Printing.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ void best_move(
6969
cout.flush();
7070
}
7171

72+
void currmove_info(
73+
const Move currentMove, const size_t moveNum)
74+
{
75+
println(cout,
76+
"info currmove {} currmovenumber {}",
77+
to_uci(currentMove), moveNum);
78+
79+
cout.flush();
80+
}
81+
7282
auto SearchInfo::Score::MateIn::moves() const noexcept -> int
7383
{
7484
return [ply = plies]() mutable {

0 commit comments

Comments
 (0)