use crate::colors::{self, BG_RESET, FG_RESET};
use termion::color;
#[derive(Copy, Clone, Debug)]
pub enum State {
Open,
Closed,
Flagged,
}
impl Default for State {
fn default() -> Self {
Self::Closed
}
}
#[derive(Copy, Clone, Debug)]
pub enum Content {
Mine,
Empty,
}
impl Default for Content {
fn default() -> Self {
Self::Empty
}
}
#[derive(Copy, Clone, Debug, Default)]
pub struct Cell {
pub state: State,
pub content: Content,
pub neighbouring_bomb_count: usize,
}
impl Cell {
pub fn set_state(&mut self, new_state: State) {
self.state = new_state;
}
pub fn to_string_with_palette(&self, palette: &colors::Palette) -> String {
match self.state {
State::Open => match self.content {
Content::Mine => {
format!("{}*{}", palette.mine_bg, BG_RESET)
}
Content::Empty => format!(
"{}{}{}{FG_RESET}{BG_RESET}",
palette.bg,
palette.neighbour_count_to_fg_color[self.neighbouring_bomb_count],
if self.neighbouring_bomb_count != 0 {
self.neighbouring_bomb_count.to_string()
} else {
" ".to_string()
},
),
},
State::Closed => ".".to_string(),
State::Flagged => {
format!("{}F{BG_RESET}", palette.flag_bg)
}
}
}
pub fn to_string_with_palette_lost(&self, palette: &colors::Palette) -> String {
match (self.state, self.content) {
(State::Flagged, Content::Mine) => format!("{}*{BG_RESET}", color::Bg(color::Green)),
(State::Flagged, Content::Empty) => {
format!(
"{bg}{count}{BG_RESET}",
bg=color::Bg(color::LightRed),
count=self.neighbouring_bomb_count,
)
}
(_, Content::Mine) => format!("{}*{BG_RESET}", palette.mine_bg),
(_, Content::Empty) => format!(
"{bg}{fg}{count}{FG_RESET}{BG_RESET}",
bg = palette.bg,
fg = palette.neighbour_count_to_fg_color[self.neighbouring_bomb_count],
count = if self.neighbouring_bomb_count != 0 {
self.neighbouring_bomb_count.to_string()
} else {
" ".to_string()
},
),
}
}
}