63 lines
1.1 KiB
Go
63 lines
1.1 KiB
Go
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, &GameStateCell{
|
|
Value: 0,
|
|
X: j,
|
|
Y: i,
|
|
})
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|