fours 0.1.0

Four in a row in your terminal, with three levels of opponent.
Documentation
//! Opponents that pick a move for a position.
//!
//! A bot only ever looks at a [`GameState`] and answers with a column, so bots can be swapped
//! without the game or the front end noticing. [`Opponent`] pairs a bot with the [`Difficulty`]
//! it plays at, and is the only thing the front end holds on to.
//!
//! # Adding an opponent
//!
//! Write a type that implements [`Bot`], add a level to [`Difficulty`] and work it into that
//! type's `ALL` and `next`, then name your type in the one `match` inside [`Opponent`]. Everything
//! else reads the list of levels rather than repeating it.

mod difficulty;
mod opponent;
mod random_bot;
mod search_bot;
mod tactical_bot;

pub use difficulty::Difficulty;
pub use difficulty::UnknownDifficulty;
pub use opponent::Opponent;
pub use random_bot::RandomBot;
pub use search_bot::SearchBot;
pub use tactical_bot::TacticalBot;

use crate::game::GameState;
use rand::SeedableRng;
use rand::rngs::StdRng;
use std::fmt;

/// A player that picks its own moves.
///
/// A bot takes itself by mutable reference so that it can carry state between moves, such as a
/// random number generator or a cache.
pub trait Bot: fmt::Debug {
    /// The column to place the next disc in.
    ///
    /// Returns [`None`] when the bot has no move to make, which happens when the game is over or
    /// the board is full. The returned column is always legal in the given state.
    fn choose_move(&mut self, state: &GameState) -> Option<usize>;

    /// The name a front end shows for this opponent.
    fn name(&self) -> &'static str {
        "Bot"
    }
}

impl Bot for Box<dyn Bot> {
    fn choose_move(&mut self, state: &GameState) -> Option<usize> {
        (**self).choose_move(state)
    }

    fn name(&self) -> &'static str {
        (**self).name()
    }
}

/// Random numbers that either follow a seed or come from the operating system.
pub(crate) fn random_numbers(seed: Option<u64>) -> StdRng {
    seed.map_or_else(StdRng::from_os_rng, StdRng::seed_from_u64)
}