rustymines/game/board/field/
view.rs

1use std::fmt::{Debug, Display, Formatter};
2
3/// View state of a field.
4#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
5pub enum View {
6    /// The field has not been visited yet and is covered.
7    #[default]
8    Covered,
9    /// The field is flagged.
10    Flag,
11    /// The player stepped onto a dud.
12    SteppedOnDud,
13    /// The player stepped onto a live mine.
14    SteppedOnMine,
15    /// The field is clear.
16    Clear {
17        /// The amount of mines adjacent to the field.
18        adjacent_mines: u8,
19    },
20    /// The field contains a mine.
21    Mine,
22}
23
24impl View {
25    /// Returns a char representation of the field's view.
26    #[allow(clippy::missing_panics_doc)]
27    #[must_use]
28    pub const fn as_char(self) -> char {
29        match self {
30            Self::Covered => '■',
31            Self::Flag => '⚐',
32            Self::SteppedOnDud => '~',
33            Self::SteppedOnMine => '☠',
34            Self::Clear { adjacent_mines } => match adjacent_mines {
35                0 => ' ',
36                mines => char::from_digit(mines as u32, 10)
37                    .expect("Amount of adjacent mines should be a single decimal digit."),
38            },
39            Self::Mine => '*',
40        }
41    }
42}
43
44impl Display for View {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        Display::fmt(&self.as_char(), f)
47    }
48}