annotate ch7ex2.py @ 48:98eb01502cf5 tip

Follow up to last commit. Re-orient the r-pentomino. Added progress display.
author Brian Neal <bgneal@gmail.com>
date Wed, 31 Jul 2013 20:37:12 -0500
parents 2b0e229e163c
children
rev   line source
bgneal@47 1 """Chapter 7, exercise 2 in Allen Downey's Think Complexity book.
bgneal@47 2
bgneal@47 3 Start with an r-pentomino as an initial condition and confirm that the results
bgneal@47 4 are consistent with the description above. You might have to adjust the size of
bgneal@47 5 the grid and the boundary behavior.
bgneal@47 6
bgneal@47 7 """
bgneal@47 8 import Life
bgneal@47 9
bgneal@47 10
bgneal@47 11 class RPentomino(Life.Life):
bgneal@47 12
bgneal@47 13 def __init__(self, n):
bgneal@48 14 super(RPentomino, self).__init__(n, mode='constant', cval=0,
bgneal@48 15 random=False, show_progress=10)
bgneal@47 16
bgneal@47 17 i, j = n / 2, n / 2
bgneal@47 18 self.array[i, j] = 1
bgneal@47 19 self.array[i, j - 1] = 1
bgneal@48 20 self.array[i + 1, j] = 1
bgneal@48 21 self.array[i + 1, j + 1] = 1
bgneal@47 22 self.array[i - 1, j] = 1
bgneal@48 23
bgneal@47 24
bgneal@47 25 if __name__ == '__main__':
bgneal@47 26 life = RPentomino(150)
bgneal@47 27 viewer = Life.LifeViewer(life, delay=1)
bgneal@47 28 viewer.animate(steps=1200)