Over a million developers have joined DZone.

Wolfram-style Cellular Automata

DZone's Guide to

Wolfram-style Cellular Automata

Free Resource
Modified from Rick Muller's recipe

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)]

# 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 screenshot.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}