fours 0.1.0

Four in a row in your terminal, with three levels of opponent.
Documentation
use crate::bot::Bot;
use crate::bot::random_numbers;
use crate::game::GameState;
use rand::rngs::StdRng;
use rand::seq::IndexedRandom;

/// A bot that plays a random legal move.
///
/// The bot can be seeded, which makes a game against it reproducible.
///
/// # Examples
///
/// ```
/// use fours::bot::Bot;
/// use fours::bot::RandomBot;
/// use fours::game::GameState;
///
/// let state = GameState::new();
/// let mut bot = RandomBot::seeded(42);
/// let column = bot.choose_move(&state).unwrap();
///
/// assert!(state.is_playable(column));
/// ```
#[derive(Debug, Clone)]
pub struct RandomBot {
    random_numbers: StdRng,
}

impl RandomBot {
    /// A bot seeded from the operating system, which plays differently every run.
    pub fn from_entropy() -> Self {
        Self::with_seed(None)
    }

    /// A bot seeded with a fixed value, which plays the same way every run.
    pub fn seeded(seed: u64) -> Self {
        Self::with_seed(Some(seed))
    }

    pub(crate) fn with_seed(seed: Option<u64>) -> Self {
        Self {
            random_numbers: random_numbers(seed),
        }
    }
}

impl Default for RandomBot {
    fn default() -> Self {
        Self::from_entropy()
    }
}

impl Bot for RandomBot {
    fn choose_move(&mut self, state: &GameState) -> Option<usize> {
        state
            .legal_moves()
            .choose(&mut self.random_numbers)
            .copied()
    }

    fn name(&self) -> &'static str {
        "Random bot"
    }
}