localize grid variable

This commit is contained in:
Baipyrus 2024-04-15 00:55:34 +02:00
parent 60a7e38c67
commit 9f2f76aa70

View File

@ -62,11 +62,9 @@ func initGrid(width, height int, parent ...[][]Cell) ([][]Cell, error) {
return cells, nil return cells, nil
} }
var grid [][]Cell
func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan bool { func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan bool {
// Initialize grid // Initialize grid
grid, _ = initGrid(width, height) grid, _ := initGrid(width, height)
// Prepare ticker and finishing flag // Prepare ticker and finishing flag
ticker := time.NewTicker((1000 / FPS) * time.Millisecond) ticker := time.NewTicker((1000 / FPS) * time.Millisecond)
@ -79,7 +77,7 @@ func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan b
case <-done: case <-done:
return return
case <-ticker.C: case <-ticker.C:
draw(callback) grid = draw(grid, callback)
} }
} }
}() }()
@ -88,7 +86,7 @@ func setup(callback func([][]Cell), width, height int, FPS time.Duration) chan b
return done return done
} }
func draw(callback func([][]Cell)) { func draw(grid [][]Cell, callback func([][]Cell)) [][]Cell {
generation, _ := initGrid(len(grid), len(grid[0]), grid) generation, _ := initGrid(len(grid), len(grid[0]), grid)
// Iterate through grid // Iterate through grid
for i := 0; i < len(grid); i++ { for i := 0; i < len(grid); i++ {
@ -108,6 +106,6 @@ func draw(callback func([][]Cell)) {
// curr && cout >= 2 && cout <= 3 || !curr && cout == 3 // curr && cout >= 2 && cout <= 3 || !curr && cout == 3
} }
} }
grid = generation
callback(generation) callback(generation)
return generation
} }