Skip to content

Commit 52a09e9

Browse files
authored
Update conways_game_of_life.py
1 parent 275ebd7 commit 52a09e9

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cellular_automata/conways_game_of_life.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Conway's Game of Life implemented in Python.
22
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life"""
33

4+
# Import block
45
from __future__ import annotations
56

67
from PIL import Image
78

9+
# Constants
810
GLIDER = [
911
[0, 1, 0, 0, 0, 0, 0, 0],
1012
[0, 0, 1, 0, 0, 0, 0, 0],
@@ -22,6 +24,7 @@
2224
[0, 1, 0]
2325
]
2426

27+
# Functions
2528
def new_generation(cells: list[list[int]]) -> list[list[int]]:
2629
"""Generates the next generation for a given state of Conway's Game of Life."""
2730
next_gen = [[0] * len(row) for row in cells]
@@ -31,7 +34,8 @@ def new_generation(cells: list[list[int]]) -> list[list[int]]:
3134
cells[x][y] for x in range(i - 1, i + 2) for y in range(j - 1, j + 2)
3235
if 0 <= x < len(cells) and 0 <= y < len(row) and (x != i or y != j)
3336
)
34-
next_gen[i][j] = 1 if cell == 1 and 2 <= neighbours <= 3 or cell == 0 and neighbours == 3 else 0
37+
next_gen[i][j] = 1 if cell == 1 and 2 <= neighbours <= 3 or cell == 0 \
38+
and neighbours == 3 else 0
3539
return next_gen
3640

3741
def generate_images(cells: list[list[int]], frames: int) -> list[Image.Image]:

0 commit comments

Comments
 (0)