Skip to content

Commit 876111c

Browse files
committed
gol-embedded: reset when there are less than 100 changes
1 parent d788847 commit 876111c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

rust/game-of-life/gol-embedded/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,10 @@ fn main() -> ! {
9898
let mut count = 0;
9999

100100
loop {
101-
if count > 100 {
101+
if count > 10 {
102102
reset_random(&mut game, &rosc);
103103
count = 0;
104104
}
105-
count += 1;
106105

107106
display.clear();
108107
for idr in 0..64 {
@@ -119,7 +118,9 @@ fn main() -> ! {
119118
}
120119
}
121120
display.flush().unwrap();
122-
game.advance();
121+
if game.advance() < 100 {
122+
count += 1;
123+
}
123124
//delay.delay_ms(500);
124125
}
125126
}

rust/game-of-life/gol-rs/src/gol.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,23 @@ impl GameOfLife {
4444
}
4545
}
4646

47-
pub fn advance(self: &mut Self) {
47+
pub fn advance(self: &mut Self) -> usize {
48+
let mut changes = 0;
4849
let mut tmp = GameOfLife::new();
4950
tmp.screen = self.screen.clone();
5051
for idxr in 0..ROWS {
5152
for idxc in 0..COLUMNS {
5253
let index = idxr * COLUMNS + idxc;
5354
let new_state = tmp.next_state_at(idxr, idxc);
55+
let old_state = self.get_at(idxr, idxc).unwrap_or(false);
56+
changes += match (old_state != new_state) {
57+
true => 1,
58+
_ => 0
59+
};
5460
self.screen.set(index, new_state);
5561
}
5662
}
63+
changes
5764
}
5865

5966
pub fn count_alive_neighbors(self: &Self, row: usize, col: usize) -> usize {

0 commit comments

Comments
 (0)