annotate ch7ex2.py @ 47:2b0e229e163c

Chapter 7, exercise 2. R-Pentomino game of life.
author Brian Neal <bgneal@gmail.com>
date Thu, 25 Jul 2013 21:53:44 -0500
parents
children 98eb01502cf5
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@47 14 super(RPentomino, self).__init__(n, mode='constant', cval=0, random=False)
bgneal@47 15
bgneal@47 16 i, j = n / 2, n / 2
bgneal@47 17 self.array[i, j] = 1
bgneal@47 18 self.array[i, j - 1] = 1
bgneal@47 19 self.array[i, j + 1] = 1
bgneal@47 20 self.array[i - 1, j] = 1
bgneal@47 21 self.array[i + 1, j - 1] = 1
bgneal@47 22
bgneal@47 23 if __name__ == '__main__':
bgneal@47 24 life = RPentomino(150)
bgneal@47 25 viewer = Life.LifeViewer(life, delay=1)
bgneal@47 26 viewer.animate(steps=1200)