feat: python store bindings (wip)
This commit is contained in:
parent
11b75eb34d
commit
d2d6d2ef98
3 changed files with 28 additions and 59 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import trictrac_store
|
import trictrac_store
|
||||||
|
|
||||||
game = trictrac_store.TricTrac()
|
game = trictrac_store.TricTrac()
|
||||||
print(game.get_active_player_id())
|
print(game.current_player_idx())
|
||||||
print(game.get_legal_actions())
|
print(game.get_legal_actions(game.current_player_idx()))
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use crate::game::{GameEvent, GameState, Stage, TurnStage};
|
||||||
use crate::game_rules_moves::MoveRules;
|
use crate::game_rules_moves::MoveRules;
|
||||||
use crate::game_rules_points::PointsRules;
|
use crate::game_rules_points::PointsRules;
|
||||||
use crate::player::{Color, PlayerId};
|
use crate::player::{Color, PlayerId};
|
||||||
use crate::training_common::get_valid_action_indices;
|
use crate::training_common::{get_valid_action_indices, TrictracAction};
|
||||||
|
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
struct TricTrac {
|
struct TricTrac {
|
||||||
|
|
@ -50,12 +50,21 @@ impl TricTrac {
|
||||||
self.game_state.active_player_id - 1
|
self.game_state.active_player_id - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_legal_actions(&self) -> Vec<usize> {
|
fn get_legal_actions(&self, player_id: u64) -> Vec<usize> {
|
||||||
get_valid_action_indices(&self.game_state)
|
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 action_to_string(&self, player_idx: u64, action_idx: usize) -> String {
|
||||||
fn roll_dice(&mut self) -> PyResult<(u8, u8)> {
|
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;
|
let player_id = self.game_state.active_player_id;
|
||||||
|
|
||||||
if self.game_state.turn_stage != TurnStage::RollDice {
|
if self.game_state.turn_stage != TurnStage::RollDice {
|
||||||
|
|
@ -65,42 +74,24 @@ impl TricTrac {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.game_state.consume(&GameEvent::Roll { player_id });
|
self.game_state.consume(&GameEvent::Roll { player_id });
|
||||||
|
let dice = Dice { values: dices };
|
||||||
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
|
self.game_state
|
||||||
.consume(&GameEvent::RollResult { player_id, dice });
|
.consume(&GameEvent::RollResult { player_id, dice });
|
||||||
|
Ok(())
|
||||||
Ok(dice.values)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applique un mouvement (deux déplacements de dames)
|
fn apply_action(&mut self, action_idx: usize) {
|
||||||
fn apply_move(&mut self, from1: usize, to1: usize, from2: usize, to2: usize) -> PyResult<()> {
|
if let Some(event) =
|
||||||
let player_id = self.game_state.active_player_id;
|
TrictracAction::from_action_index(action_idx).and_then(|a| a.to_event(&self.game_state))
|
||||||
|
|
||||||
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"));
|
if self.game_state.validate(&event) {
|
||||||
|
self.game_state.consume(&event);
|
||||||
|
// return Ok(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Err(pyo3::exceptions::PyRuntimeError::new_err(
|
||||||
self.game_state
|
// "Could not apply action",
|
||||||
.consume(&GameEvent::Move { player_id, moves });
|
// ))
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Obtenir l'état du jeu sous forme de chaîne de caractères compacte
|
/// Obtenir l'état du jeu sous forme de chaîne de caractères compacte
|
||||||
|
|
|
||||||
|
|
@ -167,28 +167,6 @@ impl TrictracAction {
|
||||||
pub fn action_space_size() -> usize {
|
pub fn action_space_size() -> usize {
|
||||||
ACTION_SPACE_SIZE
|
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
|
/// Obtient les actions valides pour l'état de jeu actuel
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue