Compare commits

...

3 Commits

@ -52,6 +52,25 @@ impl Universe{
}
count
}
fn regenerate_cells(&mut self) {
let size = (self.width * self.height) as usize;
self.cells = FixedBitSet::with_capacity(size);
for i in 0..size {
self.cells.set(i, false);
}
}
pub fn get_cells(&self) -> &FixedBitSet {
&self.cells
}
pub fn set_cells(&mut self, cells: &[(u32, u32)]) {
for (row, col) in cells.iter().cloned() {
let i = self.get_index(row, col);
self.cells.set(i, true);
}
}
}
#[wasm_bindgen]
@ -76,6 +95,16 @@ impl Universe {
self.cells = next;
}
pub fn set_width(&mut self, width: u32) {
self.width = width;
self.regenerate_cells();
}
pub fn set_height(&mut self, height: u32) {
self.height = height;
self.regenerate_cells();
}
pub fn new() -> Universe {
let width = 64;
let height = 64;
@ -84,7 +113,7 @@ impl Universe {
let mut cells = FixedBitSet::with_capacity(size);
for i in 0..size {
cells.set(i, i % 2 == 0 || i % 7 == 0);
cells.set(i, false);
}
Universe {
@ -94,7 +123,7 @@ impl Universe {
}
}
pub fn new_random() -> Universe {
pub fn new_modulo() -> Universe {
let width = 64;
let height = 64;
let size = (width * height) as usize;
@ -102,7 +131,7 @@ impl Universe {
let mut cells = FixedBitSet::with_capacity(size);
for i in 0..size {
cells.set(i, js_sys::Math::random() < 0.5);
cells.set(i, i % 2 == 0 || i % 7 == 0);
}
Universe {
@ -112,22 +141,15 @@ impl Universe {
}
}
pub fn new_space_ship() -> Universe {
let width : u32 = 64;
let height : u32 = 64;
pub fn new_random() -> Universe {
let width = 64;
let height = 64;
let size = (width * height) as usize;
let mut cells = FixedBitSet::with_capacity(size);
for i in 0..size {
cells.set(i, false);
}
let x0 = 32;
let y0 = 32;
for (x, y) in [(0, 2), (1, 0), (1, 2), (2, 1), (2, 2)].iter().cloned() {
let i = ((y0 + y) * width + x0 + x) as usize;
cells.set(i, true);
cells.set(i, js_sys::Math::random() < 0.5);
}
Universe {
@ -137,6 +159,20 @@ impl Universe {
}
}
pub fn new_space_ship() -> Universe {
let mut univese = Universe::new();
const X0: u32 = 32;
const Y0: u32 = 32;
let cells = &(
[(0, 2), (1, 0), (1, 2), (2, 1), (2, 2)]
.iter()
.map(|(x, y)| (X0+x, Y0+y))
.collect::<Vec<(u32, u32)>>()
);
univese.set_cells(cells);
univese
}
pub fn render(&self) -> String {
self.to_string()
}

@ -2,7 +2,9 @@
#![cfg(target_arch = "wasm32")]
extern crate wasm_game_of_life;
extern crate wasm_bindgen_test;
use wasm_game_of_life::Universe;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
@ -11,3 +13,29 @@ wasm_bindgen_test_configure!(run_in_browser);
fn pass() {
assert_eq!(1 + 1, 2);
}
#[cfg(test)]
pub fn input_spaceship() -> Universe {
let mut universe = Universe::new();
universe.set_width(6);
universe.set_height(6);
universe.set_cells(&[(1,2), (2,3), (3,1), (3,2), (3,3)]);
universe
}
#[cfg(test)]
pub fn expected_spaceship() -> Universe {
let mut universe = Universe::new();
universe.set_width(6);
universe.set_height(6);
universe.set_cells(&[(2,1), (2,3), (3,2), (3,3), (4,2)]);
universe
}
#[wasm_bindgen_test]
pub fn test_tick() {
let mut input_universe = input_spaceship();
let expected_universe = expected_spaceship();
input_universe.tick();
assert_eq!(&input_universe.get_cells(), &expected_universe.get_cells());
}

2
www

@ -1 +1 @@
Subproject commit f6fa2759a6dce5379554d7294f00487bb1565004
Subproject commit 38933c4a49773b2c7acd9bdd9d398da278186ac3
Loading…
Cancel
Save