box-o-sand/conway/generation_score_card.go

63 lines
1.1 KiB
Go
Raw Normal View History

2012-12-12 03:50:47 +00:00
package conway
var (
neighbors = []neighbor{
neighbor{-1, -1},
neighbor{-1, 0},
neighbor{-1, 1},
neighbor{0, -1},
neighbor{0, 1},
neighbor{1, -1},
neighbor{1, 0},
neighbor{1, 1},
}
)
type GenerationScoreCard struct {
GameState
}
type neighbor struct {
X int
Y int
}
func NewGenerationScoreCard(height, width int) *GenerationScoreCard {
genScore := &GenerationScoreCard{}
for i := 0; i < height; i++ {
row := &GameStateRow{Y: i}
2012-12-12 03:50:47 +00:00
for j := 0; j < width; j++ {
row.Cells = append(row.Cells, &GameStateCell{
Value: 0,
X: j,
Y: i,
})
2012-12-12 03:50:47 +00:00
}
genScore.Rows = append(genScore.Rows, row)
}
return genScore
}
func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
stateCells, err := state.Cells()
if err != nil {
return err
}
for stateCell := range stateCells {
if stateCell.Value == 0 {
continue
}
for _, neighbor := range neighbors {
xTarget := stateCell.X + neighbor.X
yTarget := stateCell.Y + neighbor.Y
cell := genScore.Get(xTarget, yTarget)
genScore.Set(xTarget, yTarget, cell.Value+1)
2012-12-12 03:50:47 +00:00
}
}
return nil
}