Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn generate(&mut self) -> &mut Self {
- let compare: Vec<Vec<Cell>> = self.grid.clone();
- for y in 0..self.size.1 -1 {
- for x in 0..self.size.0 -1 {
- let mut neighbors = 0;
- let mut current = self.get_mut((x, y)).unwrap();
- for (dx, dy) in DELTAS.iter() {
- let cx = x + dx;
- let cy = y + dy;
- if cx >= 0 && cy >= 0 {
- match compare[cx as usize][cy as usize].current_state {
- CellState::Alive => neighbors += 1,
- CellState::Dead => neighbors += 0,
- }
- }
- }
- if current.current_state == CellState::Alive {
- current.next_state = match neighbors {
- n if n < 2 => Some(CellState::Dead), // Underpopulation
- n if n >= 2 && n <= 3 => Some(CellState::Alive), // Healthy
- n if n > 3 => Some(CellState::Dead), // Overpopulation
- _ => { println!("Bwah! {}", neighbors); Some(CellState::Dead) }, // Wut?
- }
- } else if current.current_state == CellState::Dead && neighbors == 3 {
- current.next_state = Some(CellState::Alive); // Reproduction
- }
- }
- }
- self.generation += 1;
- self
- }
Add Comment
Please, Sign In to add comment