# 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"
+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}")
}
}