Files
jython-cgol/cgol.py
T
2025-07-17 10:38:57 +02:00

41 lines
1.5 KiB
Python

from gamegrid import *
from cell import Cell
class CGOL(Actor):
def __init__(self, game_list): # Pass game_list as an argument here
self.game_list = game_list
def calculate_next_gen(self, cells):
next_gen = []
for y in range(len(cells)):
next_gen.append([])
for x in range(len(cells[y])):
check_cell = cells[y][x]
alive_neighbors = 0
for ry in range(y-1, y+2, 1):
for rx in range(x-1, x+2, 1):
if ry < 0 or rx < 0 or ry == y and rx == x or rx > len(cells[y])-1 or ry > len(cells)-1:
pass
else:
alive_neighbors += cells[ry][rx]
if alive_neighbors > 3 or alive_neighbors < 2:
next_gen[y].append(0)
elif alive_neighbors == 3:
next_gen[y].append(1)
else:
next_gen[y].append(cells[y][x])
return next_gen
def act(self):
self.game_list = calculate_next_gen(self.game_list)
for y in range(0,20):
for x in range(0,20):
cell = getOneActorAt(Location(x,y), Cell)
if cell != None:
removeActor(cell)
for y in range(0,20):
for x in range(0,20):
if self.game_list[y][x] == 1:
addActor(Cell(),Location(x,y))