Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Wolfram-style cellular automata

Modified from Rick Muller's recipe
   1  
   2  from appuifw import *
   3  import e32, random
   4  
   5  app.body = c = Canvas()
   6  w, h =  c.size
   7  
   8  rule = [(22/pow(2,i)) % 2 for i in range(8)]  # rule 22
   9  first_row = [0] * w
  10  first_row[w/2] = 1   # start with one point
  11  # first_row = [random.randint(0,1) for i in range(w)]   # random start
  12  rows = [first_row]
  13  
  14  # iterate more rows
  15  for i in range(h-1):
  16      d = rows[-1]  # previous row data
  17      new = [rule[ 4*d[(j-1)%w] +2*d[j] +d[(j+1)%w]] for j in range(w)]
  18      rows.append(new)
  19  
  20  # render
  21  for y in range(h):
  22      for x in range(w):
  23          if rows[y][x]: c.point((x,y), 0)
  24  
  25  e32.ao_sleep(5)    # wait 5 sec then quit

See the screenshot.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS