You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
box-o-sand/conway/generation_score_card.go

75 lines
1.3 KiB

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}
for j := 0; j < width; j++ {
row.Cells = append(row.Cells, 0)
}
genScore.Rows = append(genScore.Rows, row)
}
return genScore
}
func (genScore *GenerationScoreCard) Calculate(state *GameState) error {
stateWidth := state.Width()
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
if xTarget < 0 || yTarget < 0 {
continue
}
if xTarget+1 > stateWidth || yTarget+1 > stateWidth {
continue
}
curScore, err := genScore.Get(xTarget, yTarget)
if err != nil {
continue
}
err = genScore.Set(xTarget, yTarget, curScore+1)
if err != nil {
return err
}
}
}
return nil
}