diff --git a/src/main.rs b/src/main.rs index d559a6a..c215a27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,8 @@ fn display(grid: &[[bool; WIDTH]; HEIGHT], buffer: &mut String) { } fn count(grid: &[[bool; WIDTH]; HEIGHT], x: usize, y: usize) -> u8 { + + // (i, j) coordinate let near:[(isize, isize); 8] = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), @@ -25,13 +27,13 @@ fn count(grid: &[[bool; WIDTH]; HEIGHT], x: usize, y: usize) -> u8 { let mut count = 0; for (i, j) in near { - let nx: isize = x as isize + i; - let ny: isize = y as isize + j; + let nx: isize = x as isize + j; + let ny: isize = y as isize + i; - if (nx > 0 && (WIDTH as isize) > nx && - ny > 0 && (HEIGHT as isize) > ny) { + if nx >= 0 && (WIDTH as isize) > nx && + ny >= 0 && (HEIGHT as isize) > ny { - if (grid[nx as usize][nx as usize]) { + if grid[ny as usize][nx as usize] { count += 1; } } @@ -43,10 +45,10 @@ fn count(grid: &[[bool; WIDTH]; HEIGHT], x: usize, y: usize) -> u8 { 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 { + for y in 0..HEIGHT { + for x in 0..WIDTH { let count = count(grid, x, y); - new_grid[x][y] = match (grid[x][y], count) { + new_grid[y][x] = match (grid[y][x], count) { (true, 2) | (true, 3) => true, (false, 3) => true, _ => false, @@ -61,8 +63,15 @@ fn main() { let mut grid: [[bool; WIDTH]; HEIGHT] = [[false; WIDTH]; HEIGHT]; let mut buffer: String = String::new(); + // grid[14][13] = true; + // grid[15][14] = true; + // grid[13][15] = true; + // grid[14][15] = true; + // grid[15][15] = true; + loop { display(&grid, &mut buffer); grid = generation(&grid); + std::thread::sleep(std::time::Duration::from_millis(500)); } }