Skip to content

Commit

Permalink
C-API Cube info support (#30)
Browse files Browse the repository at this point in the history
This PR adds support for obtaining the results of a cube evaluation in
the C-API. I've added the new `cube_info` function and `CCubeInfo`
types.

Cube evaluation is still using the simple 40/60 calculation, I'm
currently looking at improving this in another pr.
  • Loading branch information
mbaum0 authored Nov 17, 2024
1 parent 643633f commit c66e47b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
13 changes: 12 additions & 1 deletion crates/logic/src/cube.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use engine::probabilities::Probabilities;
#[cfg(feature = "web")]
use serde::Serialize;
#[cfg(feature = "web")]
use utoipa::ToSchema;

#[derive(Serialize, ToSchema)]
#[cfg_attr(feature = "web", derive(Serialize, ToSchema))]
/// Information about proper cube decisions. Currently quick and dirty calculations.
pub struct CubeInfo {
/// `true` if the player `x` should double, `false` if no double yet or too good.
Expand All @@ -22,3 +24,12 @@ impl From<&Probabilities> for CubeInfo {
Self { double, accept }
}
}

impl CubeInfo {
pub fn double(&self) -> bool {
self.double
}
pub fn accept(&self) -> bool {
self.accept
}
}
1 change: 0 additions & 1 deletion crates/logic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod bg_move;
#[cfg(feature = "web")]
pub mod cube;
pub mod wildbg_api;
5 changes: 5 additions & 0 deletions crates/logic/src/wildbg_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bg_move::BgMove;
use crate::cube::CubeInfo;
use engine::composite::CompositeEvaluator;
use engine::dice::Dice;
use engine::evaluator::Evaluator;
Expand Down Expand Up @@ -37,6 +38,10 @@ impl<T: Evaluator> WildbgApi<T> {
let new_position = self.evaluator.best_position(position, dice, value);
BgMove::new(position, &new_position.sides_switched(), dice)
}

pub fn cube_info(&self, position: &Position) -> CubeInfo {
CubeInfo::from(&self.evaluator.eval(position))
}
}

#[cfg(test)]
Expand Down
32 changes: 32 additions & 0 deletions crates/wildbg-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use engine::dice::Dice;
use engine::position::Position;
use engine::probabilities::Probabilities;
use logic::bg_move::{BgMove, MoveDetail};
use logic::cube::CubeInfo;
use logic::wildbg_api::{WildbgApi, WildbgConfig};

// When this file is changed, recreate the header file by executing this from the project's root:
Expand Down Expand Up @@ -148,6 +149,22 @@ impl From<&MoveDetail> for CMoveDetail {
}
}

#[repr(C)]
#[derive(Default)]
pub struct CCubeInfo {
should_double: bool,
should_accept: bool,
}

impl From<&CubeInfo> for CCubeInfo {
fn from(value: &CubeInfo) -> Self {
Self {
should_double: value.double(),
should_accept: value.accept(),
}
}
}

type Error = &'static str;

/// Returns the best move for the given position.
Expand Down Expand Up @@ -199,6 +216,21 @@ pub extern "C" fn probabilities(wildbg: &Wildbg, pips: &[c_int; 26]) -> CProbabi
}
}

#[no_mangle]
pub extern "C" fn cube_info(wildbg: &Wildbg, pips: &[c_int; 26]) -> CCubeInfo {
let pips = pips.map(|pip| pip as i8);
match Position::try_from(pips) {
Ok(position) => (&wildbg.api.cube_info(&position)).into(),
Err(error) => {
eprintln!("{}", error);
CCubeInfo {
should_double: false,
should_accept: false,
}
}
}
}

#[cfg(test)]
mod tests {

Expand Down
7 changes: 7 additions & 0 deletions crates/wildbg-c/wildbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ typedef struct CProbabilities {
float lose_bg;
} CProbabilities;

typedef struct CCubeInfo {
bool should_double;
bool should_accept;
} CCubeInfo;

/**
* Loads the neural nets into memory and returns a pointer to the API.
* Returns `NULL` if the neural nets cannot be found.
Expand Down Expand Up @@ -109,3 +114,5 @@ struct CMove best_move(const struct Wildbg *wildbg,
*/
struct CProbabilities probabilities(const struct Wildbg *wildbg,
const int (*pips)[26]);

struct CCubeInfo cube_info(const struct Wildbg *wildbg, const int (*pips)[26]);

0 comments on commit c66e47b

Please sign in to comment.