Matrix/conway.go

114 lines
2.4 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"
2024-04-14 20:01:42 +00:00
"math/rand"
2024-04-14 19:20:26 +00:00
"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
2024-04-14 20:01:42 +00:00
} else {
cells[i][j].live = rand.Intn(2) == 1
2024-04-14 19:20:26 +00:00
}
}
}
return cells, nil
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 20:00:37 +00:00
func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan bool {
// Initialize grid
2024-04-14 19:20:26 +00:00
grid, _ = initGrid(width, height)
2024-04-12 13:14:33 +00:00
2024-04-14 19:29:40 +00:00
// Prepare ticker and finishing flag
2024-04-14 20:00:37 +00:00
ticker := time.NewTicker((1000 / FPS) * time.Millisecond)
2024-04-14 19:20:26 +00:00
done := make(chan bool)
2024-04-14 19:29:40 +00:00
// Run game loop
2024-04-14 19:20:26 +00:00
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
draw(callback)
}
}
}()
2024-04-14 19:29:40 +00:00
// Return flag to be stopped outside
return done
2024-04-14 19:20:26 +00:00
}
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
}
}
2024-04-14 20:34:16 +00:00
grid = generation
2024-04-14 19:20:26 +00:00
callback(generation)
2024-04-12 13:14:33 +00:00
}