mineswipe 0.1.0

Library for playing Minesweeper!💣
Documentation
/// A cell inside the grid.
#[derive(Copy, Clone)]
pub struct Cell {
  /// Is the cell a mine?
  pub mine:       bool,
  /// Has the cell been excavated?
  pub excavated:  bool,
  /// Has the cell been flagged?
  pub flagged:    bool,
  /// How many mines the cell is neighbour to.
  pub mines:      u8,
  /// The cell's index in the grid.
  pub index:      usize
}

impl Cell {
  /// Create a new cell.
  pub fn new(mine: bool, index: usize) -> Self {
    Self { mine, excavated: false, flagged: false, mines: 0, index}
  }

  /// Get the indices of the cell's neighbours
  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;

    // Längst åt vänster
    if self.index % columns == 0 {
      // Längst upp
      if self.index == 0 {
        return vec![r, bot, rbot];
      // Längst ner
      } else if self.index == columns*(rows-1) {
        return vec![r, top, rtop];
      } else {
        return vec![r, top, rtop, bot, rbot];
      }
    }
    // Längst åt höger
    else if (self.index+1) % columns == 0 {
      // Längst upp
      if self.index == columns-1 {
        return vec![l, bot, lbot];
      // Längst ner
      } else if self.index == (rows*columns)-1 {
        return vec![l, top, ltop];
      } else {
        return vec![l, top, bot, ltop, lbot];
      }
    }
    // Längst upp
    else if self.index < columns {
      return vec![l, r, bot, lbot, rbot];
    }
    // Längst ner
    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];
    }
  }
}