Code

One Generation Later

Each tick of the Game of Life makes a new generation. Every cell looks at its live neighbours:

  • A live cell with 2 or 3 live neighbours survives.
  • A dead cell with exactly 3 live neighbours comes alive.
  • Every other cell is dead next time (too lonely or too crowded).
. . .      . # .
# # #  ->  . # .     a "blinker" flips back and forth
. . .      . # .

All at once

Every cell must look at the old generation. If you change cells in the grid while still counting, later cells see a half-updated world and the rules break. So build a brand-new grid and leave the old one untouched. A function like this, which returns a new value and changes nothing, is called pure.

Assignment

  1. next_generation(grid) returns a new grid, the same size, with 1 for cells alive next time and 0 for dead ones.
  2. Use neighbors (already written) and the three rules.
  3. Don't change grid. The program shows a blinker before and after one tick.