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.

68 lines
1.7 KiB

  1. import rule
  2. import time
  3. class Field(object):
  4. def __init__(self, shape = (32,32)):
  5. self.resize(shape)
  6. self.a_to_b = True
  7. self.rule = rule.Rule(0)
  8. self.updatedValues = [] # tuple (i,j, newValue)
  9. def setRule(self, rule):
  10. self.rule = rule
  11. def resize(self, shape):
  12. self.shape = shape
  13. self.matrix_a = []
  14. self.matrix_b = []
  15. for i in range(shape[0]):
  16. row_a = []
  17. row_b = []
  18. for j in range(shape[1]):
  19. row_a.append(0)
  20. row_b.append(0)
  21. self.matrix_a.append(row_a)
  22. self.matrix_b.append(row_b)
  23. def getUpdatedValues(self):
  24. return self.updatedValues
  25. def update(self):
  26. self.a_to_b = False if self.a_to_b else True
  27. a = self.matrix_a if self.a_to_b else self.matrix_b
  28. b = self.matrix_b if self.a_to_b else self.matrix_a
  29. h, w = self.shape
  30. for i in range(h):
  31. for j in range(w):
  32. #b[i,j] = self.rule.applyRule(a, i, j)
  33. b[i][j] = self.rule.applyRule(a, self.shape, i, j)
  34. if a[i][j] != b[i][j]:
  35. self.updatedValues.append((i,j,b[i][j]))
  36. def getField(self):
  37. return self.matrix_b if self.a_to_b else self.matrix_a
  38. def setValue(self, y, x, val):
  39. self.matrix_a[y][x] = val
  40. self.matrix_b[y][x] = val
  41. # some testing
  42. if __name__ == "__main__":
  43. f = Field((160,90))
  44. r = rule.Rule(2)
  45. f.setRule(r)
  46. f.setValue(2,2,1)
  47. steps = 10
  48. t_start = time.time()
  49. for i in range(steps):
  50. #print(f.getField())
  51. f.update()
  52. t_end = time.time()
  53. print("average time per update: " + str((t_end - t_start) / steps) )