From e624d3a5ff1bc01d4c9d03e187038e5aa68ced87 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Thu, 25 May 2023 13:50:42 -0500 Subject: [PATCH] added: board and neighbor counting function --- Cargo.lock | 7 ++++++ src/cells.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ 3 files changed, 70 insertions(+) create mode 100644 Cargo.lock create mode 100644 src/cells.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..aedfcb8 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cellseq" +version = "0.1.0" diff --git a/src/cells.rs b/src/cells.rs new file mode 100644 index 0000000..01692a0 --- /dev/null +++ b/src/cells.rs @@ -0,0 +1,60 @@ +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Cell { + Dead, + Alive, +} + +#[derive(Clone, Debug)] +pub struct Board { + pub board: Vec>, + pub width: usize, + pub height: usize, +} + +impl Board { + pub fn new(width: usize, height: usize) -> Self { + let mut col = Vec::with_capacity(height); + let mut board = Vec::with_capacity(width); + + col.fill(Cell::Dead); + board.fill(col); + + Self { + board, + width, + height, + } + } + + pub fn update_board() { + todo!() + } + + fn get_neighbors(&self, x: usize, y: usize) -> usize { + let mut count = 0; + + let x_set = if x == 0 { + [0, 1, self.width - 1] + } else if x == self.width - 1 { + [x - 1, x, 0] + } else { + [x - 1, x, x + 1] + }; + + let y_set = if y == 0 { + [0, 1, self.height - 1] + } else if y == self.height - 1 { + [y - 1, y, 0] + } else { + [y - 1, y, y + 1] + }; + + for (x, y) in x_set.iter().zip(y_set.iter()) { + if let Cell::Alive = self.board[*x][*y] { + count += 1; + } + } + + count + } +} diff --git a/src/lib.rs b/src/lib.rs index e69de29..c404f2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,3 @@ +mod cells; + +pub use cells::*; -- 2.45.2