adapting to IntGrids

This commit is contained in:
Stefan Harmuth 2020-12-30 00:38:15 +01:00
parent 9e7e470498
commit 7d03482c4a

View File

@ -4,20 +4,15 @@ import (
"tools" "tools"
) )
type coord struct {
x int
y int
}
func play(grid tools.ToggleGrid, stuckCorners bool) { func play(grid tools.ToggleGrid, stuckCorners bool) {
neighbours := make(map[coord]int) neighbours := tools.NewIntGrid()
for thisCoord := range grid.GetOnCoordinates() { for thisCoord := range grid.GetCoordinates() {
for cx := thisCoord.X - 1; cx <= thisCoord.X+1; cx++ { for cx := thisCoord.X - 1; cx <= thisCoord.X+1; cx++ {
for cy := thisCoord.Y - 1; cy <= thisCoord.Y+1; cy++ { for cy := thisCoord.Y - 1; cy <= thisCoord.Y+1; cy++ {
if cy == thisCoord.Y && cx == thisCoord.X { if cy == thisCoord.Y && cx == thisCoord.X {
continue continue
} }
neighbours[coord{cx, cy}]++ neighbours.Add(cx, cy, 1)
} }
} }
} }
@ -25,12 +20,12 @@ func play(grid tools.ToggleGrid, stuckCorners bool) {
for x := 0; x <= grid.MaxX; x++ { for x := 0; x <= grid.MaxX; x++ {
for y := 0; y <= grid.MaxY; y++ { for y := 0; y <= grid.MaxY; y++ {
if grid.State(x, y) { if grid.State(x, y) {
if neighbours[coord{x, y}] != 2 && neighbours[coord{x, y}] != 3 { if neighbours.Value(x, y) != 2 && neighbours.Value(x, y) != 3 {
if !stuckCorners || !grid.IsCorner(x, y) { if !stuckCorners || !grid.IsCorner(x, y) {
grid.Set(x, y, tools.Off) grid.Set(x, y, tools.Off)
} }
} }
} else if neighbours[coord{x, y}] == 3 { } else if neighbours.Value(x, y) == 3 {
grid.Set(x, y, tools.On) grid.Set(x, y, tools.On)
} }
} }