Skip to content

Commit 97a238a

Browse files
author
nitame
committed
Issue rustwasm#255 - Attempt to make explanation in debugging section better
1 parent 366042a commit 97a238a

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/game-of-life/debugging.md

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ features = [
4949
```
5050

5151
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:
5353

5454
[logging]: ../reference/debugging.html#logging-with-the-console-apis
5555

@@ -68,35 +68,38 @@ Now, we can start logging messages to the console by inserting calls to `log` in
6868
Rust code. For example, to log each cell's state, live neighbors count, and next
6969
state, we could modify `wasm-game-of-life/src/lib.rs` like this:
7070

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+
}
100103
```
101104

102105
## Using a Debugger to Pause Between Each Tick

0 commit comments

Comments
 (0)