@@ -49,7 +49,7 @@ features = [
49
49
```
50
50
51
51
For ergonomics, we'll wrap the ` console.log ` function up in a ` println! ` -style
52
- macro:
52
+ macro, we could add this to ` wasm-game-of-life/src/lib.rs ` file :
53
53
54
54
[ logging ] : ../reference/debugging.html#logging-with-the-console-apis
55
55
@@ -68,35 +68,38 @@ Now, we can start logging messages to the console by inserting calls to `log` in
68
68
Rust code. For example, to log each cell's state, live neighbors count, and next
69
69
state, we could modify ` wasm-game-of-life/src/lib.rs ` like this:
70
70
71
- ``` diff
72
- diff --git a/src/lib.rs b/src/lib.rs
73
- index f757641..a30e107 100755
74
- --- a/src/lib.rs
75
- +++ b/src/lib.rs
76
- @@ -123,6 +122,14 @@ impl Universe {
77
- let cell = self.cells[idx];
78
- let live_neighbors = self.live_neighbor_count(row, col);
79
-
80
- + log!(
81
- + "cell[{}, {}] is initially {:?} and has {} live neighbors",
82
- + row,
83
- + col,
84
- + cell,
85
- + live_neighbors
86
- + );
87
- +
88
- let next_cell = match (cell, live_neighbors) {
89
- // Rule 1: Any live cell with fewer than two live neighbours
90
- // dies, as if caused by underpopulation.
91
- @@ -140,6 +147,8 @@ impl Universe {
92
- (otherwise, _) => otherwise,
93
- };
94
-
95
- + log!(" it becomes {:?}", next_cell);
96
- +
97
- next[idx] = next_cell;
98
- }
99
- }
71
+ ``` rust
72
+ #[wasm_bindgen]
73
+ impl Universe {
74
+ // ...
75
+ pub fn tick (& mut self ) {
76
+ let mut next = self . cells. clone ();
77
+
78
+ for row in 0 .. self . height {
79
+ for col in 0 .. self . width {
80
+ let idx = self . get_index (row , col );
81
+ let cell = self . cells[idx ];
82
+ let live_neighbors = self . live_neighbor_count (row , col );
83
+
84
+ log! (
85
+ " cell[{}, {}] is initially {:?} and has {} live neighbors" ,
86
+ row ,
87
+ col ,
88
+ cell ,
89
+ live_neighbors
90
+ );
91
+
92
+ // ...
93
+
94
+ log! (" it becomes {:?}" , next_cell );
95
+
96
+ next [idx ] = next_cell ;
97
+ }
98
+ }
99
+ // ...
100
+ }
101
+ // ...
102
+ }
100
103
```
101
104
102
105
## Using a Debugger to Pause Between Each Tick
0 commit comments