feat: python store bindings (wip)

This commit is contained in:
Henri Bourcereau 2026-02-08 18:57:20 +01:00
parent c503771e76
commit 5eda97f461
3 changed files with 28 additions and 59 deletions

View file

@ -1,5 +1,5 @@
import trictrac_store
game = trictrac_store.TricTrac()
print(game.get_active_player_id())
print(game.get_legal_actions())
print(game.current_player_idx())
print(game.get_legal_actions(game.current_player_idx()))

View file

@ -8,7 +8,7 @@ use crate::game::{GameEvent, GameState, Stage, TurnStage};
use crate::game_rules_moves::MoveRules;
use crate::game_rules_points::PointsRules;
use crate::player::{Color, PlayerId};
use crate::training_common::get_valid_action_indices;
use crate::training_common::{get_valid_action_indices, TrictracAction};
#[pyclass]
struct TricTrac {
@ -50,12 +50,21 @@ impl TricTrac {
self.game_state.active_player_id - 1
}
fn get_legal_actions(&self) -> Vec<usize> {
get_valid_action_indices(&self.game_state)
fn get_legal_actions(&self, player_id: u64) -> Vec<usize> {
if player_id == self.current_player_idx() {
get_valid_action_indices(&self.game_state)
} else {
vec![]
}
}
/// Lance les dés ou utilise la séquence prédéfinie
fn roll_dice(&mut self) -> PyResult<(u8, u8)> {
fn action_to_string(&self, player_idx: u64, action_idx: usize) -> String {
TrictracAction::from_action_index(action_idx)
.map(|a| a.to_string())
.unwrap_or("unknown action".into())
}
fn apply_dice_roll(&mut self, dices: (u8, u8)) -> PyResult<()> {
let player_id = self.game_state.active_player_id;
if self.game_state.turn_stage != TurnStage::RollDice {
@ -65,42 +74,24 @@ impl TricTrac {
}
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()
};
let dice = Dice { values: dices };
self.game_state
.consume(&GameEvent::RollResult { player_id, dice });
Ok(dice.values)
Ok(())
}
/// 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 })
fn apply_action(&mut self, action_idx: usize) {
if let Some(event) =
TrictracAction::from_action_index(action_idx).and_then(|a| a.to_event(&self.game_state))
{
return Err(pyo3::exceptions::PyValueError::new_err("Invalid move"));
if self.game_state.validate(&event) {
self.game_state.consume(&event);
// return Ok(());
}
}
self.game_state
.consume(&GameEvent::Move { player_id, moves });
Ok(())
// Err(pyo3::exceptions::PyRuntimeError::new_err(
// "Could not apply action",
// ))
}
/// Obtenir l'état du jeu sous forme de chaîne de caractères compacte

View file

@ -167,28 +167,6 @@ impl TrictracAction {
pub fn action_space_size() -> usize {
ACTION_SPACE_SIZE
}
// pub fn to_game_event(&self, player_id: PlayerId, dice: Dice) -> GameEvent {
// match action {
// TrictracAction::Roll => Some(GameEvent::Roll { player_id }),
// TrictracAction::Mark => Some(GameEvent::Mark { player_id, points }),
// TrictracAction::Go => Some(GameEvent::Go { player_id }),
// TrictracAction::Move {
// dice_order,
// from1,
// from2,
// } => {
// // Effectuer un mouvement
// let checker_move1 = CheckerMove::new(move1.0, move1.1).unwrap_or_default();
// let checker_move2 = CheckerMove::new(move2.0, move2.1).unwrap_or_default();
//
// Some(GameEvent::Move {
// player_id: self.agent_player_id,
// moves: (checker_move1, checker_move2),
// })
// }
// };
// }
}
/// Obtient les actions valides pour l'état de jeu actuel