#[derive(Copy, Clone)]
pub struct Cell {
pub mine: bool,
pub excavated: bool,
pub flagged: bool,
pub mines: u8,
pub index: usize
}
impl Cell {
pub fn new(mine: bool, index: usize) -> Self {
Self { mine, excavated: false, flagged: false, mines: 0, index}
}
pub fn neighbours(&self, rows: usize, columns: usize) -> Vec<usize> {
let ltop = self.index.saturating_sub(columns+1);
let top = self.index.saturating_sub(columns);
let rtop = self.index.saturating_sub(columns-1);
let l = self.index.saturating_sub(1);
let r = self.index+1;
let lbot = self.index+columns-1;
let bot = self.index+columns;
let rbot = self.index+columns+1;
if self.index % columns == 0 {
if self.index == 0 {
return vec![r, bot, rbot];
} else if self.index == columns*(rows-1) {
return vec![r, top, rtop];
} else {
return vec![r, top, rtop, bot, rbot];
}
}
else if (self.index+1) % columns == 0 {
if self.index == columns-1 {
return vec![l, bot, lbot];
} else if self.index == (rows*columns)-1 {
return vec![l, top, ltop];
} else {
return vec![l, top, bot, ltop, lbot];
}
}
else if self.index < columns {
return vec![l, r, bot, lbot, rbot];
}
else if self.index >= columns*(rows-1) {
return vec![l, r, top, ltop, rtop];
}
else {
return vec![l, r, top, bot, ltop, rtop, lbot, rbot];
}
}
}