From e66d8b66244b280e9f34c6fbe8d45b2aa5498883 Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Mon, 1 Sep 2025 14:47:09 +0200 Subject: [PATCH] feat: TrictracBoard for kZero --- bot/Cargo.toml | 2 +- bot/src/training_common.rs | 9 +++++---- bot/src/trictrac_board.rs | 7 ++++++- doc/refs/inspirations.md | 2 ++ store/src/game.rs | 10 ++++++++-- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bot/Cargo.toml b/bot/Cargo.toml index 21e0128..ce1ee5a 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -20,7 +20,7 @@ serde_json = "1.0" store = { path = "../store" } rand = "0.8" env_logger = "0.10" -burn = { version = "0.17", features = ["ndarray", "autodiff"] } +burn = { version = "0.18", features = ["ndarray", "autodiff"] } burn-rl = { git = "https://github.com/yunjhongwu/burn-rl-examples.git", package = "burn-rl" } log = "0.4.20" confy = "1.0.0" diff --git a/bot/src/training_common.rs b/bot/src/training_common.rs index 0a581dd..aa54c16 100644 --- a/bot/src/training_common.rs +++ b/bot/src/training_common.rs @@ -4,6 +4,10 @@ use std::fmt::{Debug, Display, Formatter}; use serde::{Deserialize, Serialize}; use store::{CheckerMove, GameEvent, GameState}; +// 1 (Roll) + 1 (Go) + mouvements possibles +// Pour les mouvements : 2*16*16 = 514 (choix du dé + choix de la dame 0-15 pour chaque from) +pub const ACTION_SPACE_SIZE: usize = 514; + /// Types d'actions possibles dans le jeu #[derive(Debug, Copy, Clone, Eq, Serialize, Deserialize, PartialEq)] pub enum TrictracAction { @@ -158,10 +162,7 @@ impl TrictracAction { /// Retourne la taille de l'espace d'actions total pub fn action_space_size() -> usize { - // 1 (Roll) + 1 (Go) + mouvements possibles - // Pour les mouvements : 2*25*25 = 1250 (choix du dé + position 0-24 pour chaque from) - // Mais on peut optimiser en limitant aux positions valides (1-24) - 2 + (2 * 16 * 16) // = 514 + ACTION_SPACE_SIZE } // pub fn to_game_event(&self, player_id: PlayerId, dice: Dice) -> GameEvent { diff --git a/bot/src/trictrac_board.rs b/bot/src/trictrac_board.rs index 80350b8..5e9fc7f 100644 --- a/bot/src/trictrac_board.rs +++ b/bot/src/trictrac_board.rs @@ -6,10 +6,11 @@ use board_game::board::{ use board_game::impl_unit_symmetry_board; use internal_iterator::InternalIterator; use std::fmt; +use std::hash::Hash; use std::ops::ControlFlow; use store::Color; -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct TrictracBoard(crate::GameState); impl Default for TrictracBoard { @@ -78,6 +79,10 @@ impl BoardGameBoard for TrictracBoard { } impl TrictracBoard { + pub fn inner(&self) -> &crate::GameState { + &self.0 + } + pub fn to_fen(&self) -> String { self.0.to_string_id() } diff --git a/doc/refs/inspirations.md b/doc/refs/inspirations.md index 2217ed7..a94aa87 100644 --- a/doc/refs/inspirations.md +++ b/doc/refs/inspirations.md @@ -22,6 +22,7 @@ nombres aléatoires avec seed : parallel ) - front : yew + tauri + - egui - @@ -31,6 +32,7 @@ nombres aléatoires avec seed : - (wasm, rooms) - +- ## Others diff --git a/store/src/game.rs b/store/src/game.rs index 5506824..09ea3f3 100644 --- a/store/src/game.rs +++ b/store/src/game.rs @@ -9,12 +9,13 @@ use log::{debug, error}; // use itertools::Itertools; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use std::hash::{Hash, Hasher}; use std::{fmt, str}; use base64::{engine::general_purpose, Engine as _}; /// The different stages a game can be in. (not to be confused with the entire "GameState") -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum Stage { PreGame, InGame, @@ -22,7 +23,7 @@ pub enum Stage { } /// The different stages a game turn can be in. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum TurnStage { RollDice, RollWaiting, @@ -114,6 +115,11 @@ impl Default for GameState { } } } +impl Hash for GameState { + fn hash(&self, state: &mut H) { + self.to_string_id().hash(state); + } +} impl GameState { /// Create a new default game