Guest User

Untitled

a guest
Dec 13th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. pub fn generate(&mut self) -> &mut Self {
  2. let compare: Vec<Vec<Cell>> = self.grid.clone();
  3. for y in 0..self.size.1 -1 {
  4. for x in 0..self.size.0 -1 {
  5. let mut neighbors = 0;
  6. let mut current = self.get_mut((x, y)).unwrap();
  7. for (dx, dy) in DELTAS.iter() {
  8. let cx = x + dx;
  9. let cy = y + dy;
  10. if cx >= 0 && cy >= 0 {
  11. match compare[cx as usize][cy as usize].current_state {
  12. CellState::Alive => neighbors += 1,
  13. CellState::Dead => neighbors += 0,
  14. }
  15. }
  16. }
  17. if current.current_state == CellState::Alive {
  18. current.next_state = match neighbors {
  19. n if n < 2 => Some(CellState::Dead), // Underpopulation
  20. n if n >= 2 && n <= 3 => Some(CellState::Alive), // Healthy
  21. n if n > 3 => Some(CellState::Dead), // Overpopulation
  22. _ => { println!("Bwah! {}", neighbors); Some(CellState::Dead) }, // Wut?
  23. }
  24. } else if current.current_state == CellState::Dead && neighbors == 3 {
  25. current.next_state = Some(CellState::Alive); // Reproduction
  26. }
  27. }
  28. }
  29. self.generation += 1;
  30. self
  31. }
Add Comment
Please, Sign In to add comment