bot : erroneous strategy

This commit is contained in:
Henri Bourcereau 2025-03-18 21:19:57 +01:00
parent ab770f3a34
commit 27fc08c47d
6 changed files with 118 additions and 47 deletions

View file

@ -2,6 +2,7 @@ mod strategy;
use store::{CheckerMove, Color, GameEvent, GameState, PlayerId, PointsRules, Stage, TurnStage};
pub use strategy::default::DefaultStrategy;
pub use strategy::erroneous_moves::ErroneousStrategy;
pub use strategy::stable_baselines3::StableBaselines3Strategy;
pub trait BotStrategy: std::fmt::Debug {

View file

@ -1,3 +1,4 @@
pub mod client;
pub mod default;
pub mod erroneous_moves;
pub mod stable_baselines3;

View file

@ -0,0 +1,63 @@
use crate::{BotStrategy, CheckerMove, Color, GameState, PlayerId, PointsRules};
use store::MoveRules;
#[derive(Debug)]
pub struct ErroneousStrategy {
pub game: GameState,
pub player_id: PlayerId,
pub color: Color,
}
impl Default for ErroneousStrategy {
fn default() -> Self {
let game = GameState::default();
Self {
game,
player_id: 2,
color: Color::Black,
}
}
}
impl BotStrategy for ErroneousStrategy {
fn get_game(&self) -> &GameState {
&self.game
}
fn get_mut_game(&mut self) -> &mut GameState {
&mut self.game
}
fn set_color(&mut self, color: Color) {
self.color = color;
}
fn set_player_id(&mut self, player_id: PlayerId) {
self.player_id = player_id;
}
fn calculate_points(&self) -> u8 {
let dice_roll_count = self
.get_game()
.players
.get(&self.player_id)
.unwrap()
.dice_roll_count;
let points_rules = PointsRules::new(&Color::White, &self.game.board, self.game.dice);
points_rules.get_points(dice_roll_count).0
}
fn calculate_adv_points(&self) -> u8 {
self.calculate_points()
}
fn choose_go(&self) -> bool {
true
}
fn choose_move(&self) -> (CheckerMove, CheckerMove) {
(
CheckerMove::new(1, 10).unwrap(),
CheckerMove::new(2, 7).unwrap(),
)
}
}

View file

@ -1,4 +1,4 @@
use bot::{BotStrategy, DefaultStrategy, StableBaselines3Strategy};
use bot::{BotStrategy, DefaultStrategy, ErroneousStrategy, StableBaselines3Strategy};
use itertools::Itertools;
use crate::game_runner::GameRunner;
@ -22,28 +22,31 @@ pub struct App {
impl App {
// Constructs a new instance of [`App`].
pub fn new(args: AppArgs) -> Self {
let bot_strategies: Vec<Box<dyn BotStrategy>> = args
.bot
.as_deref()
.map(|str_bots| {
str_bots
.split(",")
.filter_map(|s| match s.trim() {
"dummy" => {
Some(Box::new(DefaultStrategy::default()) as Box<dyn BotStrategy>)
}
"ai" => {
Some(Box::new(StableBaselines3Strategy::default()) as Box<dyn BotStrategy>)
}
s if s.starts_with("ai:") => {
let path = s.trim_start_matches("ai:");
Some(Box::new(StableBaselines3Strategy::new(path)) as Box<dyn BotStrategy>)
}
_ => None,
})
.collect()
})
.unwrap_or_default();
let bot_strategies: Vec<Box<dyn BotStrategy>> =
args.bot
.as_deref()
.map(|str_bots| {
str_bots
.split(",")
.filter_map(|s| match s.trim() {
"dummy" => {
Some(Box::new(DefaultStrategy::default()) as Box<dyn BotStrategy>)
}
"erroneous" => {
Some(Box::new(ErroneousStrategy::default()) as Box<dyn BotStrategy>)
}
"ai" => Some(Box::new(StableBaselines3Strategy::default())
as Box<dyn BotStrategy>),
s if s.starts_with("ai:") => {
let path = s.trim_start_matches("ai:");
Some(Box::new(StableBaselines3Strategy::new(path))
as Box<dyn BotStrategy>)
}
_ => None,
})
.collect()
})
.unwrap_or_default();
let schools_enabled = false;
let should_quit = bot_strategies.len() > 1;
Self {

View file

@ -31,10 +31,31 @@
"type": "github"
}
},
"git-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1742058297,
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "59f17850021620cd348ad2e9c0c64f4e6325ce2a",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"git-hooks",
"nixpkgs"
]
},
@ -66,32 +87,14 @@
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1740915799,
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "42b1ba089d2034d910566bf6b40830af6b8ec732",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"devenv": "devenv",
"git-hooks": "git-hooks",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
"pre-commit-hooks": [
"git-hooks"
]
}
}
},

View file

@ -9,7 +9,7 @@ shell:
runcli:
RUST_LOG=info cargo run --bin=client_cli
runclibots:
RUST_LOG=info cargo run --bin=client_cli -- --bot dummy,dummy
RUST_LOG=info cargo run --bin=client_cli -- --bot dummy,ai
profile:
echo '1' | sudo tee /proc/sys/kernel/perf_event_paranoid
cargo build --profile profiling