fours 0.1.0

Four in a row in your terminal, with three levels of opponent.
Documentation
use crate::game::Cell;
use crate::game::Player;
use std::fmt;

/// How a game ended, or that it has not ended yet.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Status {
    /// The game is still being played.
    InProgress,
    /// A player lined up enough discs to win.
    Won(Victory),
    /// The board filled up without a winner.
    Draw,
}

impl Status {
    /// Whether the game ended, either in a win or in a draw.
    pub const fn is_over(&self) -> bool {
        !matches!(self, Self::InProgress)
    }

    /// The winner, if the game was won.
    pub const fn winner(&self) -> Option<Player> {
        match self {
            Self::Won(victory) => Some(victory.winner()),
            Self::InProgress | Self::Draw => None,
        }
    }
}

impl fmt::Display for Status {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InProgress => formatter.write_str("in progress"),
            Self::Won(victory) => write!(formatter, "{} wins", victory.winner()),
            Self::Draw => formatter.write_str("draw"),
        }
    }
}

/// A won game: the winner and the discs that won it.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Victory {
    winner: Player,
    line: Vec<Cell>,
}

impl Victory {
    pub(crate) fn new(winner: Player, line: Vec<Cell>) -> Self {
        Self { winner, line }
    }

    /// The player who won.
    pub const fn winner(&self) -> Player {
        self.winner
    }

    /// The connected discs that ended the game, ordered from one end of the line to the other.
    ///
    /// The line contains at least [`LINE_LENGTH`](crate::game::LINE_LENGTH) cells and may
    /// contain more when the winning move extended a longer run.
    pub fn line(&self) -> &[Cell] {
        &self.line
    }
}