Add play/pause button and toggle cell
This commit is contained in:
16
src/lib.rs
16
src/lib.rs
@@ -28,6 +28,15 @@ pub enum Cell {
|
||||
Alive = 1,
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
fn toggle(&mut self) {
|
||||
*self = match *self {
|
||||
Cell::Dead => Cell::Alive,
|
||||
Cell::Alive => Cell::Dead,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Universe {
|
||||
width: u32,
|
||||
@@ -151,13 +160,20 @@ impl Universe {
|
||||
self.height = height;
|
||||
self.cells = (0..height * self.height).map(|_i| Cell::Dead).collect();
|
||||
}
|
||||
|
||||
pub fn toggle_cell(&mut self, row: u32, col: u32) {
|
||||
let idx = self.get_index(row, col);
|
||||
self.cells[idx].toggle();
|
||||
}
|
||||
}
|
||||
|
||||
impl Universe {
|
||||
// for test
|
||||
pub fn get_cells(&self) -> &[Cell] {
|
||||
&self.cells
|
||||
}
|
||||
|
||||
// for test
|
||||
pub fn set_cells(&mut self, cells: &[(u32, u32)]) {
|
||||
for (row, col) in cells.iter().cloned() {
|
||||
let idx = self.get_index(row, col);
|
||||
|
||||
Reference in New Issue
Block a user