From ae5a15a5e2efda7319ca54416738effe65c090aa Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 7 May 2024 13:30:52 +0200 Subject: [PATCH] updating to next generation --- src/cell.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/cell.rs b/src/cell.rs index 3276063..7bd8918 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -1,8 +1,12 @@ use crate::BRIGHTNESS; +use crate::G; use crate::HEIGHT; +use crate::K1; +use crate::K2; use crate::Q; use crate::WIDTH; +#[derive(PartialEq)] pub struct Cell { pub x: u32, pub y: u32, @@ -93,3 +97,31 @@ pub fn display_grid(grid: &Vec>) { println!(); } } + +pub fn update_grid(grid: &mut Vec>) { + let mut new_gen = init_grid(Some(grid)); + for j in 0..HEIGHT { + for i in 0..WIDTH { + let x = i as usize; + let y = j as usize; + + let current = &grid[y][x]; + let next = &mut new_gen[y][x]; + + if current.state == Q - 1 { + next.state = 0; + } else if current.state == 0 { + let (inf, ill) = current.neighbor_count(&grid, StateOp::Types); + next.state = inf / K1 + ill / K2; + } else { + let (sum, states) = current.neighbor_count(&grid, StateOp::States); + next.state = sum / (9 - states) + G; + } + + if next.state >= Q { + next.state = Q - 1; + } + } + } + *grid = new_gen; +}