Skip to content

Commit

Permalink
Add Ski Jumps python module
Browse files Browse the repository at this point in the history
  • Loading branch information
t4ccer committed Nov 5, 2023
1 parent c13cf6d commit be10498
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cgt-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ mod canonical_form;
mod domineering;
mod nimber;
mod rational;
mod ski_jumps;
mod thermograph;

use crate::{canonical_form::*, domineering::*, nimber::*, rational::*, thermograph::*};
use crate::{
canonical_form::*, domineering::*, nimber::*, rational::*, ski_jumps::*, thermograph::*,
};

// TODO: Pretty printers
// TODO: SVG rendering & html()
Expand Down Expand Up @@ -55,5 +58,7 @@ fn cgt_py(_py: Python, m: &PyModule) -> PyResult<()> {

add_class!(PyThermograph);

add_class!(PySkiJumps);

Ok(())
}
54 changes: 54 additions & 0 deletions cgt-py/src/ski_jumps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::canonical_form::PyCanonicalForm;
use cgt::short::partizan::{
games::ski_jumps::SkiJumps, partizan_game::PartizanGame,
transposition_table::TranspositionTable,
};
use pyo3::prelude::*;
use std::str::FromStr;

crate::wrap_struct!(SkiJumps, PySkiJumps, "SkiJumps", Clone);
crate::wrap_struct!(
TranspositionTable<SkiJumps>,
PySkiJumpsTranspositionTable,
"SkiJumpsTranspositionTable"
);

#[pymethods]
impl PySkiJumps {
#[new]
fn py_new(position: &str) -> PyResult<Self> {
let grid = SkiJumps::from_str(position)
.or(Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"Parse error",
)))?;
Ok(Self::from(grid))
}

fn __repr__(&self) -> String {
format!("SkiJumps('{}')", self.inner)
}

fn to_svg(&self) -> String {
self.inner.to_svg()
}

#[staticmethod]
fn transposition_table() -> PySkiJumpsTranspositionTable {
PySkiJumpsTranspositionTable::from(TranspositionTable::new())
}

fn canonical_form(
&self,
transposition_table: Option<&PySkiJumpsTranspositionTable>,
) -> PyCanonicalForm {
match transposition_table {
Some(transposition_table) => {
PyCanonicalForm::from(self.inner.canonical_form(&transposition_table.inner))
}
None => PyCanonicalForm::from(
self.inner
.canonical_form(&Self::transposition_table().inner),
),
}
}
}

0 comments on commit be10498

Please sign in to comment.