]> git.huck.website - cellseq.git/commitdiff
added: 2d array for map
authorHuck Boles <huck@huck.website>
Fri, 26 May 2023 22:11:33 +0000 (17:11 -0500)
committerHuck Boles <huck@huck.website>
Fri, 26 May 2023 22:11:33 +0000 (17:11 -0500)
Cargo.lock
Cargo.toml
src/cells.rs
src/cli.rs [new file with mode: 0644]
src/lib.rs
src/main.rs

index aedfcb8fd2a28fe15d13b2185ea32654a51d8305..19218cd95533865690045829922127d8e2ee7897 100644 (file)
@@ -2,6 +2,138 @@
 # It is not intended for manual editing.
 version = 3
 
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
 [[package]]
 name = "cellseq"
 version = "0.1.0"
+dependencies = [
+ "ndarray",
+ "rand",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "getrandom"
+version = "0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.144"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
+
+[[package]]
+name = "matrixmultiply"
+version = "0.3.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "090126dc04f95dc0d1c1c91f61bdd474b3930ca064c1edc8a849da2c6cbe1e77"
+dependencies = [
+ "autocfg",
+ "rawpointer",
+]
+
+[[package]]
+name = "ndarray"
+version = "0.15.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32"
+dependencies = [
+ "matrixmultiply",
+ "num-complex",
+ "num-integer",
+ "num-traits",
+ "rawpointer",
+]
+
+[[package]]
+name = "num-complex"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rawpointer"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
index ab30ce4b0611ea3e4083fd53f468e022016291cc..9b02f26820d2ade49264f8b6990b2a1d1555a785 100644 (file)
@@ -6,3 +6,5 @@ edition = "2021"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+ndarray = "0.15"
+rand = "0.8"
index 01692a01d561b90aa436b60967aa708344479afd..d7feed9b503e0975e425502b7f2c94766684ed16 100644 (file)
+use ndarray::Array2;
+use rand::{thread_rng, Rng};
+use std::{cell::Cell, fmt::Display, ops::Deref};
+
 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
-pub enum Cell {
+pub enum State {
     Dead,
     Alive,
 }
 
-#[derive(Clone, Debug)]
-pub struct Board {
-    pub board: Vec<Vec<Cell>>,
-    pub width: usize,
-    pub height: usize,
+#[derive(Clone, Debug, Eq, PartialEq)]
+pub struct Map {
+    pub map: Array2<Cell<State>>,
+    pub rows: usize,
+    pub cols: 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);
+impl Map {
+    pub fn new(rows: usize, cols: usize) -> Self {
+        let map = Array2::from_elem((rows, cols), Cell::new(State::Dead));
+        Self { map, rows, cols }
+    }
+
+    pub fn update(&mut self) {
+        let previous = self.clone();
+        for (next, prev) in self
+            .windows((3, 3))
+            .into_iter()
+            .zip(previous.windows((3, 3)))
+        {
+            let count = prev.iter().filter(|x| x.get() == State::Alive).count();
 
-        col.fill(Cell::Dead);
-        board.fill(col);
+            let cell = next.get((1, 1)).unwrap();
 
-        Self {
-            board,
-            width,
-            height,
+            match cell.get() {
+                State::Dead => {
+                    if count == 3 {
+                        cell.set(State::Alive);
+                    }
+                }
+                State::Alive => {
+                    if count < 2 || count > 3 {
+                        cell.set(State::Dead);
+                    }
+                }
+            }
         }
     }
 
-    pub fn update_board() {
-        todo!()
-    }
+    pub fn randomize(&mut self, val: f64) {
+        let mut rng = thread_rng();
+        for cell in self.map.iter() {
+            if rng.gen::<f64>() < val {
+                cell.set(State::Alive)
+            } else {
+                cell.set(State::Dead)
+            }
+        }
 
-    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;
+        let walls = vec![
+            self.row(0),
+            self.row(self.rows - 1),
+            self.column(0),
+            self.column(self.cols - 1),
+        ];
+
+        for wall in walls {
+            for cell in wall.iter() {
+                cell.set(State::Dead)
             }
         }
+    }
+}
+
+impl Deref for Map {
+    type Target = Array2<Cell<State>>;
+    fn deref(&self) -> &Self::Target {
+        &self.map
+    }
+}
+
+impl Display for Map {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let map: String = self
+            .outer_iter()
+            .into_iter()
+            .map(|row| {
+                row.iter()
+                    .map(|x| match x.get() {
+                        State::Dead => ' ',
+                        State::Alive => 'o',
+                    })
+                    .collect::<String>()
+            })
+            .collect::<Vec<String>>()
+            .join("\n");
 
-        count
+        write!(f, "{map}")
     }
 }
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644 (file)
index 0000000..e69de29
index c404f2b44fa459eb52de8ac751df49b8426251ce..52236d94b75e07fe960fbaa386f400f28e843a65 100644 (file)
@@ -1,3 +1,5 @@
 mod cells;
+mod cli;
 
 pub use cells::*;
+pub use cli::*;
index e7a11a969c037e00a796aafeff6258501ec15e9a..283257f60b05fc61efcabb96e6f8701fa1929891 100644 (file)
@@ -1,3 +1,16 @@
+use cellseq::*;
+
 fn main() {
-    println!("Hello, world!");
+    let mut map = Map::new(32, 64);
+    map.randomize(0.5);
+
+    loop {
+        map.update();
+
+        std::process::Command::new("clear").status().unwrap();
+
+        println!("{map}");
+
+        std::thread::sleep(std::time::Duration::from_millis(1000));
+    }
 }