Permalink
Browse files

Center the alive cells on start

  • Loading branch information...
1 parent 4731a80 commit c4c0464ff74627e6db00152a82eb051cb32881de Lance Shelton committed with Jul 4, 2010
Showing with 49 additions and 0 deletions.
  1. +49 −0 life_game.py
View
@@ -42,6 +42,7 @@ def __init__(self, width, height, scale, seed_file=None):
self._init_graphics(width, height)
self._init_configuration(seed_file)
+ self._center_on_alive_cells()
self._prepare_generation()
self.advance_generation()
self._drawfield()
@@ -188,6 +189,54 @@ def advance_generation(self):
self._drawfield()
+ def _center_on_alive_cells(self):
+ """Center the table on the alive cells"""
+
+ # Initialize min and max to opposite extremes
+ x_min = self.xscale
+ x_max = 0
+ y_min = self.yscale
+ y_max = 0
+
+ # Find the bounds of the alive cells
+ for xx in range(self.xscale):
+ for yy in range(self.yscale):
+ if self.cells[xx][yy].alive_curr_gen:
+ if yy > y_max:
+ y_max = yy
+ if yy < y_min:
+ y_min = yy
+ if xx > x_max:
+ x_max = xx
+ if xx < x_min:
+ x_min = xx
+
+ # Shift right
+ while self.xscale - x_max > x_min + 1:
+ x_max = x_max + 1
+ x_min = x_min + 1
+ self.cells.insert(0, self.cells.pop())
+ else:
+ # Shift left
+ while x_min > self.xscale - x_max + 1:
+ x_min = x_min - 1
+ x_max = x_max - 1
+ self.cells.append(self.cells.pop(0))
+
+ # Shift up
+ while self.yscale - y_max > y_min + 1:
+ y_max = y_max + 1
+ y_min = y_min + 1
+ for xx in range(self.xscale):
+ self.cells[xx].insert(0, self.cells[xx].pop())
+ else:
+ # Shift down
+ while y_min > self.yscale - y_max + 1:
+ y_max = y_max - 1
+ y_min = y_min - 1
+ for xx in range(self.xscale):
+ self.cells[xx].append(self.cells[xx].pop(0))
+
def setup(filename):
"""Setup table to simulate game"""

0 comments on commit c4c0464

Please sign in to comment.