refact: BotStrategy dyn trait
This commit is contained in:
parent
4ee2b02a41
commit
5762187b04
6 changed files with 43 additions and 69 deletions
|
|
@ -1,10 +1,8 @@
|
|||
mod bot;
|
||||
|
||||
use store::{
|
||||
CheckerMove, Color, Dice, GameEvent, GameState, Player, PlayerId, PointsRules, Stage, TurnStage,
|
||||
};
|
||||
use store::{CheckerMove, Color, GameEvent, GameState, PlayerId, PointsRules, TurnStage};
|
||||
|
||||
pub trait BotStrategy {
|
||||
pub trait BotStrategy: std::fmt::Debug {
|
||||
fn get_game(&self) -> &GameState;
|
||||
fn get_mut_game(&mut self) -> &mut GameState;
|
||||
fn calculate_points(&self) -> u8;
|
||||
|
|
@ -27,18 +25,11 @@ pub struct DefaultStrategy {
|
|||
impl Default for DefaultStrategy {
|
||||
fn default() -> Self {
|
||||
let game = GameState::default();
|
||||
let mut strategy = Self {
|
||||
Self {
|
||||
game,
|
||||
player_id: 2,
|
||||
color: Color::Black,
|
||||
};
|
||||
strategy
|
||||
}
|
||||
}
|
||||
|
||||
impl DefaultStrategy {
|
||||
fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,35 +86,34 @@ impl BotStrategy for DefaultStrategy {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Bot<BotStrategy> {
|
||||
pub struct Bot {
|
||||
pub player_id: PlayerId,
|
||||
strategy: BotStrategy,
|
||||
color: Color,
|
||||
schools_enabled: bool,
|
||||
strategy: Box<dyn BotStrategy>,
|
||||
// color: Color,
|
||||
// schools_enabled: bool,
|
||||
}
|
||||
|
||||
impl Default for Bot<DefaultStrategy> {
|
||||
impl Default for Bot {
|
||||
fn default() -> Self {
|
||||
let strategy = DefaultStrategy::default();
|
||||
Self {
|
||||
player_id: 2,
|
||||
strategy: DefaultStrategy::default(),
|
||||
color: Color::Black,
|
||||
schools_enabled: false,
|
||||
strategy: Box::new(strategy),
|
||||
// color: Color::Black,
|
||||
// schools_enabled: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<BS> Bot<BS>
|
||||
where
|
||||
BS: BotStrategy,
|
||||
{
|
||||
impl Bot {
|
||||
/// new initialize a bot
|
||||
/// # Examples
|
||||
/// ```let mut bot = Bot::new(Color::Black);
|
||||
/// assert_eq!(bot.game.stage, Stage::PreGame);
|
||||
/// ```
|
||||
pub fn new(mut strategy: BS, color: Color, schools_enabled: bool) -> Self {
|
||||
let game = strategy.get_mut_game();
|
||||
// pub fn new(mut strategy: Box<dyn BotStrategy>, color: Color, schools_enabled: bool) -> Self {
|
||||
pub fn new(mut strategy: Box<dyn BotStrategy>, color: Color) -> Self {
|
||||
// let game = strategy.get_mut_game();
|
||||
strategy.init_players();
|
||||
let player_id = match color {
|
||||
Color::White => 1,
|
||||
|
|
@ -133,8 +123,8 @@ where
|
|||
Self {
|
||||
player_id,
|
||||
strategy,
|
||||
color,
|
||||
schools_enabled: false,
|
||||
// color,
|
||||
// schools_enabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -174,16 +164,19 @@ where
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use store::{Dice, Stage};
|
||||
|
||||
#[test]
|
||||
fn test_new() {
|
||||
let bot = Bot::new(DefaultStrategy::new(), Color::Black, false);
|
||||
let bot = Bot::new(Box::new(DefaultStrategy::default()), Color::Black);
|
||||
// let bot = Bot::new(Box::new(DefaultStrategy::default()), Color::Black, false);
|
||||
assert_eq!(bot.get_state().stage, Stage::PreGame);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_consume() {
|
||||
let mut bot = Bot::new(DefaultStrategy::new(), Color::Black, false);
|
||||
let mut bot = Bot::new(Box::new(DefaultStrategy::default()), Color::Black);
|
||||
// let mut bot = Bot::new(Box::new(DefaultStrategy::default()), Color::Black, false);
|
||||
let mut event = bot.handle_event(&GameEvent::BeginGame { goes_first: 2 });
|
||||
assert_eq!(event, Some(GameEvent::Roll { player_id: 2 }));
|
||||
assert_eq!(bot.get_state().active_player_id, 2);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue