use crate::game::Cell;
use crate::game::Player;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Status {
InProgress,
Won(Victory),
Draw,
}
impl Status {
pub const fn is_over(&self) -> bool {
!matches!(self, Self::InProgress)
}
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"),
}
}
}
#[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 }
}
pub const fn winner(&self) -> Player {
self.winner
}
pub fn line(&self) -> &[Cell] {
&self.line
}
}