@@ -3,14 +3,14 @@ mod utils;
3
3
use std:: fmt;
4
4
use wasm_bindgen:: prelude:: * ;
5
5
extern crate js_sys;
6
- extern crate web_sys;
6
+ // extern crate web_sys;
7
7
8
8
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
9
- macro_rules! log {
10
- ( $( $t: tt) * ) => {
11
- web_sys:: console:: log_1( & format!( $( $t ) * ) . into( ) ) ;
12
- } ;
13
- }
9
+ // macro_rules! log {
10
+ // ($( $t:tt)* ) => {
11
+ // web_sys::console::log_1(&format!( $( $t )*).into());
12
+ // };
13
+ // }
14
14
15
15
#[ wasm_bindgen]
16
16
#[ repr( u8 ) ]
@@ -20,6 +20,15 @@ pub enum Cell {
20
20
Alive = 1 ,
21
21
}
22
22
23
+ impl Cell {
24
+ fn toggle ( & mut self ) {
25
+ * self = match * self {
26
+ Cell :: Dead => Cell :: Alive ,
27
+ Cell :: Alive => Cell :: Dead ,
28
+ }
29
+ }
30
+ }
31
+
23
32
#[ wasm_bindgen]
24
33
pub struct Universe {
25
34
width : u32 ,
@@ -74,13 +83,13 @@ impl Universe {
74
83
let cell = self . cells [ idx] ;
75
84
let live_neighbors = self . live_neighbor_count ( row, col) ;
76
85
77
- log ! (
78
- "cell[{}, {}] is initially {:?} and has {} live neighbors" ,
79
- row,
80
- col,
81
- cell,
82
- live_neighbors
83
- ) ;
86
+ // log!(
87
+ // "cell[{}, {}] is initially {:?} and has {} live neighbors",
88
+ // row,
89
+ // col,
90
+ // cell,
91
+ // live_neighbors
92
+ // );
84
93
85
94
let next_cell = match ( cell, live_neighbors) {
86
95
// Rule 1: Any live cell with fewer than two live neighbours
@@ -99,7 +108,7 @@ impl Universe {
99
108
( otherwise, _) => otherwise,
100
109
} ;
101
110
102
- log ! ( "it becomes {:?}" , next_cell) ;
111
+ // log!("it becomes {:?}", next_cell);
103
112
104
113
next[ idx] = next_cell;
105
114
}
@@ -160,6 +169,11 @@ impl Universe {
160
169
self . height = height;
161
170
self . cells = ( 0 ..self . width * height) . map ( |_i| Cell :: Dead ) . collect ( ) ;
162
171
}
172
+
173
+ pub fn toggle_cell ( & mut self , row : u32 , col : u32 ) {
174
+ let idx = self . get_index ( row, col) ;
175
+ self . cells [ idx] . toggle ( ) ;
176
+ }
163
177
}
164
178
165
179
impl fmt:: Display for Universe {
0 commit comments