Skip to content

Code from April Meeting

by on June 6, 2012

Game of Life Minimal Python Implementation to use as a demonstration of displaying a two dimensional object in various graphic frameworks


'''
Created on Apr 5, 2012

@author: admin
'''

from itertools import repeat

class Grid(object):
    '''
    classdocs
    '''


    def __init__(self,  height, width ):
        '''
        Constructor
        '''
        self.width = width
        self.height = height
        self.rows = [ list( repeat(0,width) ) for dummy in range(height) ]
        
    def __str__(self):
        """
        Show the grid as text
        """
        output = []
        for row in self.rows:
            line = ''.join( '*' if cell else '-' for cell in row )
            output.append( line )
            
        return '\n'.join( output )+'\n'
    
    def __str1__(self):
        """
        Another way to do it with somewhat more readable code
        """
        output = ''
        for rowIndex in range(self.height):
            for columnIndex in range(self.width):
                output += str( self.rows[rowIndex][columnIndex] )
            output += '\n'
        return output.replace('0', '-').replace('1','*')
    
    def nextGeneration(self):
        nextGrid = Grid( height = self.height, width = self.width )
        for rowIndex in range(self.height):
            for columnIndex in range(self.width):
                nextGrid.rows[rowIndex][columnIndex] = self.rule( row = rowIndex, column = columnIndex)
                
        return nextGrid
    
    def rule(self, row, column ):
        nLiveNeighbors = 0
        for r in range( max(0,row-1), min(self.height, row+2) ):
            for c in range( max(0, column-1 ), min( self.width, column+2) ):
                nLiveNeighbors += self.rows[r][c]
                
        nLiveNeighbors -= self.rows[row][column]     
        
        amAlive = self.rows[row][column]
        if amAlive:
            if nLiveNeighbors == 2 or nLiveNeighbors == 3:
                return 1
            else:
                return 0
        else:
            if nLiveNeighbors == 3:
                return 1
            else:
                return 0
                   
        
                
    
    def __eq__( self, other ):
        return ( str( self ) == str( other ) )
        

And here are the unit tests.

'''
 Created on Apr 5, 2012

@author: admin
 '''
 import unittest

from grid import Grid

class GridTest(unittest.TestCase):

def testCreateGrid(self):
 grid = Grid( width = 17, height = 19 )
 self.assertEqual( 17, grid.width )
 self.assertEqual( 19, grid.height )

def testGridExtent(self):
 grid = Grid( width = 17, height = 19 )
 self.assertEquals( 19, len(grid.rows ) )
 self.assertEquals( 17, len( grid.rows[0] ) )
 self.assertEquals( 0, grid.rows[0][0] )
 self.assertEquals( 0, grid.rows[19-1][17-1] )

def testDisplayStr(self):
 grid = Grid( height = 3, width = 5 )
 grid.rows[1][1] = 1
 grid.rows[2][4] = 1
 print grid

def testDisplayStr1(self):
 grid = Grid( height = 3, width = 5 )
 grid.rows[1][1] = 1
 grid.rows[2][4] = 1
 self.assertEquals( grid.__str__(), grid.__str1__( )  )

def testRule(self):
 grid = Grid( height = 3, width = 3 )
 nextGrid = grid.nextGeneration()
 self.assertEquals( grid, nextGrid )

def testDieWhenOneNeighbor(self):
 grid = Grid( height = 3, width = 3 )
 grid.rows[1][1] = 1
 nextGrid = grid.nextGeneration()
 self.assertEqual( 0, nextGrid.rows[1][1] )

def testLiveWhenTwoNeighbors(self):
 grid = Grid( height = 3, width = 3 )
 grid.rows[1][0] = 1
 grid.rows[1][1] = 1
 grid.rows[1][2] = 1

nextGrid = grid.nextGeneration()
 self.assertEqual( 0, nextGrid.rows[1][0] )
 self.assertEqual( 1, nextGrid.rows[1][1] )
 self.assertEqual( 0, nextGrid.rows[1][2] )
 print 'testLiveWhenTwoNeighbors'
 print grid
 print
 print nextGrid

def testFlasher(self):
 grid = Grid( height = 3, width = 3 )
 grid.rows[1][0] = 1
 grid.rows[1][1] = 1
 grid.rows[1][2] = 1

 grid2 = grid.nextGeneration()
 grid3 = grid2.nextGeneration()

print 'testFlasher'
 print grid
 print
 print grid2
 print
 print grid3

if __name__ == "__main__":
 #import sys;sys.argv = ['', 'Test.testName']
 unittest.main()
Advertisements

From → Code

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: