Skip to content

Commit bf5461e

Browse files
committed
tb
1 parent c8ed2f4 commit bf5461e

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

tb.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "tb.h"
2+
#include <exception>
3+
#include <iostream>
4+
#include <tbprobe.h>
5+
#include <unordered_set>
6+
7+
namespace engine::tb {
8+
9+
tbprobe::syzygy::Tablebase tablebase;
10+
11+
namespace {
12+
std::size_t unique_table_count(const std::unordered_map<std::string, tbprobe::syzygy::Table *> &tables) {
13+
std::unordered_set<const tbprobe::syzygy::Table *> uniqueTables;
14+
15+
for (const auto &[_, table] : tables)
16+
if (table != nullptr)
17+
uniqueTables.insert(table);
18+
19+
return uniqueTables.size();
20+
}
21+
} // namespace
22+
23+
void init(const std::string &path) {
24+
tablebase.close();
25+
26+
if (path.empty()) {
27+
std::cout << "info string Found 0 WDL and 0 DTZ tablebase files\n";
28+
return;
29+
}
30+
31+
tbprobe::syzygy::initialize();
32+
tablebase.add_directory(path, true, true);
33+
std::cout << "info string Found " << wdl_count() << " WDL and " << dtz_count() << " DTZ tablebase files\n";
34+
}
35+
36+
std::size_t wdl_count() { return unique_table_count(tablebase.wdl); }
37+
38+
std::size_t dtz_count() { return unique_table_count(tablebase.dtz); }
39+
40+
int probe_wdl(chess::Board &board) {
41+
try {
42+
return *tablebase.get_wdl(board, TB_ERROR);
43+
} catch (const std::exception &) {
44+
return TB_ERROR;
45+
}
46+
}
47+
48+
int probe_dtz(chess::Board &board) {
49+
try {
50+
return *tablebase.get_dtz(board, TB_ERROR);
51+
} catch (const std::exception &) {
52+
return TB_ERROR;
53+
}
54+
}
55+
} // namespace engine::tb

tb.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include "eval.h"
3+
#include <cstddef>
4+
#include <position.h>
5+
#include <string>
6+
7+
namespace engine::tb {
8+
9+
void init(const std::string &path);
10+
constexpr int TB_ERROR = 1000;
11+
std::size_t wdl_count();
12+
std::size_t dtz_count();
13+
int probe_wdl(chess::Board &board);
14+
int probe_dtz(chess::Board &board);
15+
} // namespace engine::tb

0 commit comments

Comments
 (0)