Skip to content

Commit

Permalink
simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
hackingmath authored Apr 17, 2018
1 parent c23ee10 commit f0ced78
Showing 1 changed file with 47 additions and 74 deletions.
121 changes: 47 additions & 74 deletions cellularAutomata.pyde
Original file line number Diff line number Diff line change
@@ -1,105 +1,78 @@
# cellularAutomata.pyde

from random import choice

width_of_grid = 41
height_of_grid = 41
GRID_W = 41
GRID_H = 41

#size of cell
sz = 600//width_of_grid + 1
generation = 0

class Cell:
def __init__(self,c,r,on=0):
self.c = c
self.r = r
self.on = on

def checkNeighbors(self):
neighbs = 0
#check the neighbor above
if self.r > 0:
if cellList[self.r-1][self.c].on == 1:
neighbs += 1

#check the neighbor below
if self.r < height_of_grid - 1:
if cellList[self.r+1][self.c].on == 1:
neighbs += 1

#check the neighbor to the left
if self.c > 0:
if cellList[self.r][self.c-1].on == 1:
neighbs += 1
def display(self):
if self.on == 1:
fill(0) #black
else:
fill(255) #white
rect(SZ*self.c, SZ*self.r, SZ, SZ)

#check the neighbor to the right
if self.c < width_of_grid - 1:
if cellList[self.r][self.c+1].on == 1:
neighbs += 1

if neighbs in [1]:
def checkNeighbors(self):
neighbs = 0 #check the neighbors
if self.on == 1: return 1
for dr,dc in [[-1,0], [1,0], [0,-1],[0,1]]:
try:
if cellList[self.r + dr][self.c + dc].on == 1:
neighbs += 1
except IndexError:
continue
if neighbs in [1,4]:
return 1
else:
return 0


def update(self):
if self.on == 1:
fill(0)
else:
fill(255)
rect(sz*self.c,sz*self.r,sz,sz)


# cellList = [] #empty list for cells
# for i in range(width_of_grid): #columns
# for j in range(height_of_grid): #rows
# cellList.append(Cell(i,j,choice([0,1])))


def setup():
global cellList
size(600,600)
global SZ, cellList
noStroke()
size(600,600)
SZ = width // GRID_W + 1
cellList = createCellList()

def draw():
global cellList,level
global generation,cellList
frameRate(10)

newList = [] #create a new list for the next gen
for r,row in enumerate(cellList):
newList.append([]) #add empty row
for c,cell in enumerate(row):
if cell.on == 0: #if cell is off
#check neighbs and append new value
newList[r].append(Cell(c,r,cell.checkNeighbors()))
else: #on cells stay on
newList[r].append(Cell(c,r,1))
cellList = newList[::]
level += 1
for row in cellList:
for cell in row:
cell.update()
if level == 24:
cellList = createCellList()
cellList = update(cellList)
for row in cellList:
for cell in row:
cell.update()
if level == 24:
cell.display()
generation += 1
if generation == 30:
generation = 1
cellList = createCellList()

loop()

def update(cellList):
newList = []
for r,row in enumerate(cellList):
newList.append([])
for c,cell in enumerate(row):
newList[r].append(Cell(r,c,cell.checkNeighbors()))
return newList[::]


def createCellList():
'''Creates a big list of off cells with
one on Cell in the center'''
global cellList, level
cellList=[]#empty list for cells

one on Cell in the center '''
newList=[]#empty list for cells
#populate the initial cell list
for j in range(height_of_grid):
cellList.append([]) #add empty row
for i in range(width_of_grid):
cellList[j].append(Cell(i,j,0)) #add off Cells or zeroes
for j in range(GRID_H):
newList.append([]) #add empty row
for i in range(GRID_W):
newList [j].append(Cell(i,j,0)) #add off Cells or zeroes
#center cell is set to on
cellList[height_of_grid//2][width_of_grid//2].on = 1
level = 0
return cellList
newList [GRID_H//2][GRID_W//2].on = 1
return newList

0 comments on commit f0ced78

Please sign in to comment.