Count the neighbors, then decide
Seed a blinker in Conway's Game of Life, count the eight neighbors with array rolls, and watch a three-cell line come back as itself two generations later.
Conway’s Game of Life is the same machinery as the one-dimensional rules with a larger lookup: a two-dimensional grid, two states, the Moore neighborhood, and one local rule. The rule is usually written B3/S23: a dead cell is born with exactly 3 live neighbors, and a live cell survives with 2 or 3. Every cell’s fate depends on nine bits rather than three.
The program below seeds the chapter’s blinker, counts neighbors with eight array rolls, applies B3/S23, and prints the region around the pattern as text. Four generations are enough to show the whole cycle.
Run it
Save this as life.py and run it with Python 3.11 or later. It needs NumPy
only.
import numpy as np
grid = np.zeros((40, 60), dtype=np.uint8)
grid[20, 29:32] = 1
def neighbor_count(grid):
total = np.zeros_like(grid, dtype=np.uint8)
for dy in (-1, 0, 1):
for dx in (-1, 0, 1):
if dx == 0 and dy == 0:
continue
total += np.roll(np.roll(grid, dy, axis=0), dx, axis=1)
return total
def life_step(grid):
n = neighbor_count(grid)
born = (grid == 0) & (n == 3)
survive = (grid == 1) & ((n == 2) | (n == 3))
return (born | survive).astype(np.uint8)
def show(patch):
return "\n".join(
"".join("#" if cell else "." for cell in row) for row in patch
)
for generation in range(4):
print(f"generation {generation}")
print(show(grid[18:23, 27:34]))
grid = life_step(grid)The program is the chapter’s own code composed into one file: the grid, the
neighbor count, the B3/S23 step, and the four-generation print loop. The only
addition is the show helper, which renders the chapter’s
grid[18:23, 27:34] slice as # and . instead of NumPy’s array
representation.
Expected outcome
generation 0
.......
.......
..###..
.......
.......
generation 1
.......
...#...
...#...
...#...
.......
generation 2
.......
.......
..###..
.......
.......
generation 3
.......
...#...
...#...
...#...
.......The horizontal bar becomes a vertical bar and then becomes the horizontal bar again. Generation 2 is identical to generation 0, so the pattern is a genuine period-2 oscillator. No cell in the program knows that.
What the neighbor count actually does
neighbor_count is the whole neighborhood. It adds eight shifted copies of
the grid, skipping the center, so a cell’s new value is the sum of the eight
cells around it.
def neighbor_count(grid):
total = np.zeros_like(grid, dtype=np.uint8)
for dy in (-1, 0, 1):
for dx in (-1, 0, 1):
if dx == 0 and dy == 0:
continue
total += np.roll(np.roll(grid, dy, axis=0), dx, axis=1)
return totalTwo properties follow from np.roll itself. The chapter states it plainly:
“np.roll reintroduces wrapped-around elements by definition”, so the top
row connects to the bottom row and the left edge to the right edge. This 40 by
60 world is a torus, not an unbounded plane, and that is a modeling choice
rather than an implementation accident.
The second property is that the count is a number, not a birth or a death. Everything the rule needs to say is a comparison on that number, so the rule and the neighborhood stay separate:
n == 3on a dead cell: it is bornn == 2orn == 3on a live cell: it survives- anything else: it is dead
The chapter’s discipline still applies: every birth and death is computed from
the same current generation, because life_step writes a new array rather
than updating the grid in place.
Mechanism and limitations
Documented: the chapter states the rule as B3/S23, with a dead cell born
on exactly 3 live neighbors and a live cell surviving on 2 or 3; it states
that np.roll makes the grid periodic, so the world is a torus; it notes
that the blinker “alternates between horizontal and vertical states” and that
this is “a genuine period-2 cycle”; and it separates the implementation
ontology (cells plus rules) from the observer ontology (gliders plus blinkers
plus spaceships).
Observed: the run above. The horizontal seed became vertical at generation 1, and the patch printed at generation 2 was identical to the patch printed at generation 0, so the period-2 claim is confirmed by the output itself. The printed patch is seven columns by five rows, and the seed was the only thing in the grid.
Not shown. Only one of the chapter’s patterns is exercised. The glider, the block, the beehive and the methuselah are named in the chapter but not run, so no claim is made here about translation, about still lifes, or about any pattern that does not fit in this window. The torus is in force for the whole grid, but the printed window never touches an edge, so the finite-size warning the chapter raises about gliders wrapping into their own trail is not demonstrated. The chapter’s population measurement, bounding box, period detection and entropy list are not implemented, and nothing here supports the chapter’s sharper claim that a local configuration has become a moving object.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.