4 Commits

3 changed files with 67 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ type Grid struct {
Neighbors [][]Neighbor
Empty bool
Config *Config
Counter func(x, y int) uint8
}
// Create new empty grid and allocate Data according to provided dimensions
@@ -58,6 +59,12 @@ func NewGrid(config *Config) *Grid {
}
}
if grid.Config.Wrap {
grid.Counter = grid.CountNeighborsWrap
} else {
grid.Counter = grid.CountNeighbors
}
return grid
}
@@ -101,8 +108,62 @@ func (grid *Grid) SetupNeighbors(x, y int) {
grid.Neighbors[y+STRIDE*x] = neighbors
}
// count the living neighbors of a cell
func (grid *Grid) CountNeighborsWrap(x, y int) uint8 {
var sum uint8
for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ {
var col, row int
// In wrap mode we look at all the 8 neighbors surrounding us.
// In case we are on an edge we'll look at the neighbor on the
// other side of the grid, thus wrapping lookahead around
// using the mod() function.
col = (x + nbgX + grid.Config.Width) % grid.Config.Width
row = (y + nbgY + grid.Config.Height) % grid.Config.Height
sum += grid.Data[row+STRIDE*col]
}
}
// don't count ourselfes though
sum -= grid.Data[y+STRIDE*x]
return sum
}
func (grid *Grid) CountNeighbors(x, y int) uint8 {
var sum uint8
width := grid.Config.Width
height := grid.Config.Height
for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ {
var col, row int
xnbgX := x + nbgX
ynbgY := y + nbgY
// In traditional grid mode the edges are deadly
if xnbgX < 0 || xnbgX >= width || ynbgY < 0 || ynbgY >= height {
continue
}
col = xnbgX
row = ynbgY
sum += grid.Data[row+STRIDE*col]
}
}
// don't count ourselfes though
sum -= grid.Data[y+STRIDE*x]
return sum
}
// count the living neighbors of a cell
func (grid *Grid) _CountNeighbors(x, y int) uint8 {
var count uint8
pos := y + STRIDE*x

View File

@@ -167,7 +167,7 @@ func (scene *ScenePlay) UpdateCells() {
for x := 0; x < width; x++ {
state := scene.Grids[scene.Index].Data[y+STRIDE*x] // 0|1 == dead or alive
neighbors := scene.Grids[scene.Index].CountNeighbors(x, y)
neighbors := scene.Grids[scene.Index].Counter(x, y)
// actually apply the current rules
nextstate := scene.RuleCheckFunc(state, neighbors)

View File

@@ -42,10 +42,10 @@ var THEMES = map[string]ThemeDef{
dead: "5a5a5a",
old: "ff1e1e",
grid: "808080",
age1: "735f52",
age2: "6c6059",
age3: "635d59",
age4: "7b5e4b",
age3: "6c6059",
age2: "735f52",
age1: "7b5e4b",
age4: "635d59",
},
"dark": {
life: "c8c8c8",