From ffb8d1e43cfdbddcc529efbc60ddd3470dbd9724 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 9 Dec 2012 22:33:42 -0500 Subject: [PATCH] Implementing dead cell reproduction --- conway/go/conway_test.go | 24 ++++++++++++++++++++ conway/go/game_of_life.go | 46 +++++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/conway/go/conway_test.go b/conway/go/conway_test.go index 6884353..8d5149e 100644 --- a/conway/go/conway_test.go +++ b/conway/go/conway_test.go @@ -18,6 +18,7 @@ func init() { tsis.Set(1, 0, 1) tsis.Set(1, 1, 1) tsis.Set(2, 2, 1) + tsis.Set(3, 0, 1) tsis.Set(3, 2, 1) } @@ -89,6 +90,29 @@ func TestLiveCellWithMoreThanThreeLiveNeighborsDies(t *testing.T) { } } +func TestDeadCellWithExactlyThreeLiveNeighborsBecomesAlive(t *testing.T) { + game := NewGameOfLife(16, 16) + err := game.ImportState(TEST_SIMPLE_INITIAL_STATE) + + if err != nil { + t.Error(err) + return + } + + game.EvaluateGeneration() + + value, err := game.State.Get(3, 1) + if err != nil { + t.Error(err) + return + } + + if value != 1 { + t.Errorf("%v != 1", value) + return + } +} + func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { game := NewGameOfLife(16, 16) if game.State.Height() != 16 || game.State.Width() != 16 { diff --git a/conway/go/game_of_life.go b/conway/go/game_of_life.go index fd61971..ca41b91 100644 --- a/conway/go/game_of_life.go +++ b/conway/go/game_of_life.go @@ -123,29 +123,43 @@ func (game *GameOfLife) EvaluateGeneration() error { return err } - if score < 2 { - err = game.State.Deaden(x, y) - if err != nil { - return err - } - continue - } - curState, err := game.State.Get(x, y) if err != nil { return err } - if curState == 1 && (score == 2 || score == 3) { - continue - } + if curState == 1 { - if curState == 1 && score > 3 { - err = game.State.Deaden(x, y) - if err != nil { - return err + if score < 2 { + err = game.State.Deaden(x, y) + if err != nil { + return err + } + continue + } + + if score == 2 || score == 3 { + continue + } + + if score > 3 { + err = game.State.Deaden(x, y) + if err != nil { + return err + } + continue + } + } else if curState == 0 { + + if score == 3 { + err = game.State.Enliven(x, y) + if err != nil { + return err + } + continue + } else { + continue } - continue } } }