fours 0.1.0

Four in a row in your terminal, with three levels of opponent.
Documentation
use crate::game::BoardDimensions;
use crate::game::GameError;
use crate::game::GameState;
use crate::game::Placement;

/// A game of four in a row: a [`GameState`] together with the moves that produced it.
///
/// # Examples
///
/// ```
/// use fours::game::Game;
///
/// let mut game = Game::new();
/// game.play(3).unwrap();
/// game.play(3).unwrap();
///
/// assert_eq!(game.move_history(), [3, 3]);
/// assert_eq!(game.state().disc_count(), 2);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Game {
    state: GameState,
    move_history: Vec<usize>,
}

impl Game {
    /// A game on an empty board of [`BoardDimensions::STANDARD`] size.
    pub fn new() -> Self {
        Self::with_dimensions(BoardDimensions::STANDARD)
    }

    /// A game on an empty board of the given size.
    pub fn with_dimensions(dimensions: BoardDimensions) -> Self {
        Self {
            state: GameState::with_dimensions(dimensions),
            move_history: Vec::new(),
        }
    }

    /// A game that has had the given moves played on it.
    ///
    /// # Errors
    ///
    /// Returns the error of the first move that could not be played. The returned game is the one
    /// that existed before that move.
    ///
    /// # Examples
    ///
    /// ```
    /// use fours::game::Game;
    /// use fours::game::Player;
    ///
    /// let game = Game::from_moves([0, 1, 0, 1, 0, 1, 0]).unwrap();
    /// assert_eq!(game.state().winner(), Some(Player::Red));
    /// ```
    pub fn from_moves<I>(moves: I) -> Result<Self, GameError>
    where
        I: IntoIterator<Item = usize>,
    {
        let mut game = Self::new();
        game.play_all(moves)?;
        Ok(game)
    }

    /// The current position.
    pub const fn state(&self) -> &GameState {
        &self.state
    }

    /// The columns that have been played, in the order they were played.
    pub fn move_history(&self) -> &[usize] {
        &self.move_history
    }

    /// Place a disc of the current player in a column.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`GameState::apply_move`]. A move that fails leaves the game
    /// unchanged.
    pub fn play(&mut self, column: usize) -> Result<Placement, GameError> {
        let placement = self.state.apply_move(column)?;
        self.move_history.push(column);
        Ok(placement)
    }

    /// Play a sequence of moves.
    ///
    /// # Errors
    ///
    /// Returns the error of the first move that could not be played. Moves before that one have
    /// been played.
    pub fn play_all<I>(&mut self, moves: I) -> Result<(), GameError>
    where
        I: IntoIterator<Item = usize>,
    {
        for column in moves {
            self.play(column)?;
        }

        Ok(())
    }

    /// Start over on an empty board of the same size.
    pub fn restart(&mut self) {
        *self = Self::with_dimensions(self.state.dimensions());
    }
}

impl Default for Game {
    fn default() -> Self {
        Self::new()
    }
}