From 817c7e645395c99f50eae724650913d4b046a890 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Mon, 8 Apr 2019 19:06:08 -0700 Subject: [PATCH] Fix life algorithm You have to update the whole field atomically, you cannot change them as you scan the cells. So move the alive update into the draw function. Fixes #1 --- cell.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cell.go b/cell.go index e778241..699f26c 100644 --- a/cell.go +++ b/cell.go @@ -35,9 +35,6 @@ type cell struct { // checkState determines the state of the cell for the next tick of the game. func (c *cell) checkState(cells [][]*cell) { - c.alive = c.nextState - c.nextState = c.alive - liveCount := c.liveNeighbors(cells) if c.alive { // 1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. @@ -97,6 +94,7 @@ func (c *cell) liveNeighbors(cells [][]*cell) int { // draw draws the cell if it is alive. func (c *cell) draw() { + c.alive = c.nextState if !c.alive { return }