boundary wrapping

This commit is contained in:
Baipyrus 2024-04-15 01:26:37 +02:00
parent 0c3d18afbe
commit 00886d2e07

View File

@ -13,6 +13,10 @@ type Cell struct {
}
func (c *Cell) NeighborCount(arr [][]Cell) int {
// Read grid dimensions
width := len(arr)
height := len(arr[0])
count := 0
// Iterate through 3x3 neighboring grid
for i := -1; i <= 1; i++ {
@ -22,11 +26,8 @@ func (c *Cell) NeighborCount(arr [][]Cell) int {
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
}
nx := (c.x + i + width) % width
ny := (c.y + j + height) % height
// Count if neighbor is alive
if arr[nx][ny].live {
count++