restmill.blogg.se

Conway game of life python
Conway game of life python






conway game of life python conway game of life python

flat ): i = i + 1 color = colour_map if ( not ( sys. GetStdHandle ( c_ulong ( 0 xfffffff5 )) for i, row in enumerate ( state. matrix ( a ), 'body' def prettyprint ( state ): colours = # empty cells colour black if ( not ( sys. matrix ( a ), 'edge' else : # normal body yield ( r - 1, r + p, c - 1, c + q ), ( r, c ), ( 1, 1 ), np. matrix ( a ), 'corner' elif r = 0 and c != 0 : #top yield ( r, r + q, c - 1, c + q ), ( r, c ), ( 0, 1 ), np. arange ( col - q + 2 )): if r = 0 and c = 0 : #corner yield ( r, r + q, c, c + q ), ( r, c ), ( 0, 0 ), np. shape ( a ) #print row,col for ( r, c ) in (( r, c ) for r in np. elif grid = 0 and ( len ( countNumberOfAdjoiningCellsActive ) = 3 ): isAlive = True return isAlive def iterate_grid ( a, ( p, q )): ''' generate the next sub matrix from the original matrix yield the sub matrix back to be consumed Parameters - all parameters are keyword parameters a : parent matrix represents the larger parent matrix (p,q): tuple represents the dimensions of each sub matrix to generate Returns - yields the next sub matrix ''' row, col = np. elif grid = 1 and ( len ( countNumberOfAdjoiningCellsActive ) = 2 or len ( countNumberOfAdjoiningCellsActive ) = 3 ): isAlive = True #Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. if grid = 1 and ( len ( countNumberOfAdjoiningCellsActive ) 3 ) : # under or over populated then die out isAlive = False #Any live cell with two or three live neighbours lives on to the next generation. #Any live cell with more than three live neighbours dies, as if by overcrowding.

conway game of life python

append ( grid ) if verbose : print 'after row %d, col %d, value %d, count %d ' % ( row, col, grid, len ( countNumberOfAdjoiningCellsActive )) except : pass def isalive (( r, c ), ( lr, lc ), grid, location, verbose = False ): ''' apply rules against the r,c based on the local grid Parameters - all parameters are keyword parameters (r,c) : interger tuple represents the location of the cell in the global matrix (lr,lc) : integer tuple represents the location in the local grid matrix grid : matrix represents the neighbours to analyse location: string either corner, edge, top or body ''' countNumberOfAdjoiningCellsActive = check ( grid, lr, lc + 1, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr, lc - 1, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr + 1, lc, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr + 1, lc + 1, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr + 1, lc - 1, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr - 1, lc, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr - 1, lc + 1, countNumberOfAdjoiningCellsActive, verbose ) check ( grid, lr - 1, lc - 1, countNumberOfAdjoiningCellsActive, verbose ) if verbose : if len ( countNumberOfAdjoiningCellsActive ) != 0 : print 'value %d, countNumberOfAdjoiningCellsActive %d ' % ( grid, len ( countNumberOfAdjoiningCellsActive )) isAlive = False #Any live cell with fewer than two live neighbours dies, as if caused by under-population. ''' Created on 6 June 2012 bakera Each cell needs to be considered with neighbours 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 's_Game_of_Life ''' import numpy as np import time import sys from ctypes import * def check ( grid, row, col, countNumberOfAdjoiningCellsActive, verbose = False ): try : #if verbose: # print 'before row %d, col %d, value %d, count %d' % (row, col, grid, len(countNumberOfAdjoiningCellsActive)) if ( col >= 0 ) and ( row >= 0 ): # protect if grid != 0 : countNumberOfAdjoiningCellsActive.








Conway game of life python