@@ -26,7 +26,7 @@ def create_random_grid(
26
26
27
27
Args:
28
28
rows: Number of grid rows
29
- columns: Number of grid columns
29
+ columns: Number of grid columns
30
30
alive_probability: Probability (0.0-1.0) of each cell being initially alive
31
31
seed: Random seed for reproducibility
32
32
@@ -115,23 +115,25 @@ def count_von_neumann_neighbors(
115
115
down_neighbors = np .roll (alive_mask , 1 , axis = 0 )
116
116
left_neighbors = np .roll (alive_mask , - 1 , axis = 1 )
117
117
right_neighbors = np .roll (alive_mask , 1 , axis = 1 )
118
- neighbor_counts = up_neighbors + down_neighbors + left_neighbors + right_neighbors
118
+ neighbor_counts = (
119
+ up_neighbors + down_neighbors + left_neighbors + right_neighbors
120
+ )
119
121
else :
120
122
# Manually count neighbors without wraparound
121
123
for r in range (rows ):
122
124
for c in range (cols ):
123
125
count = 0
124
126
# Check up
125
- if r > 0 and alive_mask [r - 1 , c ]:
127
+ if r > 0 and alive_mask [r - 1 , c ]:
126
128
count += 1
127
129
# Check down
128
- if r < rows - 1 and alive_mask [r + 1 , c ]:
130
+ if r < rows - 1 and alive_mask [r + 1 , c ]:
129
131
count += 1
130
132
# Check left
131
- if c > 0 and alive_mask [r , c - 1 ]:
133
+ if c > 0 and alive_mask [r , c - 1 ]:
132
134
count += 1
133
135
# Check right
134
- if c < cols - 1 and alive_mask [r , c + 1 ]:
136
+ if c < cols - 1 and alive_mask [r , c + 1 ]:
135
137
count += 1
136
138
neighbor_counts [r , c ] = count
137
139
@@ -168,7 +170,7 @@ def apply_cellular_automaton_rules(
168
170
Examples:
169
171
>>> ages = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype=np.uint8)
170
172
>>> new_ages = apply_cellular_automaton_rules(
171
- ... ages, birth_neighbor_counts={2},
173
+ ... ages, birth_neighbor_counts={2},
172
174
... survival_neighbor_counts={2, 3}, use_wraparound=False
173
175
... )
174
176
>>> bool(new_ages[0, 0] > 0) # corner should be born (2 neighbors: right and down)
@@ -177,7 +179,7 @@ def apply_cellular_automaton_rules(
177
179
>>> # Test aging of dead cells
178
180
>>> dead_aging = np.array([[2, 0, 0]], dtype=np.uint8) # age 2, no survival
179
181
>>> result = apply_cellular_automaton_rules(
180
- ... dead_aging, birth_neighbor_counts=set(),
182
+ ... dead_aging, birth_neighbor_counts=set(),
181
183
... survival_neighbor_counts=set(), maximum_age=3
182
184
... )
183
185
>>> bool(result[0, 0] == 3) # should age from 2 to 3
@@ -198,8 +200,12 @@ def apply_cellular_automaton_rules(
198
200
)
199
201
200
202
# Determine which cells are born or survive
201
- birth_mask = (~ alive_cells_mask ) & np .isin (neighbor_counts , list (birth_neighbor_counts ))
202
- survival_mask = alive_cells_mask & np .isin (neighbor_counts , list (survival_neighbor_counts ))
203
+ birth_mask = (~ alive_cells_mask ) & np .isin (
204
+ neighbor_counts , list (birth_neighbor_counts )
205
+ )
206
+ survival_mask = alive_cells_mask & np .isin (
207
+ neighbor_counts , list (survival_neighbor_counts )
208
+ )
203
209
204
210
new_ages = current_ages .copy ()
205
211
@@ -299,4 +305,5 @@ def simulate_von_neumann_cellular_automaton(
299
305
300
306
if __name__ == "__main__" :
301
307
import doctest
308
+
302
309
doctest .testmod (verbose = True )
0 commit comments