From 34cb5fb51c71f2978c3a044b90be1a642da4ba35 Mon Sep 17 00:00:00 2001 From: Jean-Marie Mineau Date: Thu, 13 May 2021 00:41:36 +0200 Subject: [PATCH] random start --- Cargo.toml | 1 + src/lib.rs | 23 +++++++++++++++++++++++ www | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1303340..706d514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"] default = ["console_error_panic_hook"] [dependencies] +js-sys = "0.3" wasm-bindgen = "0.2.63" # The `console_error_panic_hook` crate provides better debugging of panics by diff --git a/src/lib.rs b/src/lib.rs index 5e3db3e..5216eab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ mod utils; use std::fmt; use wasm_bindgen::prelude::*; +extern crate js_sys; + // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. #[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 { self.to_string() } diff --git a/www b/www index 645cf7d..7ba57de 160000 --- a/www +++ b/www @@ -1 +1 @@ -Subproject commit 645cf7d32c691bf9c156d34c5b18c0ea5f85e767 +Subproject commit 7ba57de4672a6a84db7f155c162a267809bacd7e