init python lib generation with pyo3

This commit is contained in:
Henri Bourcereau 2025-02-08 13:28:42 +01:00
parent 59c80c66e4
commit 883d799edb
8 changed files with 201 additions and 6 deletions

View file

@ -5,11 +5,18 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "trictrac"
# "cdylib" is necessary to produce a shared library for Python to import from.
crate-type = ["cdylib"]
[dependencies]
base64 = "0.21.7"
# provides macros for creating log messages to be used by a logger (for example env_logger)
log = "0.4.20"
merge = "0.1.0"
# generate python lib to be used in AI training
pyo3 = { version = "0.23", features = ["extension-module", "abi3-py38"] }
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
transpose = "0.2.2"

10
store/pyproject.toml Normal file
View file

@ -0,0 +1,10 @@
[build-system]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"
[tool.maturin]
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
features = ["pyo3/extension-module"]
# python-source = "python"
# module-name = "trictrac.game"

9
store/python/test.py Normal file
View file

@ -0,0 +1,9 @@
import trictrac
game = trictrac.TricTrac()
print(game.get_state()) # "Initial state"
moves = game.get_available_moves()
print(moves) # [(0, 5), (3, 8)]
game.play_move(0, 5)

42
store/src/engine.rs Normal file
View file

@ -0,0 +1,42 @@
//! # Expose trictrac game state and rules in a python module
use pyo3::prelude::*;
use pyo3::types::PyTuple;
#[pyclass]
struct TricTrac {
state: String, // Remplace par ta structure d'état du jeu
}
#[pymethods]
impl TricTrac {
#[new]
fn new() -> Self {
TricTrac {
state: "Initial state".to_string(),
}
}
fn get_state(&self) -> String {
self.state.clone()
}
fn get_available_moves(&self) -> Vec<(i32, i32)> {
vec![(0, 5), (3, 8)] // Remplace par ta logique de génération de coups
}
fn play_move(&mut self, from_pos: i32, to_pos: i32) -> bool {
// Ajoute la logique du jeu ici
println!("Move... from {} to {}", from_pos, to_pos);
true
}
}
/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn trictrac(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<TricTrac>()?;
Ok(())
}

View file

@ -16,3 +16,6 @@ pub use board::CheckerMove;
mod dice;
pub use dice::{Dice, DiceRoller};
// python interface "trictrac_engine" (for AI training..)
mod engine;