From c2ffaee97908199880dac50e6d411ffd5505985a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 9 Dec 2012 22:16:16 -0500 Subject: [PATCH] Implementing overcrowding rule --- conway/go/conway_test.go | 121 +++++++++++++++++++++++--------------- conway/go/game_of_life.go | 18 ++++++ 2 files changed, 91 insertions(+), 48 deletions(-) diff --git a/conway/go/conway_test.go b/conway/go/conway_test.go index 735eb34..6884353 100644 --- a/conway/go/conway_test.go +++ b/conway/go/conway_test.go @@ -13,10 +13,80 @@ var TEST_SIMPLE_INITIAL_STATE = NewGameState(16, 16) func init() { tsis := TEST_SIMPLE_INITIAL_STATE tsis.Set(0, 0, 1) - tsis.Set(1, 0, 1) tsis.Set(0, 1, 1) + tsis.Set(0, 2, 1) + tsis.Set(1, 0, 1) tsis.Set(1, 1, 1) tsis.Set(2, 2, 1) + tsis.Set(3, 2, 1) +} + +func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { + game := NewGameOfLife(16, 16) + err := game.State.Import(TEST_SIMPLE_INITIAL_STATE) + if err != nil { + t.Error(err) + return + } + + game.EvaluateGeneration() + + value, err := game.State.Get(3, 2) + if err != nil { + t.Error(err) + return + } + + if value != 0 { + t.Errorf("%v != 0", value) + return + } +} + +func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(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(0, 0) + if err != nil { + t.Error(err) + return + } + + if value != 1 { + t.Errorf("%v != 1", value) + return + } +} + +func TestLiveCellWithMoreThanThreeLiveNeighborsDies(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(0, 1) + if err != nil { + t.Error(err) + return + } + + if value != 0 { + t.Errorf("%v != 0", value) + return + } } func TestNewGameOfLifeHasCorrectDimensions(t *testing.T) { @@ -66,51 +136,6 @@ func TestGameStateCanImportState(t *testing.T) { } } -func TestLiveCellWithFewerThanTwoLiveNeighborsDies(t *testing.T) { - game := NewGameOfLife(16, 16) - err := game.State.Import(TEST_SIMPLE_INITIAL_STATE) - if err != nil { - t.Error(err) - return - } - - game.EvaluateGeneration() - - value, err := game.State.Get(2, 2) - if err != nil { - t.Error(err) - return - } - - if value != 0 { - t.Errorf("%v != 0", value) - return - } -} - -func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(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(0, 0) - if err != nil { - t.Error(err) - return - } - - if value != 1 { - t.Errorf("%v != 1", value) - return - } -} - func TestNewGameStatesAreAllDead(t *testing.T) { state := NewGameState(4, 4) for x := 0; x < 3; x++ { @@ -203,12 +228,12 @@ func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T return } - value, err = genScore.Get(2, 2) + value, err = genScore.Get(3, 2) if err != nil { t.Error(err) return } else if value != 1 { - t.Errorf("[2, 2] value %d != 1", value) + t.Errorf("[3, 2] value %d != 1", value) return } } diff --git a/conway/go/game_of_life.go b/conway/go/game_of_life.go index d38eb5f..fd61971 100644 --- a/conway/go/game_of_life.go +++ b/conway/go/game_of_life.go @@ -128,6 +128,24 @@ func (game *GameOfLife) EvaluateGeneration() error { 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 && score > 3 { + err = game.State.Deaden(x, y) + if err != nil { + return err + } + continue } } }