summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrianna Rainey2023-02-17 15:16:57 -0500
committerBrianna Rainey2023-02-17 15:16:57 -0500
commit89de020fd56bf38a4f4809710f1777504bf5f611 (patch)
treee62ea3676fdac4986521b433776de70a72b89630
parent9cf8815aee0601fae67d11cca54d83121f7675f8 (diff)
Add comments to Life component
added comments to preFrameRender and gridForTick methods
-rw-r--r--src/components/life.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/components/life.py b/src/components/life.py
index d0bb708..3a5276f 100644
--- a/src/components/life.py
+++ b/src/components/life.py
@@ -117,6 +117,10 @@ class Component(Component):
117 return self.drawGrid(self.startingGrid) 117 return self.drawGrid(self.startingGrid)
118 118
119 def preFrameRender(self, *args, **kwargs): 119 def preFrameRender(self, *args, **kwargs):
120 '''
121 Evolution must be computed before frameRender begins
122 because frameRender may be called non-sequentially.
123 '''
120 super().preFrameRender(*args, **kwargs) 124 super().preFrameRender(*args, **kwargs)
121 self.progressBarSetText.emit("Computing evolution...") 125 self.progressBarSetText.emit("Computing evolution...")
122 self.tickGrids = {0: self.startingGrid} 126 self.tickGrids = {0: self.startingGrid}
@@ -351,7 +355,11 @@ class Component(Component):
351 return frame 355 return frame
352 356
353 def gridForTick(self, tick): 357 def gridForTick(self, tick):
354 '''Given a tick number over 0, returns a new grid set of tuples''' 358 '''
359 Given a tick number over 0, returns a new grid (a set of tuples).
360 This depends on the previous tick's grid already being computed,
361 so it must be called sequentially.
362 '''
355 lastGrid = self.tickGrids[tick - 1] 363 lastGrid = self.tickGrids[tick - 1]
356 364
357 def neighbours(x, y): 365 def neighbours(x, y):
@@ -361,16 +369,21 @@ class Component(Component):
361 } 369 }
362 370
363 newGrid = set() 371 newGrid = set()
372 # Copy cells from the previous grid if they have 2 or 3 neighbouring cells
364 for x, y in lastGrid: 373 for x, y in lastGrid:
365 surrounding = len(neighbours(x, y)) 374 surrounding = len(neighbours(x, y))
366 if surrounding == 2 or surrounding == 3: 375 if surrounding == 2 or surrounding == 3:
367 newGrid.add((x, y)) 376 newGrid.add((x, y))
377
378 # Find positions around living cells which must be checked for reproduction
368 potentialNewCells = { 379 potentialNewCells = {
369 coordTup for origin in lastGrid 380 coordTup for origin in lastGrid
370 for coordTup in list(self.nearbyCoords(*origin)) 381 for coordTup in list(self.nearbyCoords(*origin))
371 } 382 }
383 # Check for reproduction
372 for x, y in potentialNewCells: 384 for x, y in potentialNewCells:
373 if (x, y) in newGrid: 385 if (x, y) in newGrid:
386 # Ignore non-empty cell
374 continue 387 continue
375 surrounding = len(neighbours(x, y)) 388 surrounding = len(neighbours(x, y))
376 if surrounding == 3: 389 if surrounding == 3: