From 3bfa23fff28d59772288e22f03f85cfee1b615a3 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 16 Dec 2012 16:04:54 -0500 Subject: [PATCH] Have we achieved torus? --- conway/conway_test.go | 6 +++--- conway/game_state.go | 8 ++++++++ conway/generation_score_card.go | 9 --------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/conway/conway_test.go b/conway/conway_test.go index d7215e3..f8cd8f2 100644 --- a/conway/conway_test.go +++ b/conway/conway_test.go @@ -60,7 +60,7 @@ func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(t *testing.T) { game.EvaluateGeneration() - cell := game.State.Get(0, 0) + cell := game.State.Get(0, 2) if cell.Value != 1 { t.Errorf("%v != 1", cell.Value) return @@ -262,8 +262,8 @@ func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T genScore.Calculate(state) cell := genScore.Get(0, 0) - if cell.Value != 3 { - t.Errorf("[0, 0] value %v != 3", cell.Value) + if cell.Value != 4 { + t.Errorf("[0, 0] value %v != 4", cell.Value) return } diff --git a/conway/game_state.go b/conway/game_state.go index 0438fba..d281be3 100644 --- a/conway/game_state.go +++ b/conway/game_state.go @@ -59,6 +59,10 @@ func (state *GameState) Width() int { func (state *GameState) GetRow(y int) *GameStateRow { lenRows := len(state.Rows) + if y < 0 { + return state.GetRow(lenRows + y) + } + if y+1 > lenRows { return state.GetRow(y % lenRows) } @@ -75,6 +79,10 @@ func (state *GameState) Get(x, y int) *GameStateCell { row := state.GetRow(y) lenCells := len(row.Cells) + if x < 0 { + return state.Get(lenCells+x, row.Y) + } + if x+1 > lenCells { return state.Get(x%lenCells, row.Y) } diff --git a/conway/generation_score_card.go b/conway/generation_score_card.go index bc055df..8c308f5 100644 --- a/conway/generation_score_card.go +++ b/conway/generation_score_card.go @@ -39,7 +39,6 @@ func NewGenerationScoreCard(height, width int) *GenerationScoreCard { } func (genScore *GenerationScoreCard) Calculate(state *GameState) error { - stateWidth := state.Width() stateCells, err := state.Cells() if err != nil { return err @@ -54,14 +53,6 @@ func (genScore *GenerationScoreCard) Calculate(state *GameState) error { xTarget := stateCell.X + neighbor.X yTarget := stateCell.Y + neighbor.Y - if xTarget < 0 || yTarget < 0 { - continue - } - - if xTarget+1 > stateWidth || yTarget+1 > stateWidth { - continue - } - cell := genScore.Get(xTarget, yTarget) genScore.Set(xTarget, yTarget, cell.Value+1) }