]> git.huck.website - cellseq.git/commitdiff
added: board and neighbor counting function
authorHuck Boles <huck@huck.website>
Thu, 25 May 2023 18:50:42 +0000 (13:50 -0500)
committerHuck Boles <huck@huck.website>
Thu, 25 May 2023 18:50:42 +0000 (13:50 -0500)
Cargo.lock [new file with mode: 0644]
src/cells.rs [new file with mode: 0644]
src/lib.rs

diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644 (file)
index 0000000..aedfcb8
--- /dev/null
@@ -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 (file)
index 0000000..01692a0
--- /dev/null
@@ -0,0 +1,60 @@
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub enum Cell {
+    Dead,
+    Alive,
+}
+
+#[derive(Clone, Debug)]
+pub struct Board {
+    pub board: Vec<Vec<Cell>>,
+    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
+    }
+}
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c404f2b44fa459eb52de8ac751df49b8426251ce 100644 (file)
@@ -0,0 +1,3 @@
+mod cells;
+
+pub use cells::*;