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
next_generation(grid)returns a new grid, the same size, with1for cells alive next time and0for dead ones.- Use
neighbors(already written) and the three rules. - Don't change
grid. The program shows a blinker before and after one tick.
next_generation(grid)returns a new grid, the same size, with1for cells alive next time and0for dead ones.- Use
neighbors(already written) and the three rules. - Don't change
grid. The program shows a blinker before and after one tick.
main.py Python sleeping
Press Run to try your code, then Check to test it.