DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Wolfram-style Cellular Automata

09.14.2005
| 2063 views |
  • submit to reddit
        Modified from Rick Muller's <a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/343386>recipe</a>
from appuifw import *
import e32, random

app.body = c = Canvas()
w, h =  c.size

rule = [(22/pow(2,i)) % 2 for i in range(8)]  # rule 22
first_row = [0] * w
first_row[w/2] = 1   # start with one point
# first_row = [random.randint(0,1) for i in range(w)]   # random start
rows = [first_row]

# iterate more rows
for i in range(h-1):
    d = rows[-1]  # previous row data
    new = [rule[ 4*d[(j-1)%w] +2*d[j] +d[(j+1)%w]] for j in range(w)]
    rows.append(new)

# render
for y in range(h):
    for x in range(w):
        if rows[y][x]: c.point((x,y), 0)

e32.ao_sleep(5)    # wait 5 sec then quit
See the <a href=http://www.flickr.com/photos/korakot/43300497/>screenshot</a>.