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 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<Box<dyn BotStrategy>>,
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");

View file

@ -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<CheckerMove>,
pub player_id: Option<PlayerId>,
bot: Bot,
bots: Vec<Bot>,
}
impl Game {
impl GameRunner {
// 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);
// local : player
let player_id: Option<PlayerId> = 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
}
}