mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-25 11:31:46 +00:00
auto reset checking across 2 generations at a time
This commit is contained in:
parent
a9d600aaed
commit
2b709d4d05
27
conway.go
27
conway.go
@ -37,6 +37,17 @@ func (c *Cell) NeighborCount(arr [][]Cell) uint {
|
|||||||
return count
|
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) {
|
func initGrid(width, height uint, parent ...[][]Cell) ([][]Cell, error) {
|
||||||
exists := len(parent) == 1
|
exists := len(parent) == 1
|
||||||
if 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 {
|
func setup(callback func([][]Cell), scale, width, height uint, FPS time.Duration) chan bool {
|
||||||
// Initialize grid at specified scale
|
// 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
|
// Prepare ticker and finishing flag
|
||||||
ticker := time.NewTicker((1000 / FPS) * time.Millisecond)
|
ticker := time.NewTicker((1000 / FPS) * time.Millisecond)
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
|
|
||||||
// Run game loop
|
// Run game loop
|
||||||
|
var parent [][]Cell
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
Reference in New Issue
Block a user