mirror of
https://gitlab1.ptb.de/waltem01/Matrix
synced 2024-12-26 03:51:45 +00:00
boundary wrapping
This commit is contained in:
parent
0c3d18afbe
commit
00886d2e07
11
conway.go
11
conway.go
@ -13,6 +13,10 @@ type Cell struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cell) NeighborCount(arr [][]Cell) int {
|
func (c *Cell) NeighborCount(arr [][]Cell) int {
|
||||||
|
// Read grid dimensions
|
||||||
|
width := len(arr)
|
||||||
|
height := len(arr[0])
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
// Iterate through 3x3 neighboring grid
|
// Iterate through 3x3 neighboring grid
|
||||||
for i := -1; i <= 1; i++ {
|
for i := -1; i <= 1; i++ {
|
||||||
@ -22,11 +26,8 @@ func (c *Cell) NeighborCount(arr [][]Cell) int {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Get neighbor coordinates
|
// Get neighbor coordinates
|
||||||
nx, ny := c.x+i, c.y+j
|
nx := (c.x + i + width) % width
|
||||||
// Check if neighbor is within bounds
|
ny := (c.y + j + height) % height
|
||||||
if nx < 0 || nx >= len(arr) || ny < 0 || ny >= len(arr[0]) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// Count if neighbor is alive
|
// Count if neighbor is alive
|
||||||
if arr[nx][ny].live {
|
if arr[nx][ny].live {
|
||||||
count++
|
count++
|
||||||
|
Loading…
Reference in New Issue
Block a user