bots : initialization

This commit is contained in:
Henri Bourcereau 2024-11-19 17:28:18 +01:00
parent 14021ff09b
commit 71992ccf7c
2 changed files with 29 additions and 21 deletions

View file

@ -1,7 +1,7 @@
use bot::{BotStrategy, DefaultStrategy}; use bot::{BotStrategy, DefaultStrategy};
use itertools::Itertools; use itertools::Itertools;
use crate::game_runner::Game; use crate::game_runner::GameRunner;
use store::{CheckerMove, GameEvent, GameState, Stage, TurnStage}; use store::{CheckerMove, GameEvent, GameState, Stage, TurnStage};
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -16,8 +16,7 @@ pub struct App {
// should the application exit? // should the application exit?
pub should_quit: bool, pub should_quit: bool,
pub schools_enabled: bool, pub schools_enabled: bool,
pub game: Game, pub game: GameRunner,
pub bot_strategies: Vec<Box<dyn BotStrategy>>,
} }
impl App { impl App {
@ -40,10 +39,9 @@ impl App {
.unwrap_or_default(); .unwrap_or_default();
let schools_enabled = false; let schools_enabled = false;
Self { 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, should_quit: false,
schools_enabled, schools_enabled,
bot_strategies,
} }
} }
@ -298,7 +296,7 @@ Player :: holes :: points
"; ";
let mut app = App::new(AppArgs { let mut app = App::new(AppArgs {
seed: Some(1327), seed: Some(1327),
bot: None, bot: Some("dummy".into()),
}); });
app.input("roll"); app.input("roll");
app.input("1 3"); app.input("1 3");

View file

@ -1,35 +1,46 @@
use bot::{Bot, DefaultStrategy}; use bot::{Bot, BotStrategy, DefaultStrategy};
use store::{CheckerMove, DiceRoller, GameEvent, GameState, PlayerId, TurnStage}; use store::{CheckerMove, DiceRoller, GameEvent, GameState, PlayerId, TurnStage};
// Application Game // Application Game
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Game { pub struct GameRunner {
pub state: GameState, pub state: GameState,
pub dice_roller: DiceRoller, pub dice_roller: DiceRoller,
pub first_move: Option<CheckerMove>, pub first_move: Option<CheckerMove>,
pub player_id: Option<PlayerId>, pub player_id: Option<PlayerId>,
bot: Bot, bots: Vec<Bot>,
} }
impl Game { impl GameRunner {
// Constructs a new instance of [`App`]. // Constructs a new instance of [`App`].
pub fn new(schools_enabled: bool, seed: Option<u64>) -> Self { pub fn new(
schools_enabled: bool,
bot_strategies: Vec<Box<dyn BotStrategy>>,
seed: Option<u64>,
) -> Self {
let mut state = GameState::new(schools_enabled); let mut state = GameState::new(schools_enabled);
// local : player // local : player
let player_id: Option<PlayerId> = state.init_player("myself"); let player_id: Option<PlayerId> = state.init_player("myself");
// bot
// bots
let bots = bot_strategies
.into_iter()
.map(|strategy| {
let bot_id: PlayerId = state.init_player("bot").unwrap(); let bot_id: PlayerId = state.init_player("bot").unwrap();
let bot_color = state.player_color_by_id(&bot_id).unwrap(); let bot_color = state.player_color_by_id(&bot_id).unwrap();
let bot_strategy = Box::new(DefaultStrategy::default()); 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, schools_enabled);
let bot: Bot = Bot::new(bot_strategy, bot_color); // let bot: Bot = Bot::new(bot_strategy, bot_color);
let mut game = Self { let mut game = Self {
state, state,
dice_roller: DiceRoller::new(seed), dice_roller: DiceRoller::new(seed),
first_move: None, first_move: None,
player_id, player_id,
bot, bots,
}; };
game.handle_event(&GameEvent::BeginGame { game.handle_event(&GameEvent::BeginGame {
goes_first: player_id.unwrap(), goes_first: player_id.unwrap(),
@ -44,15 +55,14 @@ impl Game {
// println!("consuming {:?}", event); // println!("consuming {:?}", event);
self.state.consume(event); self.state.consume(event);
// chain all successive bot actions // chain all successive bot actions
let bot_event = self let bot_event = self.bots[0]
.bot
.handle_event(event) .handle_event(event)
.and_then(|evt| self.handle_event(&evt)); .and_then(|evt| self.handle_event(&evt));
// roll dice for bot if needed // roll dice for bot if needed
if self.bot_needs_dice_roll() { if self.bot_needs_dice_roll() {
let dice = self.dice_roller.roll(); let dice = self.dice_roller.roll();
self.handle_event(&GameEvent::RollResult { self.handle_event(&GameEvent::RollResult {
player_id: self.bot.player_id, player_id: self.bots[0].player_id,
dice, dice,
}) })
} else { } else {
@ -61,7 +71,7 @@ impl Game {
} }
fn bot_needs_dice_roll(&self) -> bool { 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 && self.state.turn_stage == TurnStage::RollWaiting
} }
} }