use cells instead of only bools, use pointer list to all neighbors

This commit is contained in:
2024-06-14 17:53:58 +02:00
committed by T.v.Dein
parent e516b218fd
commit 7b0a74fb93
2 changed files with 97 additions and 71 deletions

View File

@@ -12,35 +12,100 @@ import (
"github.com/tlinden/golsky/rle"
)
type Cell struct {
State bool
Neighbors [8]*Cell
NeighborCount int
}
func (cell *Cell) Count() int {
count := 0
for idx := 0; idx < cell.NeighborCount; idx++ {
count += bool2int(cell.Neighbors[idx].State)
}
return count
}
type Grid struct {
Data [][]bool
Width, Height, Density int
Empty bool
Data [][]*Cell
Empty bool
Config *Config
}
// Create new empty grid and allocate Data according to provided dimensions
func NewGrid(width, height, density int, empty bool) *Grid {
func NewGrid(config *Config) *Grid {
grid := &Grid{
Height: height,
Width: width,
Density: density,
Data: make([][]bool, height),
Empty: empty,
Data: make([][]*Cell, config.Height),
Empty: config.Empty,
Config: config,
}
for y := 0; y < height; y++ {
grid.Data[y] = make([]bool, width)
// first setup the cells
for y := 0; y < config.Height; y++ {
grid.Data[y] = make([]*Cell, config.Width)
for x := 0; x < config.Width; x++ {
grid.Data[y][x] = &Cell{}
}
}
// in a second pass, collect pointers to the neighbors of each cell
for y := 0; y < config.Height; y++ {
for x := 0; x < config.Width; x++ {
grid.SetupNeighbors(x, y)
}
}
return grid
}
func (grid *Grid) SetupNeighbors(x, y int) {
idx := 0
for nbgY := -1; nbgY < 2; nbgY++ {
for nbgX := -1; nbgX < 2; nbgX++ {
var col, row int
if grid.Config.Wrap {
// 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
} else {
// In traditional grid mode the edges are deadly
if x+nbgX < 0 || x+nbgX >= grid.Config.Width || y+nbgY < 0 || y+nbgY >= grid.Config.Height {
continue
}
col = x + nbgX
row = y + nbgY
}
if col == x && row == y {
continue
}
grid.Data[y][x].Neighbors[idx] = grid.Data[row][col]
grid.Data[y][x].NeighborCount++
idx++
}
}
}
// count the living neighbors of a cell
func (grid *Grid) CountNeighbors(x, y int) int {
return grid.Data[y][x].Count()
}
// Create a new 1:1 instance
func (grid *Grid) Clone() *Grid {
newgrid := &Grid{}
newgrid.Width = grid.Width
newgrid.Height = grid.Height
newgrid.Config = grid.Config
newgrid.Data = grid.Data
return newgrid
@@ -59,7 +124,7 @@ func (grid *Grid) Copy(other *Grid) {
func (grid *Grid) Clear() {
for y := range grid.Data {
for x := range grid.Data[y] {
grid.Data[y][x] = false
grid.Data[y][x].State = false
}
}
}
@@ -69,8 +134,8 @@ func (grid *Grid) FillRandom() {
if !grid.Empty {
for y := range grid.Data {
for x := range grid.Data[y] {
if rand.Intn(grid.Density) == 1 {
grid.Data[y][x] = true
if rand.Intn(grid.Config.Density) == 1 {
grid.Data[y][x].State = true
}
}
}
@@ -78,9 +143,9 @@ func (grid *Grid) FillRandom() {
}
func (grid *Grid) Dump() {
for y := 0; y < grid.Height; y++ {
for x := 0; x < grid.Width; x++ {
if grid.Data[y][x] {
for y := 0; y < grid.Config.Height; y++ {
for x := 0; x < grid.Config.Width; x++ {
if grid.Data[y][x].State {
fmt.Print("XX")
} else {
fmt.Print(" ")
@@ -93,8 +158,8 @@ func (grid *Grid) Dump() {
// initialize using a given RLE pattern
func (grid *Grid) LoadRLE(pattern *rle.RLE) {
if pattern != nil {
startX := (grid.Width / 2) - (pattern.Width / 2)
startY := (grid.Height / 2) - (pattern.Height / 2)
startX := (grid.Config.Width / 2) - (pattern.Width / 2)
startY := (grid.Config.Height / 2) - (pattern.Height / 2)
var y, x int
for rowIndex, patternRow := range pattern.Pattern {
@@ -103,7 +168,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
x = colIndex + startX
y = rowIndex + startY
grid.Data[y][x] = true
grid.Data[y][x].State = true
}
}
}
@@ -214,7 +279,7 @@ func (grid *Grid) SaveState(filename, rule string) error {
for y := range grid.Data {
for _, cell := range grid.Data[y] {
row := "."
if cell {
if cell.State {
row = "o"
}