# Each function defines a set of rules for the CA # If you want to add new functions the syntax is: # # def func(GRID, X,Y, MAXX, MAXY) # # GRID - a 2D Numeric array # X,Y - X,Y position of the current cell # MAXX,MAXY - Size of the grid in X & Y diections # # The function must return 1 if the current cell is to live # or 0 if it is to die # # An important point to note: Though the function will recieve the x # and y coordinates, you need to use them reversed. That is x is actually # y and y is actually x. from random import * def flipflop(grid, x,y, maxx, maxy): if grid[y][x] == 1: return 0 else: return 1 def rca(grid,x,y,maxx,maxy): x = random() if x < .5: return 1 else: return 0 def neighbor3(grid,x,y,maxx,maxy): # does'nt wrap around the grid c = 0 cl = [] cl.append( (x+1,y) ) cl.append( (x-1,y) ) cl.append( (x,y+1) ) cl.append( (x,y-1) ) for xx,yy in cl: if xx < 0 or xx >= maxx: continue if yy < 0 or yy >= maxy: continue if grid[yy][xx] == 1: c += 1 if c >= 3: return 0 else: return 1 def nb(grid,x,y,maxx,maxy): # does'nt wrap around the grid c = 0 cl = [] total = add(grid) cl.append( (x+1,y) ) cl.append( (x-1,y) ) cl.append( (x,y+1) ) cl.append( (x,y-1) ) for xx,yy in cl: if xx < 0 or xx >= maxx: continue if yy < 0 or yy >= maxy: continue if grid[yy][xx] == 1: c += 1 if c >= 1 and total < .9*maxx*maxy: return 1 else: return 0