From 2e4e27092aa70fa939dc8de84cc46691e11b0417 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Fri, 26 May 2023 17:11:33 -0500 Subject: [PATCH] added: 2d array for map --- Cargo.lock | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/cells.rs | 124 +++++++++++++++++++++++++++++++---------------- src/cli.rs | 0 src/lib.rs | 2 + src/main.rs | 15 +++++- 6 files changed, 232 insertions(+), 43 deletions(-) create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index aedfcb8..19218cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index ab30ce4..9b02f26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/cells.rs b/src/cells.rs index 01692a0..d7feed9 100644 --- a/src/cells.rs +++ b/src/cells.rs @@ -1,60 +1,100 @@ +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>, - pub width: usize, - pub height: usize, +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct Map { + pub map: Array2>, + 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::() < 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>; + 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::() + }) + .collect::>() + .join("\n"); - count + write!(f, "{map}") } } diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs index c404f2b..52236d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ mod cells; +mod cli; pub use cells::*; +pub use cli::*; diff --git a/src/main.rs b/src/main.rs index e7a11a9..283257f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)); + } } -- 2.44.2