Compare commits

..

7 commits

Author SHA1 Message Date
Henri Bourcereau 1271672341 beads 2026-01-18 20:20:24 +01:00
Henri Bourcereau 39fd807339 feat: python bindings (wip) 2026-01-18 20:19:10 +01:00
Henri Bourcereau 13ec2009a5 fix: comment 2026-01-18 18:41:08 +01:00
Henri Bourcereau 8be1071291 python bindings 2026-01-17 20:58:00 +01:00
Henri Bourcereau 0024843a5c chore:add beads 2026-01-17 20:42:59 +01:00
Henri Bourcereau daf84f29ab doc 2026-01-15 18:31:12 +01:00
Henri Bourcereau e4bd20578a Merge tag 'v0.1.1' into develop
v0.1.1
2026-01-15 17:14:44 +01:00
23 changed files with 652 additions and 17 deletions

44
.beads/.gitignore vendored Normal file
View file

@ -0,0 +1,44 @@
# SQLite databases
*.db
*.db?*
*.db-journal
*.db-wal
*.db-shm
# Daemon runtime files
daemon.lock
daemon.log
daemon.pid
bd.sock
sync-state.json
last-touched
# Local version tracking (prevents upgrade notification spam after git ops)
.local_version
# Legacy database files
db.sqlite
bd.db
# Worktree redirect file (contains relative path to main repo's .beads/)
# Must not be committed as paths would be wrong in other clones
redirect
# Merge artifacts (temporary files from 3-way merge)
beads.base.jsonl
beads.base.meta.json
beads.left.jsonl
beads.left.meta.json
beads.right.jsonl
beads.right.meta.json
# Sync state (local-only, per-machine)
# These files are machine-specific and should not be shared across clones
.sync.lock
sync_base.jsonl
# NOTE: Do NOT add negation patterns (e.g., !issues.jsonl) here.
# They would override fork protection in .git/info/exclude, allowing
# contributors to accidentally commit upstream issue databases.
# The JSONL files (issues.jsonl, interactions.jsonl) and config files
# are tracked by git by default since no pattern above ignores them.

81
.beads/README.md Normal file
View file

