Over a million developers have joined DZone.

Wolfram-style Cellular Automata

·
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)]
    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 screenshot.
Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}