fours 0.1.0

Four in a row in your terminal, with three levels of opponent.
Documentation
use crate::bot::Bot;
use crate::bot::Difficulty;
use crate::bot::RandomBot;
use crate::bot::SearchBot;
use crate::bot::TacticalBot;
use crate::game::GameState;

/// The opponent of a game, together with the [`Difficulty`] it was picked for.
///
/// This is the only place that decides which bot plays at which level, so a new opponent reaches
/// the front end by adding a level here rather than by changing the front end.
///
/// # Examples
///
/// ```
/// use fours::bot::Bot;
/// use fours::bot::Difficulty;
/// use fours::bot::Opponent;
/// use fours::game::GameState;
///
/// let mut opponent = Opponent::seeded(Difficulty::Hard, 1);
/// let state = GameState::new();
///
/// assert!(state.is_playable(opponent.choose_move(&state).unwrap()));
/// ```
#[derive(Debug)]
pub struct Opponent {
    difficulty: Difficulty,
    bot: Box<dyn Bot>,
}

impl Opponent {
    /// The opponent of a level, playing differently every run.
    pub fn new(difficulty: Difficulty) -> Self {
        Self::from_bot(difficulty, bot_for(difficulty, None))
    }

    /// The opponent of a level, playing the same way every run.
    pub fn seeded(difficulty: Difficulty, seed: u64) -> Self {
        Self::from_bot(difficulty, bot_for(difficulty, Some(seed)))
    }

    /// An opponent of your own, playing at the level you file it under.
    pub fn from_bot(difficulty: Difficulty, bot: Box<dyn Bot>) -> Self {
        Self { difficulty, bot }
    }

    /// The level this opponent was picked for.
    pub const fn difficulty(&self) -> Difficulty {
        self.difficulty
    }
}

/// The bot that plays at a level.
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()
    }
}