@ -0,0 +1,81 @@
# Beads - AI-Native Issue Tracking
Welcome to Beads! This repository uses **Beads** for issue tracking - a modern, AI-native tool designed to live directly in your codebase alongside your code.
## What is Beads?
Beads is issue tracking that lives in your repo, making it perfect for AI coding agents and developers who want their issues close to their code. No web UI required - everything works through the CLI and integrates seamlessly with git.
**Learn more:** [github.com/steveyegge/beads](https://github.com/steveyegge/beads)
## Quick Start
### Essential Commands
```bash
# Create new issues
bd create "Add user authentication"
# View all issues
bd list
# View issue details
bd show <issue-id>
# Update issue status
bd update <issue-id> --status in_progress
bd update <issue-id> --status done
# Sync with git remote
bd sync
```
### Working with Issues
Issues in Beads are:
- **Git-native**: Stored in `.beads/issues.jsonl` and synced like code
- **AI-friendly**: CLI-first design works perfectly with AI coding agents
- **Branch-aware**: Issues can follow your branch workflow
- **Always in sync**: Auto-syncs with your commits
## Why Beads?
✨ **AI-Native Design**
- Built specifically for AI-assisted development workflows
- CLI-first interface works seamlessly with AI coding agents
- No context switching to web UIs
🚀 **Developer Focused**
- Issues live in your repo, right next to your code
- Works offline, syncs when you push
- Fast, lightweight, and stays out of your way
🔧 **Git Integration**
- Automatic sync with git commits
- Branch-aware issue tracking
- Intelligent JSONL merge resolution
## Get Started with Beads
Try Beads in your own projects:
```bash
# Install Beads
curl -sSL https://raw.githubusercontent.com/steveyegge/beads/main/scripts/install.sh | bash
# Initialize in your repo
bd init
# Create your first issue
bd create "Try out Beads"
```
## Learn More
- **Documentation**: [github.com/steveyegge/beads/docs](https://github.com/steveyegge/beads/tree/main/docs)
- **Quick Start Guide**: Run `bd quickstart`
- **Examples**: [github.com/steveyegge/beads/examples](https://github.com/steveyegge/beads/tree/main/examples)
---
*Beads: Issue tracking that moves at the speed of thought* ⚡

62
.beads/config.yaml Normal file
View file

@ -0,0 +1,62 @@
# Beads Configuration File
# This file configures default behavior for all bd commands in this repository
# All settings can also be set via environment variables (BD_* prefix)
# or overridden with command-line flags
# Issue prefix for this repository (used by bd init)
# If not set, bd init will auto-detect from directory name
# Example: issue-prefix: "myproject" creates issues like "myproject-1", "myproject-2", etc.
# issue-prefix: ""
# Use no-db mode: load from JSONL, no SQLite, write back after each command
# When true, bd will use .beads/issues.jsonl as the source of truth
# instead of SQLite database
# no-db: false
# Disable daemon for RPC communication (forces direct database access)
# no-daemon: false
# Disable auto-flush of database to JSONL after mutations
# no-auto-flush: false
# Disable auto-import from JSONL when it's newer than database
# no-auto-import: false
# Enable JSON output by default
# json: false
# Default actor for audit trails (overridden by BD_ACTOR or --actor)
# actor: ""
# Path to database (overridden by BEADS_DB or --db)
# db: ""
# Auto-start daemon if not running (can also use BEADS_AUTO_START_DAEMON)
# auto-start-daemon: true
# Debounce interval for auto-flush (can also use BEADS_FLUSH_DEBOUNCE)
# flush-debounce: "5s"
# Git branch for beads commits (bd sync will commit to this branch)
# IMPORTANT: Set this for team projects so all clones use the same sync branch.
# This setting persists across clones (unlike database config which is gitignored).
# Can also use BEADS_SYNC_BRANCH env var for local override.
# If not set, bd sync will require you to run 'bd config set sync.branch <branch>'.
sync-branch: "beads-sync"
# Multi-repo configuration (experimental - bd-307)
# Allows hydrating from multiple repositories and routing writes to the correct JSONL
# repos:
# primary: "." # Primary repo (where this database lives)
# additional: # Additional repos to hydrate from (read-only)
# - ~/beads-planning # Personal planning repo
# - ~/work-planning # Work planning repo
# Integration settings (access with 'bd config get/set')
# These are stored in the database, not in this file:
# - jira.url
# - jira.project
# - linear.url
# - linear.api-key
# - github.org
# - github.repo

View file

1
.beads/issues.jsonl Normal file
View file

@ -0,0 +1 @@
{"id":"trictrac-wxm","title":"Make a python library to use in Open Spiel","description":"A python tritrac game definition for Open Spiel, without implementation, is in the project located at doc/refs/open_spiel/open_spiel/python/games/trictrac.py (doc/refs/open_spiel is a symbolic link).\n\nI want to use PYO3, like in the `store/src/pyengine.rs` file, to expose the structures and methods needed to implement the functions of the `open_spiel` `trictrac.py` game file.\n\nEstablish an action plan and create sub-tasks of this issue for each step.\n","status":"closed","priority":2,"issue_type":"task","owner":"henri.bourcereau@gmail.com","created_at":"2026-01-18T19:24:00.323375444+01:00","created_by":"Henri Bourcereau","updated_at":"2026-01-18T20:03:17.168210389+01:00","closed_at":"2026-01-18T20:03:17.168210389+01:00","close_reason":"Closed"}

4
.beads/metadata.json Normal file
View file

@ -0,0 +1,4 @@
{
"database": "beads.db",
"jsonl_export": "issues.jsonl"
}

3
.gitattributes vendored Normal file
View file

@ -0,0 +1,3 @@
# Use bd merge for beads JSONL files
.beads/issues.jsonl merge=beads

24
AGENTS.md Normal file
View file

@ -0,0 +1,24 @@
# Agent Instructions
This project uses **bd** (beads) for issue tracking.
Run `bd prime` for workflow context, or install hooks (`bd hooks install`) for auto-injection.
## Quick Reference
- `bd ready` - Find unblocked work
- `bd create "Title" --type task --priority 2` - Create issue
- `bd update <id> --status in_progress` # Claim work
- `bd close <id>` - Complete work
- `bd sync` - Sync with git (run at session end)
## Landing the Plane (Session Completion)
**When ending a work session**, you MUST complete ALL steps below.
**MANDATORY WORKFLOW:**
1. **File issues for remaining work** - Create issues for anything that needs follow-up
2. **Run quality gates** (if code changed) - Tests, linters, builds
3. **Update issue status** - Close finished work, update in-progress items
4. **Hand off** - Provide context for next session

79
Cargo.lock generated
View file

@ -3460,6 +3460,15 @@ dependencies = [
"stable_deref_trait", "stable_deref_trait",
] ]
[[package]]
name = "memoffset"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "merge" name = "merge"
version = "0.1.0" version = "0.1.0"
@ -4210,6 +4219,69 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "pyo3"
version = "0.23.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872"
dependencies = [
"cfg-if",
"indoc",
"libc",
"memoffset",
"once_cell",
"portable-atomic",
"pyo3-build-config",
"pyo3-ffi",
"pyo3-macros",
"unindent",
]
[[package]]
name = "pyo3-build-config"
version = "0.23.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94f6cbe86ef3bf18998d9df6e0f3fc1050a8c5efa409bf712e661a4366e010fb"
dependencies = [
"once_cell",
"target-lexicon",
]
[[package]]
name = "pyo3-ffi"
version = "0.23.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9f1b4c431c0bb1c8fb0a338709859eed0d030ff6daa34368d3b152a63dfdd8d"
dependencies = [
"libc",
"pyo3-build-config",
]
[[package]]
name = "pyo3-macros"
version = "0.23.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbc2201328f63c4710f68abdf653c89d8dbc2858b88c5d88b0ff38a75288a9da"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
"quote",
"syn 2.0.106",
]
[[package]]
name = "pyo3-macros-backend"
version = "0.23.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fca6726ad0f3da9c9de093d6f116a93c1a38e417ed73bf138472cf4064f72028"
dependencies = [
"heck",
"proc-macro2",
"pyo3-build-config",
"quote",
"syn 2.0.106",
]
[[package]] [[package]]
name = "qoi" name = "qoi"
version = "0.4.1" version = "0.4.1"
@ -5154,6 +5226,7 @@ dependencies = [
"base64 0.21.7", "base64 0.21.7",
"log", "log",
"merge", "merge",
"pyo3",
"rand 0.8.5", "rand 0.8.5",
"serde", "serde",
"transpose", "transpose",
@ -5892,6 +5965,12 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "unindent"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
[[package]] [[package]]
name = "universal-hash" name = "universal-hash"
version = "0.5.1" version = "0.5.1"

4
bot/python/test.py Normal file
View file

@ -0,0 +1,4 @@
import store
game = store.TricTrac()
print(game.get_state_dict())

View file

@ -237,7 +237,7 @@ impl TrictracEnvironment {
// Mapper l'index d'action sur une action valide // Mapper l'index d'action sur une action valide
let action_index = (action.index as usize) % valid_actions.len(); let action_index = (action.index as usize) % valid_actions.len();
Some(valid_actions[action_index].clone()) Some(valid_actions[action_index])
} }
/// Exécute une action Trictrac dans le jeu /// Exécute une action Trictrac dans le jeu

View file

@ -6,8 +6,8 @@ use std::fmt::{Debug, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use store::{CheckerMove, GameEvent, GameState}; use store::{CheckerMove, GameEvent, GameState};
// 1 (Roll) + 1 (Go) + mouvements possibles // 1 (Roll) + 1 (Go) + 512 (mouvements possibles)
// Pour les mouvements : 2*16*16 = 514 (choix du dé + choix de la dame 0-15 pour chaque from) // avec 512 = 2 (choix du dé) * 16 * 16 (choix de la dame 0-15 pour chaque from)
pub const ACTION_SPACE_SIZE: usize = 514; pub const ACTION_SPACE_SIZE: usize = 514;
/// Types d'actions possibles dans le jeu /// Types d'actions possibles dans le jeu
@ -15,7 +15,8 @@ pub const ACTION_SPACE_SIZE: usize = 514;
pub enum TrictracAction { pub enum TrictracAction {
/// Lancer les dés /// Lancer les dés
Roll, Roll,
/// Continuer après avoir gagné un trou /// Faire un nouveau 'relevé' (repositionnement des dames à l'état de départ) après avoir gagné un trou,
/// au lieu de continuer dans la position courante
Go, Go,
/// Effectuer un mouvement de pions /// Effectuer un mouvement de pions
Move { Move {

View file

@ -3,10 +3,10 @@
"devenv": { "devenv": {
"locked": { "locked": {
"dir": "src/modules", "dir": "src/modules",
"lastModified": 1753667201, "lastModified": 1768056019,
"owner": "cachix", "owner": "cachix",
"repo": "devenv", "repo": "devenv",
"rev": "4d584d7686a50387f975879788043e55af9f0ad4", "rev": "9bfc4a64c3a798ed8fa6cee3a519a9eac5e73cb5",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -19,14 +19,14 @@
"flake-compat": { "flake-compat": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1747046372, "lastModified": 1767039857,
"owner": "edolstra", "owner": "NixOS",
"repo": "flake-compat", "repo": "flake-compat",
"rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885", "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "edolstra", "owner": "NixOS",
"repo": "flake-compat", "repo": "flake-compat",
"type": "github" "type": "github"
} }
@ -40,10 +40,10 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1750779888, "lastModified": 1767281941,
"owner": "cachix", "owner": "cachix",
"repo": "git-hooks.nix", "repo": "git-hooks.nix",
"rev": "16ec914f6fb6f599ce988427d9d94efddf25fe6d", "rev": "f0927703b7b1c8d97511c4116eb9b4ec6645a0fa",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -60,10 +60,10 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1709087332, "lastModified": 1762808025,
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "gitignore.nix", "repo": "gitignore.nix",
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394", "rev": "cb5e3fdca1de58ccbc3ef53de65bd372b48f567c",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -74,10 +74,10 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1753432016, "lastModified": 1767995494,
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "6027c30c8e9810896b92429f0092f624f7b1aace", "rev": "45a1530683263666f42d1de4cdda328109d5a676",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -15,6 +15,12 @@
pkgs.samply # code profiler pkgs.samply # code profiler
pkgs.feedgnuplot # to visualize bots training results pkgs.feedgnuplot # to visualize bots training results
# --- AI training with python ---
# generate python classes from rust code
pkgs.maturin
# required by python numpy
pkgs.libz
# for bevy # for bevy
pkgs.alsa-lib pkgs.alsa-lib
pkgs.udev pkgs.udev
@ -47,6 +53,25 @@
# https://devenv.sh/languages/ # https://devenv.sh/languages/
languages.rust.enable = true; languages.rust.enable = true;
# AI training with python
enterShell = ''
PYTHONPATH=$PYTHONPATH:$PWD/.devenv/state/venv/lib/python3/site-packages
'';
languages.python = {
enable = true;
uv.enable = true;
venv.enable = true;
venv.requirements = "
pip
gymnasium
numpy
stable-baselines3
shimmy
";
};
# https://devenv.sh/scripts/ # https://devenv.sh/scripts/
# scripts.hello.exec = "echo hello from $GREET"; # scripts.hello.exec = "echo hello from $GREET";

24
doc/ai/history/beads.md Normal file
View file

@ -0,0 +1,24 @@
```sh
bd init
Repository ID: d5459d4d
Clone ID: 73ab432945c43882
✓ Created AGENTS.md with landing-the-plane instructions
✓ bd initialized successfully!
Database: .beads/beads.db
Issue prefix: trictrac
Issues will be named: trictrac-<hash> (e.g., trictrac-a3f2dd)
Run bd quickstart to get started.
⚠ Setup incomplete. Some issues were detected:
• Git Hooks: Missing 1 recommended hook(s)
• Sync Divergence: 1 sync divergence issue(s) detected
• Claude Integration: Not configured
• Git Working Tree: Uncommitted changes present
• Version Tracking: Version tracking not initialized
• Sync Branch Config: sync-branch not configured
Run bd doctor --fix to see details and fix these issues.
```

View file

@ -53,6 +53,10 @@ Client
### Epic : Bot ### Epic : Bot
- PGX
- https://joe-antognini.github.io/ml/jax-tic-tac-toe
- https://www.sotets.uk/pgx/api_usage/
- OpenAi gym - OpenAi gym
- doc gymnasium <https://gymnasium.farama.org/introduction/basic_usage/> - doc gymnasium <https://gymnasium.farama.org/introduction/basic_usage/>
- Rust implementation for OpenAi gym <https://github.com/MathisWellmann/gym-rs> - Rust implementation for OpenAi gym <https://github.com/MathisWellmann/gym-rs>

31
doc/python.md Normal file
View file

@ -0,0 +1,31 @@
# Python bindings
## Génération bindings
```sh
# Generate trictrac python lib as a wheel
maturin build -m store/Cargo.toml --release
# Install wheel in local python env
pip install --no-deps --force-reinstall --prefix .devenv/state/venv target/wheels/*.whl
```
## Usage
Pour vérifier l'accès à la lib : lancer le shell interactif `python`
```python
Python 3.13.11 (main, Dec 5 2025, 16:06:33) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import store
>>> game = store.TricTrac()
>>> game.get_active_player_id()
1
```
### Appels depuis python
`python bot/python/test.py`
## Interfaces
## Entraînement

View file

@ -20,6 +20,7 @@ profile:
cargo build --profile profiling cargo build --profile profiling
samply record ./target/profiling/client_cli --bot dummy,dummy samply record ./target/profiling/client_cli --bot dummy,dummy
pythonlib: pythonlib:
rm -rf target/wheels
maturin build -m store/Cargo.toml --release maturin build -m store/Cargo.toml --release
pip install --no-deps --force-reinstall --prefix .devenv/state/venv target/wheels/*.whl pip install --no-deps --force-reinstall --prefix .devenv/state/venv target/wheels/*.whl
trainbot algo: trainbot algo:

View file

@ -7,14 +7,17 @@ edition = "2021"
[lib] [lib]
name = "store" name = "store"
# "cdylib" is necessary to produce a shared library for Python to import from.
# Only "rlib" is needed for other Rust crates to use this library # Only "rlib" is needed for other Rust crates to use this library
crate-type = ["rlib"] crate-type = ["cdylib", "rlib"]
[dependencies] [dependencies]
base64 = "0.21.7" base64 = "0.21.7"
# provides macros for creating log messages to be used by a logger (for example env_logger) # provides macros for creating log messages to be used by a logger (for example env_logger)
log = "0.4.20" log = "0.4.20"
merge = "0.1.0" merge = "0.1.0"
# generate python lib (with maturin) to be used in AI training
pyo3 = { version = "0.23", features = ["extension-module", "abi3-py38"] }
rand = "0.8.5" rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
transpose = "0.2.2" transpose = "0.2.2"

9
store/pyproject.toml Normal file
View file

@ -0,0 +1,9 @@
[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"

View file

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

View file

@ -1,9 +1,11 @@
use pyo3::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
// This just makes it easier to dissern between a player id and any ol' u64 // This just makes it easier to dissern between a player id and any ol' u64
pub type PlayerId = u64; pub type PlayerId = u64;
#[pyclass(eq, eq_int)]
#[derive(Copy, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Copy, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Color { pub enum Color {
White, White,

230
store/src/pyengine.rs Normal file
View file

@ -0,0 +1,230 @@
//! # Expose trictrac game state and rules in a python module
use pyo3::prelude::*;
use pyo3::types::PyDict;
use crate::board::CheckerMove;
use crate::dice::{Dice, DiceRoller};
use crate::game::{GameEvent, GameState, Stage, TurnStage};
use crate::game_rules_moves::MoveRules;
use crate::game_rules_points::PointsRules;
use crate::player::{Color, PlayerId};
#[pyclass]
struct TricTrac {
game_state: GameState,
dice_roll_sequence: Vec<(u8, u8)>,
current_dice_index: usize,
}
#[pymethods]
impl TricTrac {
#[new]
fn new() -> Self {
let mut game_state = GameState::new(false); // schools_enabled = false
// Initialiser 2 joueurs
game_state.init_player("player1");
game_state.init_player("player2");
// Commencer la partie avec le joueur 1
game_state.consume(&GameEvent::BeginGame { goes_first: 1 });
TricTrac {
game_state,
dice_roll_sequence: Vec::new(),
current_dice_index: 0,
}
}
/// Obtenir l'état du jeu sous forme de dictionnaire
fn get_state_dict<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new(py);
dict.set_item("stage", format!("{:?}", self.game_state.stage))?;
dict.set_item("turn_stage", format!("{:?}", self.game_state.turn_stage))?;
dict.set_item("active_player_id", self.game_state.active_player_id)?;
// Board
let board_list = self.game_state.board.to_vec(); // returns Vec<i8>
dict.set_item("board", board_list)?;
// Dice
dict.set_item("dice", (self.game_state.dice.values.0, self.game_state.dice.values.1))?;
// Players
let players_dict = PyDict::new(py);
for (id, player) in &self.game_state.players {
let p_dict = PyDict::new(py);
p_dict.set_item("color", format!("{:?}", player.color))?;
p_dict.set_item("holes", player.holes)?;
p_dict.set_item("points", player.points)?;
p_dict.set_item("can_bredouille", player.can_bredouille)?;
p_dict.set_item("dice_roll_count", player.dice_roll_count)?;
players_dict.set_item(id, p_dict)?;
}
dict.set_item("players", players_dict)?;
Ok(dict)
}
/// Lance les dés ou utilise la séquence prédéfinie
fn roll_dice(&mut self) -> PyResult<(u8, u8)> {
let player_id = self.game_state.active_player_id;
if self.game_state.turn_stage != TurnStage::RollDice {
return Err(pyo3::exceptions::PyRuntimeError::new_err("Not in RollDice stage"));
}
self.game_state.consume(&GameEvent::Roll { player_id });
let dice = if self.current_dice_index < self.dice_roll_sequence.len() {
let vals = self.dice_roll_sequence[self.current_dice_index];
self.current_dice_index += 1;
Dice { values: vals }
} else {
DiceRoller::default().roll()
};
self.game_state.consume(&GameEvent::RollResult { player_id, dice });
Ok(dice.values)
}
/// Applique un mouvement (deux déplacements de dames)
fn apply_move(&mut self, from1: usize, to1: usize, from2: usize, to2: usize) -> PyResult<()> {
let player_id = self.game_state.active_player_id;
let m1 = CheckerMove::new(from1, to1).map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
let m2 = CheckerMove::new(from2, to2).map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
let moves = (m1, m2);
if !self.game_state.validate(&GameEvent::Move { player_id, moves }) {
return Err(pyo3::exceptions::PyValueError::new_err("Invalid move"));
}
self.game_state.consume(&GameEvent::Move { player_id, moves });
Ok(())
}
/// Obtenir l'état du jeu sous forme de chaîne de caractères compacte
fn get_state_id(&self) -> String {
self.game_state.to_string_id()
}
/// Renvoie les positions des pièces pour un joueur spécifique
fn get_checker_positions(&self, color: Color) -> Vec<(usize, i8)> {
self.game_state.board.get_color_fields(color)
}
/// Obtenir la liste des mouvements légaux sous forme de paires (from, to)
fn get_available_moves(&self) -> Vec<((usize, usize), (usize, usize))> {
// L'agent joue toujours le joueur actif
let color = self
.game_state
.player_color_by_id(&self.game_state.active_player_id)
.unwrap_or(Color::White);
// Si ce n'est pas le moment de déplacer les pièces, retourner une liste vide
if self.game_state.turn_stage != TurnStage::Move
&& self.game_state.turn_stage != TurnStage::HoldOrGoChoice
{
return vec![];
}
let rules = MoveRules::new(&color, &self.game_state.board, self.game_state.dice);
let possible_moves = rules.get_possible_moves_sequences(true, vec![]);
// Convertir les mouvements CheckerMove en tuples (from, to) pour Python
possible_moves
.into_iter()
.map(|(move1, move2)| {
(
(move1.get_from(), move1.get_to()),
(move2.get_from(), move2.get_to()),
)
})
.collect()
}
/// Calcule les points maximaux que le joueur actif peut obtenir avec les dés actuels
fn calculate_points(&self) -> u8 {
let active_player = self
.game_state
.players
.get(&self.game_state.active_player_id);
if let Some(player) = active_player {
let dice_roll_count = player.dice_roll_count;
let color = player.color;
let points_rules =
PointsRules::new(&color, &self.game_state.board, self.game_state.dice);
let (points, _) = points_rules.get_points(dice_roll_count);
points
} else {
0
}
}
/// Réinitialise la partie
fn reset(&mut self) {
self.game_state = GameState::new(false);
// Initialiser 2 joueurs
self.game_state.init_player("player1");
self.game_state.init_player("player2");
// Commencer la partie avec le joueur 1
self.game_state
.consume(&GameEvent::BeginGame { goes_first: 1 });
// Réinitialiser l'index de la séquence de dés
self.current_dice_index = 0;
}
/// Vérifie si la partie est terminée
fn is_done(&self) -> bool {
self.game_state.stage == Stage::Ended || self.game_state.determine_winner().is_some()
}
/// Obtenir le gagnant de la partie
fn get_winner(&self) -> Option<PlayerId> {
self.game_state.determine_winner()
}
/// Obtenir le score du joueur actif (nombre de trous)
fn get_score(&self, player_id: PlayerId) -> i32 {
if let Some(player) = self.game_state.players.get(&player_id) {
player.holes as i32
} else {
-1
}
}
/// Obtenir l'ID du joueur actif
fn get_active_player_id(&self) -> PlayerId {
self.game_state.active_player_id
}
/// Définir une séquence de dés à utiliser (pour la reproductibilité)
fn set_dice_sequence(&mut self, sequence: Vec<(u8, u8)>) {
self.dice_roll_sequence = sequence;
self.current_dice_index = 0;
}
/// Afficher l'état du jeu (pour le débogage)
fn __str__(&self) -> String {
format!("{}", self.game_state)
}
}
/// 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 store(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<TricTrac>()?;
Ok(())
}