Matrix/conway.go

107 lines
2.2 KiB
Go
Raw Normal View History

2024-04-12 13:14:33 +00:00
package main
import (
2024-04-14 19:20:26 +00:00
"errors"
"time"
2024-04-12 13:14:33 +00:00
)
type Cell struct {
x int
y int
live bool
}
2024-04-14 19:20:26 +00:00
func (c *Cell) NeighborCount(arr [][]Cell) int {
count := 0
// Iterate through 3x3 neighboring grid
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
// Ignore own position
if i == 0 && j == 0 {
continue
}
// Get neighbor coordinates
nx, ny := c.x+i, c.y+j
// Check if neighbor is within bounds
if nx < 0 || nx >= len(arr) || ny < 0 || ny >= len(arr[0]) {
continue
}
// Count if neighbor is alive
if arr[nx][ny].live {
count++
}
}
}
return count
2024-04-12 13:14:33 +00:00
}
2024-04-14 19:20:26 +00:00
func initGrid(width, height int, parent ...[][]Cell) ([][]Cell, error) {
exists := len(parent) == 1
if exists && len(parent) > 1 {
return nil, errors.New("Too many parents specified")
}
// Make first dimension
cells := make([][]Cell, width)
for i := 0; i < width; i++ {
// Make second dimension
cells[i] = make([]Cell, height)
for j := 0; j < height; j++ {
// Make cells
cells[i][j].x = i
cells[i][j].y = j
// If specified, copy state from parent
if exists {
cells[i][j].live = parent[0][i][j].live
}
}
}
return cells, nil
2024-04-12 13:14:33 +00:00
}
2024-04-14 19:20:26 +00:00
const FPS = 10
2024-04-12 13:14:33 +00:00
2024-04-14 19:20:26 +00:00
var grid [][]Cell
2024-04-12 13:14:33 +00:00
2024-04-14 19:20:26 +00:00
func setup(callback func([][]Cell), width, height int) {
grid, _ = initGrid(width, height)
2024-04-12 13:14:33 +00:00
2024-04-14 19:20:26 +00:00
// Prepare
ticker := time.NewTicker(time.Second / FPS)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
draw(callback)
}
}
}()
}
2024-04-12 13:14:33 +00:00
2024-04-14 19:20:26 +00:00
func draw(callback func([][]Cell)) {
generation, _ := initGrid(len(grid), len(grid[0]), grid)
// Iterate through grid
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
// Count neighbors
cout := grid[i][j].NeighborCount(grid)
// Get state
curr := grid[i][j].live
// Apply rules
died := curr && (cout < 2 || cout > 3)
born := !curr && cout == 3
next := curr && !died || born
// Set state
generation[i][j].live = next
// A * !(A * (B + C)) + !A * D
// = A * !B * !C + !A * D
// curr && cout >= 2 && cout <= 3 || !curr && cout == 3
}
}
callback(generation)
2024-04-12 13:14:33 +00:00
}