From b053bbcbeb2c1cc89d7f2ecb02d91ca15ccf5e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Thu, 1 May 2025 22:05:06 +0200 Subject: [PATCH] feat: add generation function This commit adds the generation function, which: - Takes the current grid arena as input. - Creates a new temporary grid. - Applies Conway's Game of Life rules to compute the next state. - Replaces the original grid with the updated one. Conway's Game of Life rules: - A live cell dies if it has fewer than 2 or more than 3 live neighbours. - A live cell survives if it has 2 or 3 live neighbours. - A dead cell becomes alive if it has exactly 3 live neighbours. --- src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 61e2322..d559a6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,11 +40,29 @@ fn count(grid: &[[bool; WIDTH]; HEIGHT], x: usize, y: usize) -> u8 { return count; } +fn generation(grid: &[[bool; WIDTH]; HEIGHT]) -> [[bool; WIDTH]; HEIGHT] { + let mut new_grid = [[false; WIDTH]; HEIGHT]; + + for x in 0..HEIGHT { + for y in 0..WIDTH { + let count = count(grid, x, y); + new_grid[x][y] = match (grid[x][y], count) { + (true, 2) | (true, 3) => true, + (false, 3) => true, + _ => false, + }; + } + } + + return new_grid; +} + fn main() { - let grid: [[bool; WIDTH]; HEIGHT] = [[false; WIDTH]; HEIGHT]; + let mut grid: [[bool; WIDTH]; HEIGHT] = [[false; WIDTH]; HEIGHT]; let mut buffer: String = String::new(); loop { display(&grid, &mut buffer); + grid = generation(&grid); } }