You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

import rule
import time
class Field(object):
def __init__(self, shape = (32,32)):
self.resize(shape)
self.a_to_b = True
self.rule = rule.Rule(0)
self.updatedValues = [] # tuple (i,j, newValue)
def setRule(self, rule):
self.rule = rule
def resize(self, shape):
self.shape = shape
self.matrix_a = []
self.matrix_b = []
for i in range(shape[0]):
row_a = []
row_b = []
for j in range(shape[1]):
row_a.append(0)
row_b.append(0)
self.matrix_a.append(row_a)
self.matrix_b.append(row_b)
def getUpdatedValues(self):
return self.updatedValues
def update(self):
self.a_to_b = False if self.a_to_b else True
a = self.matrix_a if self.a_to_b else self.matrix_b
b = self.matrix_b if self.a_to_b else self.matrix_a
h, w = self.shape
for i in range(h):
for j in range(w):
#b[i,j] = self.rule.applyRule(a, i, j)
b[i][j] = self.rule.applyRule(a, self.shape, i, j)
if a[i][j] != b[i][j]:
self.updatedValues.append((i,j,b[i][j]))
def getField(self):
return self.matrix_b if self.a_to_b else self.matrix_a
def setValue(self, y, x, val):
self.matrix_a[y][x] = val
self.matrix_b[y][x] = val
# some testing
if __name__ == "__main__":
f = Field((160,90))
r = rule.Rule(2)
f.setRule(r)
f.setValue(2,2,1)
steps = 10
t_start = time.time()
for i in range(steps):
#print(f.getField())
f.update()
t_end = time.time()
print("average time per update: " + str((t_end - t_start) / steps) )