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()
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
}

View File

@ -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)
}

View File

@ -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)
}