|
|
@ -3,6 +3,8 @@ mod utils;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fmt;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern crate js_sys;
|
|
|
|
|
|
|
|
|
|
|
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
|
|
|
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
|
|
|
|
// allocator.
|
|
|
|
// allocator.
|
|
|
|
#[cfg(feature = "wee_alloc")]
|
|
|
|
#[cfg(feature = "wee_alloc")]
|
|
|
@ -101,6 +103,27 @@ impl Universe {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new_random() -> Universe {
|
|
|
|
|
|
|
|
let width = 64;
|
|
|
|
|
|
|
|
let height = 64;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cells = (0..width * height)
|
|
|
|
|
|
|
|
.map(|_i| {
|
|
|
|
|
|
|
|
if js_sys::Math::random() < 0.5 {
|
|
|
|
|
|
|
|
Cell::Alive
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Cell::Dead
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Universe {
|
|
|
|
|
|
|
|
width,
|
|
|
|
|
|
|
|
height,
|
|
|
|
|
|
|
|
cells,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn render(&self) -> String {
|
|
|
|
pub fn render(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|