updating to next generation

This commit is contained in:
Baipyrus 2024-05-07 13:30:52 +02:00
parent ca4b04723f
commit ae5a15a5e2

View File

@ -1,8 +1,12 @@
use crate::BRIGHTNESS; use crate::BRIGHTNESS;
use crate::G;
use crate::HEIGHT; use crate::HEIGHT;
use crate::K1;
use crate::K2;
use crate::Q; use crate::Q;
use crate::WIDTH; use crate::WIDTH;
#[derive(PartialEq)]
pub struct Cell { pub struct Cell {
pub x: u32, pub x: u32,
pub y: u32, pub y: u32,
@ -93,3 +97,31 @@ pub fn display_grid(grid: &Vec<Vec<Cell>>) {
println!(); println!();
} }
} }
pub fn update_grid(grid: &mut Vec<Vec<Cell>>) {
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;
}