rustymines/game/board/field/
view.rs1use std::fmt::{Debug, Display, Formatter};
2
3#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
5pub enum View {
6 #[default]
8 Covered,
9 Flag,
11 SteppedOnDud,
13 SteppedOnMine,
15 Clear {
17 adjacent_mines: u8,
19 },
20 Mine,
22}
23
24impl View {
25 #[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}