A dependency-free C++17 implementation of the OneDice V1 unified dice-string standard — the C++ counterpart to the official Python / Kotlin / Rust / TypeScript reference libraries.
#include "onedice/onedice.h"
onedice::RD dice("(1d8+2d100)*1d10");
if (dice.roll() == 0) {
long long result = dice.resInt(); // e.g. 1296
std::string how = dice.detail(); // human-readable breakdown
}Or the one-shot form:
auto r = onedice::eval("4d6kh3"); // DND ability score
if (r.ok) std::cout << r.value << " " << r.detail;Evaluation failures are classified so callers can build a safe parser chain:
auto r = onedice::eval(expr);
if (!r.ok && r.errorKind == onedice::ErrorKind::UnsupportedSyntax) {
// It is safe to let another expression engine try the input.
} else if (!r.ok) {
// Evaluation failed (for example division by zero); do not reroll elsewhere.
}You can inject your own RNG (e.g. a seeded or session-shared generator):
onedice::RD d("3d6");
d.setRandom([](long long faces){ return /* 1..faces */; });| Feature | Syntax |
|---|---|
| Arithmetic & precedence | + - * / ^ ( ), x as * |
| Multi-face dice | AdB (face defaults configurable) |
| Keep highest / lowest | kh kl dh dl, and d-suffix k/q |
| Clamp | max / min |
| Penalty / bonus (COC) | ApB / AbB |
| Infinite adding pool | AaBkCqDmE |
| Double-cross pool | AcBmC |
| FATE / Fudge | Af, AdfB, f |
| Comparison / bitwise | < >, & | |
| Tuples | [a,b,c] |
?: ternary, $t temp variables, lp loop, sp slice, tp eject,
factorial, xo explode, environment variables. Contributions welcome.
cmake -B build -S . && cmake --build build
./build/onedice-example # runs the smoke testIntegrate into your project via CMake:
add_subdirectory(onedice-cpp-lib)
target_link_libraries(your_target PRIVATE onedice::onedice)MIT (see LICENSE), matching the OneDice project.