From 71992ccf7cc35c54107f83374c1267113bbd0f1a Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Tue, 19 Nov 2024 17:28:18 +0100 Subject: [PATCH] bots : initialization --- client_cli/src/app.rs | 10 ++++----- client_cli/src/game_runner.rs | 40 ++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/client_cli/src/app.rs b/client_cli/src/app.rs index 6685657..59b8952 100644 --- a/client_cli/src/app.rs +++ b/client_cli/src/app.rs @@ -1,7 +1,7 @@ use bot::{BotStrategy, DefaultStrategy}; use itertools::Itertools; -use crate::game_runner::Game; +use crate::game_runner::GameRunner; use store::{CheckerMove, GameEvent, GameState, Stage, TurnStage}; #[derive(Debug, Default)] @@ -16,8 +16,7 @@ pub struct App { // should the application exit? pub should_quit: bool, pub schools_enabled: bool, - pub game: Game, - pub bot_strategies: Vec>, + pub game: GameRunner, } impl App { @@ -40,10 +39,9 @@ impl App { .unwrap_or_default(); let schools_enabled = false; Self { - game: Game::new(schools_enabled, args.seed.map(|s| s as u64)), + game: GameRunner::new(schools_enabled, bot_strategies, args.seed.map(|s| s as u64)), should_quit: false, schools_enabled, - bot_strategies, } } @@ -298,7 +296,7 @@ Player :: holes :: points "; let mut app = App::new(AppArgs { seed: Some(1327), - bot: None, + bot: Some("dummy".into()), }); app.input("roll"); app.input("1 3"); diff --git a/client_cli/src/game_runner.rs b/client_cli/src/game_runner.rs index 76db032..2ce304b 100644 --- a/client_cli/src/game_runner.rs +++ b/client_cli/src/game_runner.rs @@ -1,35 +1,46 @@ -use bot::{Bot, DefaultStrategy}; +use bot::{Bot, BotStrategy, DefaultStrategy}; use store::{CheckerMove, DiceRoller, GameEvent, GameState, PlayerId, TurnStage}; // Application Game #[derive(Debug, Default)] -pub struct Game { +pub struct GameRunner { pub state: GameState, pub dice_roller: DiceRoller, pub first_move: Option, pub player_id: Option, - bot: Bot, + bots: Vec, } -impl Game { +impl GameRunner { // Constructs a new instance of [`App`]. - pub fn new(schools_enabled: bool, seed: Option) -> Self { + pub fn new( + schools_enabled: bool, + bot_strategies: Vec>, + seed: Option, + ) -> Self { let mut state = GameState::new(schools_enabled); // local : player let player_id: Option = state.init_player("myself"); - // bot - let bot_id: PlayerId = state.init_player("bot").unwrap(); - let bot_color = state.player_color_by_id(&bot_id).unwrap(); - let bot_strategy = Box::new(DefaultStrategy::default()); + + // bots + let bots = bot_strategies + .into_iter() + .map(|strategy| { + let bot_id: PlayerId = state.init_player("bot").unwrap(); + let bot_color = state.player_color_by_id(&bot_id).unwrap(); + Bot::new(strategy, bot_color) + }) + .collect(); + // let bot_strategy = Box::new(DefaultStrategy::default()); // let bot: Bot = Bot::new(bot_strategy, bot_color, schools_enabled); - let bot: Bot = Bot::new(bot_strategy, bot_color); + // let bot: Bot = Bot::new(bot_strategy, bot_color); let mut game = Self { state, dice_roller: DiceRoller::new(seed), first_move: None, player_id, - bot, + bots, }; game.handle_event(&GameEvent::BeginGame { goes_first: player_id.unwrap(), @@ -44,15 +55,14 @@ impl Game { // println!("consuming {:?}", event); self.state.consume(event); // chain all successive bot actions - let bot_event = self - .bot + let bot_event = self.bots[0] .handle_event(event) .and_then(|evt| self.handle_event(&evt)); // roll dice for bot if needed if self.bot_needs_dice_roll() { let dice = self.dice_roller.roll(); self.handle_event(&GameEvent::RollResult { - player_id: self.bot.player_id, + player_id: self.bots[0].player_id, dice, }) } else { @@ -61,7 +71,7 @@ impl Game { } fn bot_needs_dice_roll(&self) -> bool { - self.state.active_player_id == self.bot.player_id + self.state.active_player_id == self.bots[0].player_id && self.state.turn_stage == TurnStage::RollWaiting } }