random init state
This commit is contained in:
parent
d0bed390a5
commit
4a096adfe2
5 changed files with 112 additions and 35 deletions
68
Cargo.lock
generated
68
Cargo.lock
generated
|
@ -2,6 +2,74 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[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.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gol-bitwise"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.147"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||
|
||||
[[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 = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
|
|
|
@ -6,7 +6,7 @@ fn main() {
|
|||
let mut region = Region::new();
|
||||
|
||||
loop {
|
||||
println!("---");
|
||||
println!("####################");
|
||||
region.print_all();
|
||||
region.step();
|
||||
{
|
||||
|
|
|
@ -3,26 +3,40 @@ use crate::tile::{Edges, Tile, WIDTH};
|
|||
pub struct Region {
|
||||
/// rows of tiles
|
||||
tiles: Vec<Vec<Tile>>,
|
||||
size: (usize, usize),
|
||||
offset: (isize, isize),
|
||||
auto_expand: bool,
|
||||
}
|
||||
|
||||
impl Region {
|
||||
pub fn new() -> Self {
|
||||
let tiles = vec![vec![Tile::glider(); 2]; 2];
|
||||
let width = 2;
|
||||
let height = 2;
|
||||
let mut tiles = Vec::new();
|
||||
for _ in 0..height {
|
||||
let mut row = Vec::new();
|
||||
for _ in 0..width {
|
||||
row.push(Tile::random());
|
||||
}
|
||||
tiles.push(row);
|
||||
}
|
||||
|
||||
Self {
|
||||
tiles,
|
||||
size: (2, 2),
|
||||
offset: (-1, -1),
|
||||
auto_expand: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn height(&self) -> usize {
|
||||
self.tiles.len()
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
self.tiles[0].len()
|
||||
}
|
||||
|
||||
pub fn print_all(&self) {
|
||||
for y in 0..self.size.1 {
|
||||
for y in 0..self.height() {
|
||||
for ch_row in 0..(WIDTH / 2) {
|
||||
for x in 0..self.size.1 {
|
||||
for x in 0..self.width() {
|
||||
self.tiles[y][x].print_row(ch_row);
|
||||
// print!("|");
|
||||
}
|
||||
|
@ -32,9 +46,9 @@ impl Region {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_cell(&mut self, x: isize, y: isize, state: bool) {
|
||||
//
|
||||
}
|
||||
// pub fn set_cell(&mut self, x: isize, y: isize, state: bool) {
|
||||
// //
|
||||
// }
|
||||
|
||||
fn get_tile_relative(&self, x: usize, y: usize, relx: isize, rely: isize) -> Option<&Tile> {
|
||||
self.tiles
|
||||
|
@ -44,10 +58,10 @@ impl Region {
|
|||
|
||||
pub fn step(&mut self) {
|
||||
// store edges
|
||||
let mut edges = Vec::with_capacity(self.size.1);
|
||||
for y in 0..self.size.1 {
|
||||
let mut row = Vec::with_capacity(self.size.0);
|
||||
for x in 0..self.size.0 {
|
||||
let mut edges = Vec::with_capacity(self.height());
|
||||
for y in 0..self.height() {
|
||||
let mut row = Vec::with_capacity(self.width());
|
||||
for x in 0..self.width() {
|
||||
let n = self.get_tile_relative(x, y, 0, -1).unwrap_or(&Tile::EMPTY);
|
||||
let s = self.get_tile_relative(x, y, 0, 1).unwrap_or(&Tile::EMPTY);
|
||||
let e = self.get_tile_relative(x, y, 1, 0).unwrap_or(&Tile::EMPTY);
|
||||
|
@ -57,16 +71,14 @@ impl Region {
|
|||
let se = self.get_tile_relative(x, y, 1, 1).unwrap_or(&Tile::EMPTY);
|
||||
let sw = self.get_tile_relative(x, y, -1, 1).unwrap_or(&Tile::EMPTY);
|
||||
|
||||
let edge = Edges::full(n, s, e, w, ne, nw, se, sw);
|
||||
// dbg!(&edge);
|
||||
let edge = Edges::new(n, s, e, w, ne, nw, se, sw);
|
||||
row.push(edge);
|
||||
}
|
||||
edges.push(row);
|
||||
}
|
||||
// dbg!(&edges);
|
||||
for y in 0..self.size.1 {
|
||||
for x in 0..self.size.0 {
|
||||
// dbg!(x, y, &edges[y][x]);
|
||||
|
||||
for y in 0..self.height() {
|
||||
for x in 0..self.width() {
|
||||
self.tiles[y][x].step(&edges[y][x]);
|
||||
}
|
||||
}
|
||||
|
|
24
src/tile.rs
24
src/tile.rs
|
@ -1,4 +1,4 @@
|
|||
pub type Row = u16;
|
||||
pub type Row = u64;
|
||||
|
||||
pub const WIDTH: usize = Row::BITS as usize;
|
||||
const LAST: usize = WIDTH - 1;
|
||||
|
@ -31,17 +31,12 @@ impl Tile {
|
|||
self.rows.iter().fold(0, |a, r| a | r) == 0
|
||||
}
|
||||
|
||||
pub fn glider() -> Self {
|
||||
let mut tile = Self::new();
|
||||
|
||||
tile.rows[WIDTH - 8] = 0b_00100000;
|
||||
tile.rows[WIDTH - 7] = 0b_00010000;
|
||||
tile.rows[WIDTH - 6] = 0b_01110000;
|
||||
|
||||
tile.rows[WIDTH - 3] = 0b_0010;
|
||||
tile.rows[WIDTH - 2] = 0b_0001;
|
||||
tile.rows[WIDTH - 1] = 0b_0111;
|
||||
tile
|
||||
pub fn random() -> Self {
|
||||
let mut t = Self::new();
|
||||
for r in 0..(WIDTH / 2) {
|
||||
t.rows[r] = rand::random();
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
pub fn print_row(&self, ch_row: usize) {
|
||||
|
@ -92,7 +87,7 @@ impl Tile {
|
|||
partial_sums_2[y] = (left & right) | ((left ^ right) & row);
|
||||
}
|
||||
|
||||
for y in 1..LAST {
|
||||
for y in 1..(WIDTH - 1) {
|
||||
step_row(
|
||||
&mut tile[y],
|
||||
partial_sums_1[y - 1],
|
||||
|
@ -139,7 +134,7 @@ impl Tile {
|
|||
}
|
||||
|
||||
impl Edges {
|
||||
pub fn full(
|
||||
pub fn new(
|
||||
n: &Tile,
|
||||
s: &Tile,
|
||||
e: &Tile,
|
||||
|
@ -194,6 +189,7 @@ impl Edges {
|
|||
fn ne_bit(&self) -> Row {
|
||||
self.ne as Row
|
||||
}
|
||||
|
||||
fn se_bit(&self) -> Row {
|
||||
self.se as Row
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue