auto reset checking across 2 generations at a time

This commit is contained in:
Baipyrus 2024-04-17 11:17:19 +02:00
parent a9d600aaed
commit 2b709d4d05

View File

@ -37,6 +37,17 @@ func (c *Cell) NeighborCount(arr [][]Cell) uint {
return count
}
func compareGrids(a, b [][]Cell) bool {
for i := 0; i < len(a); i++ {
for j := 0; j < len(a[0]); j++ {
if a[i][j].live != b[i][j].live {
return false
}
}
}
return true
}
func initGrid(width, height uint, parent ...[][]Cell) ([][]Cell, error) {
exists := len(parent) == 1
if exists && len(parent) > 1 {
@ -65,20 +76,32 @@ func initGrid(width, height uint, parent ...[][]Cell) ([][]Cell, error) {
func setup(callback func([][]Cell), scale, width, height uint, FPS time.Duration) chan bool {
// Initialize grid at specified scale
grid, _ := initGrid(width/scale, height/scale)
gridWidth := width / scale
gridHeight := height / scale
grid, _ := initGrid(gridWidth, gridHeight)
// Prepare ticker and finishing flag
ticker := time.NewTicker((1000 / FPS) * time.Millisecond)
done := make(chan bool)
// Run game loop
var parent [][]Cell
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
grid = draw(grid, callback)
temp := draw(grid, callback)
if parent != nil && compareGrids(parent, temp) {
parent = nil
grid, _ = initGrid(gridWidth, gridHeight)
} else {
parent, _ = initGrid(gridWidth, gridHeight, grid)
grid = temp
}
}
}
}()