-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathgameOfLife.pyde
More file actions
82 lines (66 loc) · 1.99 KB
/
Copy pathgameOfLife.pyde
File metadata and controls
82 lines (66 loc) · 1.99 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# cellularAutomata.pyde
from random import choice
GRID_W = 101
GRID_H = 101
generation = 0
class Cell:
def __init__(self,r,c,on=0):
self.c = c
self.r = r
self.on = on
def display(self):
if self.on == 1:
fill(0) #black
else:
fill(255) #white
rect(SZ*self.r, SZ*self.c, SZ, SZ)
def checkNeighbors(self):
neighbs = 0 #check the neighbors
for dr,dc in [[-1,-1],[-1,0],[-1,1], [1,0], [1,-1],[1,1],[0,-1],[0,1]]:
try:
if cellList[self.r + dr][self.c + dc].on == 1:
neighbs += 1
except IndexError:
continue
if self.on == 1:
if neighbs in [2,3]:
return 1
return 0
if neighbs == 3:
return 1
return 0
def setup():
global SZ, cellList
noStroke()
size(800,800)
SZ = width // GRID_W + 1
cellList = createCellList()
def draw():
global generation,cellList
frameRate(10)
cellList = update(cellList)
for row in cellList:
for cell in row:
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 '''
newList=[]#empty list for cells
#populate the initial cell list
for j in range(GRID_H):
newList.append([]) #add empty row
for i in range(GRID_W):
newList [j].append(Cell(i,j,choice([0,1]))) #add off Cells or zeroes
return newList