use crate::bot::Bot;
use crate::bot::Difficulty;
use crate::bot::RandomBot;
use crate::bot::SearchBot;
use crate::bot::TacticalBot;
use crate::game::GameState;
#[derive(Debug)]
pub struct Opponent {
difficulty: Difficulty,
bot: Box<dyn Bot>,
}
impl Opponent {
pub fn new(difficulty: Difficulty) -> Self {
Self::from_bot(difficulty, bot_for(difficulty, None))
}
pub fn seeded(difficulty: Difficulty, seed: u64) -> Self {
Self::from_bot(difficulty, bot_for(difficulty, Some(seed)))
}
pub fn from_bot(difficulty: Difficulty, bot: Box<dyn Bot>) -> Self {
Self { difficulty, bot }
}
pub const fn difficulty(&self) -> Difficulty {
self.difficulty
}
}
fn bot_for(difficulty: Difficulty, seed: Option<u64>) -> Box<dyn Bot> {
match difficulty {
Difficulty::Easy => Box::new(RandomBot::with_seed(seed)),
Difficulty::Medium => Box::new(TacticalBot::with_seed(seed)),
Difficulty::Hard => Box::new(SearchBot::with_seed(seed)),
}
}
impl Default for Opponent {
fn default() -> Self {
Self::new(Difficulty::default())
}
}
impl Bot for Opponent {
fn choose_move(&mut self, state: &GameState) -> Option<usize> {
self.bot.choose_move(state)
}
fn name(&self) -> &'static str {
self.bot.name()
}
}