wip bot mirror
This commit is contained in:
parent
fa9c02084a
commit
12004ec4f3
11 changed files with 656 additions and 254 deletions
|
|
@ -22,3 +22,4 @@ rand = "0.8"
|
|||
env_logger = "0.10"
|
||||
burn = { version = "0.17", features = ["ndarray", "autodiff"] }
|
||||
burn-rl = { git = "https://github.com/yunjhongwu/burn-rl-examples.git", package = "burn-rl" }
|
||||
log = "0.4.20"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
pub mod dqn;
|
||||
pub mod strategy;
|
||||
|
||||
use log::{error, info};
|
||||
use store::{CheckerMove, Color, GameEvent, GameState, PlayerId, PointsRules, Stage, TurnStage};
|
||||
pub use strategy::default::DefaultStrategy;
|
||||
pub use strategy::dqn::DqnStrategy;
|
||||
|
|
@ -26,7 +27,7 @@ pub trait BotStrategy: std::fmt::Debug {
|
|||
pub struct Bot {
|
||||
pub player_id: PlayerId,
|
||||
strategy: Box<dyn BotStrategy>,
|
||||
// color: Color,
|
||||
color: Color,
|
||||
// schools_enabled: bool,
|
||||
}
|
||||
|
||||
|
|
@ -34,9 +35,9 @@ impl Default for Bot {
|
|||
fn default() -> Self {
|
||||
let strategy = DefaultStrategy::default();
|
||||
Self {
|
||||
player_id: 2,
|
||||
player_id: 1,
|
||||
strategy: Box::new(strategy),
|
||||
// color: Color::Black,
|
||||
color: Color::White,
|
||||
// schools_enabled: false,
|
||||
}
|
||||
}
|
||||
|
|
@ -52,57 +53,86 @@ impl Bot {
|
|||
Color::White => 1,
|
||||
Color::Black => 2,
|
||||
};
|
||||
strategy.set_player_id(player_id);
|
||||
strategy.set_color(color);
|
||||
// strategy.set_player_id(player_id);
|
||||
// strategy.set_color(color);
|
||||
Self {
|
||||
player_id,
|
||||
strategy,
|
||||
// color,
|
||||
color,
|
||||
// schools_enabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_event(&mut self, event: &GameEvent) -> Option<GameEvent> {
|
||||
info!(">>>> {:?} BOT handle", self.color);
|
||||
let game = self.strategy.get_mut_game();
|
||||
game.consume(event);
|
||||
let internal_event = if self.color == Color::Black {
|
||||
&event.get_mirror()
|
||||
} else {
|
||||
event
|
||||
};
|
||||
|
||||
let init_player_points = game.who_plays().map(|p| (p.points, p.holes));
|
||||
let turn_stage = game.turn_stage;
|
||||
game.consume(internal_event);
|
||||
if game.stage == Stage::Ended {
|
||||
info!("<<<< end {:?} BOT handle", self.color);
|
||||
return None;
|
||||
}
|
||||
if game.active_player_id == self.player_id {
|
||||
return match game.turn_stage {
|
||||
let active_player_id = if self.color == Color::Black {
|
||||
if game.active_player_id == 1 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
}
|
||||
} else {
|
||||
game.active_player_id
|
||||
};
|
||||
if active_player_id == self.player_id {
|
||||
let player_points = game.who_plays().map(|p| (p.points, p.holes));
|
||||
if self.color == Color::Black {
|
||||
info!( " input (internal) evt : {internal_event:?}, points : {init_player_points:?}, stage : {turn_stage:?}");
|
||||
}
|
||||
let internal_event = match game.turn_stage {
|
||||
TurnStage::MarkAdvPoints => Some(GameEvent::Mark {
|
||||
player_id: self.player_id,
|
||||
player_id: 1,
|
||||
points: self.strategy.calculate_adv_points(),
|
||||
}),
|
||||
TurnStage::RollDice => Some(GameEvent::Roll {
|
||||
player_id: self.player_id,
|
||||
}),
|
||||
TurnStage::RollDice => Some(GameEvent::Roll { player_id: 1 }),
|
||||
TurnStage::MarkPoints => Some(GameEvent::Mark {
|
||||
player_id: self.player_id,
|
||||
player_id: 1,
|
||||
points: self.strategy.calculate_points(),
|
||||
}),
|
||||
TurnStage::Move => Some(GameEvent::Move {
|
||||
player_id: self.player_id,
|
||||
player_id: 1,
|
||||
moves: self.strategy.choose_move(),
|
||||
}),
|
||||
TurnStage::HoldOrGoChoice => {
|
||||
if self.strategy.choose_go() {
|
||||
Some(GameEvent::Go {
|
||||
player_id: self.player_id,
|
||||
})
|
||||
Some(GameEvent::Go { player_id: 1 })
|
||||
} else {
|
||||
Some(GameEvent::Move {
|
||||
player_id: self.player_id,
|
||||
player_id: 1,
|
||||
moves: self.strategy.choose_move(),
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
return if self.color == Color::Black {
|
||||
info!(" bot (internal) evt : {internal_event:?} ; points : {player_points:?}");
|
||||
info!("<<<< end {:?} BOT handle", self.color);
|
||||
internal_event.map(|evt| evt.get_mirror())
|
||||
} else {
|
||||
info!("<<<< end {:?} BOT handle", self.color);
|
||||
internal_event
|
||||
};
|
||||
}
|
||||
info!("<<<< end {:?} BOT handle", self.color);
|
||||
None
|
||||
}
|
||||
|
||||
// Only used in tests below
|
||||
pub fn get_state(&self) -> &GameState {
|
||||
self.strategy.get_game()
|
||||
}
|
||||
|
|
@ -121,17 +151,31 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_consume() {
|
||||
fn test_handle_event() {
|
||||
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);
|
||||
assert_eq!(bot.get_state().active_player_id, 1); // bot internal active_player_id for black
|
||||
event = bot.handle_event(&GameEvent::RollResult {
|
||||
player_id: 2,
|
||||
dice: Dice { values: (2, 3) },
|
||||
});
|
||||
assert_eq!(
|
||||
event,
|
||||
Some(GameEvent::Move {
|
||||
player_id: 2,
|
||||
moves: (
|
||||
CheckerMove::new(24, 21).unwrap(),
|
||||
CheckerMove::new(24, 22).unwrap()
|
||||
)
|
||||
})
|
||||
);
|
||||
|
||||
event = bot.handle_event(&GameEvent::BeginGame { goes_first: 1 });
|
||||
assert_eq!(event, None);
|
||||
|
||||
assert_eq!(bot.get_state().active_player_id, 1);
|
||||
assert_eq!(bot.get_state().active_player_id, 2); //internal active_player_id
|
||||
bot.handle_event(&GameEvent::RollResult {
|
||||
player_id: 1,
|
||||
dice: Dice { values: (2, 3) },
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ impl Default for DefaultStrategy {
|
|||
let game = GameState::default();
|
||||
Self {
|
||||
game,
|
||||
player_id: 2,
|
||||
color: Color::Black,
|
||||
player_id: 1,
|
||||
color: Color::White,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue