forked from rizzleroc/CellAutomata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellular_automaton.py
More file actions
39 lines (39 loc) · 1.75 KB
/
cellular_automaton.py
File metadata and controls
39 lines (39 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'''
This file defines the CellularAutomaton class, which manages the grid of cells and applies the rules of natural selection.
'''
from cell import Cell
class CellularAutomaton:
def __init__(self, width, height):
self.width = width
self.height = height
self.grid = [[Cell() for _ in range(width)] for _ in range(height)]
def step(self):
# Apply the rules of natural selection to the grid
# First, mark all cells as new
for row in self.grid:
for cell in row:
cell.is_new = True
# Change the color of all cells
for y in range(self.height):
for x in range(self.width):
self.grid[y][x].change_color()
# Then, try to combine cells with their neighbors
for y in range(self.height):
for x in range(self.width):
current_cell = self.grid[y][x]
# Check right neighbor
if x < self.width - 1:
self.try_combine(current_cell, self.grid[y][x + 1])
# Check bottom neighbor
if y < self.height - 1:
self.try_combine(current_cell, self.grid[y + 1][x])
# Check bottom-right neighbor
if x < self.width - 1 and y < self.height - 1:
self.try_combine(current_cell, self.grid[y + 1][x + 1])
# Check bottom-left neighbor
if x > 0 and y < self.height - 1:
self.try_combine(current_cell, self.grid[y + 1][x - 1])
def try_combine(self, cell1, cell2):
# Helper function to try to combine two adjacent cells of the same color
if cell1.color == cell2.color:
cell1.combine(cell2)