From 00886d2e07d4cffe575dc01a5d2c3bfe2f81a131 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 15 Apr 2024 01:26:37 +0200 Subject: [PATCH] boundary wrapping --- conway.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/conway.go b/conway.go index 777fad5..2799053 100644 --- a/conway.go +++ b/conway.go @@ -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++