From e9f4940c40e3518d57ca8609a44a5f25f5e85aea Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Thu, 7 Nov 2024 16:51:33 +0100 Subject: [PATCH] refact: bot strategies --- bot/src/lib.rs | 66 ++----------------------------------- bot/src/strategy.rs | 2 ++ bot/src/strategy/client.rs | 64 +++++++++++++++++++++++++++++++++++ bot/src/strategy/default.rs | 64 +++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 64 deletions(-) create mode 100644 bot/src/strategy.rs create mode 100644 bot/src/strategy/client.rs create mode 100644 bot/src/strategy/default.rs diff --git a/bot/src/lib.rs b/bot/src/lib.rs index 6b39bda..e6766b3 100644 --- a/bot/src/lib.rs +++ b/bot/src/lib.rs @@ -1,6 +1,7 @@ -mod bot; +mod strategy; use store::{CheckerMove, Color, GameEvent, GameState, PlayerId, PointsRules, TurnStage}; +pub use strategy::default::DefaultStrategy; pub trait BotStrategy: std::fmt::Debug { fn get_game(&self) -> &GameState; @@ -15,69 +16,6 @@ pub trait BotStrategy: std::fmt::Debug { } } -#[derive(Debug)] -pub struct DefaultStrategy { - pub game: GameState, - pub player_id: PlayerId, - pub color: Color, -} - -impl Default for DefaultStrategy { - fn default() -> Self { - let game = GameState::default(); - Self { - game, - player_id: 2, - color: Color::Black, - } - } -} - -impl BotStrategy for DefaultStrategy { - fn get_game(&self) -> &GameState { - &self.game - } - fn get_mut_game(&mut self) -> &mut GameState { - &mut self.game - } - - 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_move(&self) -> (CheckerMove, CheckerMove) { - let (dice1, dice2) = match self.color { - Color::White => (self.game.dice.values.0 as i8, self.game.dice.values.1 as i8), - Color::Black => ( - 0 - self.game.dice.values.0 as i8, - 0 - self.game.dice.values.1 as i8, - ), - }; - - let fields = self.game.board.get_color_fields(self.color); - let first_field = fields.first().unwrap(); - ( - CheckerMove::new(first_field.0, (first_field.0 as i8 + dice1) as usize).unwrap(), - CheckerMove::new(first_field.0, (first_field.0 as i8 + dice2) as usize).unwrap(), - ) - } -} - #[derive(Debug)] pub struct Bot { pub player_id: PlayerId, diff --git a/bot/src/strategy.rs b/bot/src/strategy.rs new file mode 100644 index 0000000..d1e88f8 --- /dev/null +++ b/bot/src/strategy.rs @@ -0,0 +1,2 @@ +pub mod client; +pub mod default; diff --git a/bot/src/strategy/client.rs b/bot/src/strategy/client.rs new file mode 100644 index 0000000..90b13bf --- /dev/null +++ b/bot/src/strategy/client.rs @@ -0,0 +1,64 @@ +use crate::{BotStrategy, CheckerMove, Color, GameState, PlayerId, PointsRules}; + +#[derive(Debug)] +pub struct ClientStrategy { + pub game: GameState, + pub player_id: PlayerId, + pub color: Color, +} + +impl Default for ClientStrategy { + fn default() -> Self { + let game = GameState::default(); + Self { + game, + player_id: 1, + color: Color::White, + } + } +} + +impl BotStrategy for ClientStrategy { + fn get_game(&self) -> &GameState { + &self.game + } + fn get_mut_game(&mut self) -> &mut GameState { + &mut self.game + } + + 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_move(&self) -> (CheckerMove, CheckerMove) { + let (dice1, dice2) = match self.color { + Color::White => (self.game.dice.values.0 as i8, self.game.dice.values.1 as i8), + Color::Black => ( + 0 - self.game.dice.values.0 as i8, + 0 - self.game.dice.values.1 as i8, + ), + }; + + let fields = self.game.board.get_color_fields(self.color); + let first_field = fields.first().unwrap(); + ( + CheckerMove::new(first_field.0, (first_field.0 as i8 + dice1) as usize).unwrap(), + CheckerMove::new(first_field.0, (first_field.0 as i8 + dice2) as usize).unwrap(), + ) + } +} diff --git a/bot/src/strategy/default.rs b/bot/src/strategy/default.rs new file mode 100644 index 0000000..53a96de --- /dev/null +++ b/bot/src/strategy/default.rs @@ -0,0 +1,64 @@ +use crate::{BotStrategy, CheckerMove, Color, GameState, PlayerId, PointsRules}; + +#[derive(Debug)] +pub struct DefaultStrategy { + pub game: GameState, + pub player_id: PlayerId, + pub color: Color, +} + +impl Default for DefaultStrategy { + fn default() -> Self { + let game = GameState::default(); + Self { + game, + player_id: 2, + color: Color::Black, + } + } +} + +impl BotStrategy for DefaultStrategy { + fn get_game(&self) -> &GameState { + &self.game + } + fn get_mut_game(&mut self) -> &mut GameState { + &mut self.game + } + + 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_move(&self) -> (CheckerMove, CheckerMove) { + let (dice1, dice2) = match self.color { + Color::White => (self.game.dice.values.0 as i8, self.game.dice.values.1 as i8), + Color::Black => ( + 0 - self.game.dice.values.0 as i8, + 0 - self.game.dice.values.1 as i8, + ), + }; + + let fields = self.game.board.get_color_fields(self.color); + let first_field = fields.first().unwrap(); + ( + CheckerMove::new(first_field.0, (first_field.0 as i8 + dice1) as usize).unwrap(), + CheckerMove::new(first_field.0, (first_field.0 as i8 + dice2) as usize).unwrap(), + ) + } +}