Have we achieved torus?

This commit is contained in:
Dan Buch 2012-12-16 16:04:54 -05:00
parent 8056d86cbf
commit 3bfa23fff2
3 changed files with 11 additions and 12 deletions

View File

@ -60,7 +60,7 @@ func TestLiveCellWithTwoOrThreeLiveNeighborsLivesOn(t *testing.T) {
game.EvaluateGeneration() game.EvaluateGeneration()
cell := game.State.Get(0, 0) cell := game.State.Get(0, 2)
if cell.Value != 1 { if cell.Value != 1 {
t.Errorf("%v != 1", cell.Value) t.Errorf("%v != 1", cell.Value)
return return
@ -262,8 +262,8 @@ func TestGenerationScoreCardCalculateResultsInCorrectNeighborCounts(t *testing.T
genScore.Calculate(state) genScore.Calculate(state)
cell := genScore.Get(0, 0) cell := genScore.Get(0, 0)
if cell.Value != 3 { if cell.Value != 4 {
t.Errorf("[0, 0] value %v != 3", cell.Value) t.Errorf("[0, 0] value %v != 4", cell.Value)
return return
} }

View File

@ -59,6 +59,10 @@ func (state *GameState) Width() int {
func (state *GameState) GetRow(y int) *GameStateRow { func (state *GameState) GetRow(y int) *GameStateRow {
lenRows := len(state.Rows) lenRows := len(state.Rows)
if y < 0 {
return state.GetRow(lenRows + y)
}
if y+1 > lenRows { if y+1 > lenRows {
return state.GetRow(y % lenRows) return state.GetRow(y % lenRows)
} }
@ -75,6 +79,10 @@ func (state *GameState) Get(x, y int) *GameStateCell {
row := state.GetRow(y) row := state.GetRow(y)
lenCells := len(row.Cells) lenCells := len(row.Cells)
if x < 0 {
return state.Get(lenCells+x, row.Y)
}
if x+1 > lenCells { if x+1 > lenCells {
return state.Get(x%lenCells, row.Y) return state.Get(x%lenCells, row.Y)
} }

View File

@ -39,7 +39,6 @@ func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
} }
func (genScore *GenerationScoreCard) Calculate(state *GameState) error { func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
stateWidth := state.Width()
stateCells, err := state.Cells() stateCells, err := state.Cells()
if err != nil { if err != nil {
return err return err
@ -54,14 +53,6 @@ func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
xTarget := stateCell.X + neighbor.X xTarget := stateCell.X + neighbor.X
yTarget := stateCell.Y + neighbor.Y yTarget := stateCell.Y + neighbor.Y
if xTarget < 0 || yTarget < 0 {
continue
}
if xTarget+1 > stateWidth || yTarget+1 > stateWidth {
continue
}
cell := genScore.Get(xTarget, yTarget) cell := genScore.Get(xTarget, yTarget)
genScore.Set(xTarget, yTarget, cell.Value+1) genScore.Set(xTarget, yTarget, cell.Value+1)
} }