mirror of
https://github.com/brmlab/ledbar.git
synced 2025-06-09 05:14:03 +02:00
cellular.py: a simple 2D cellular automata implementation
This commit is contained in:
parent
ce20bce854
commit
04593730a4
1 changed files with 44 additions and 0 deletions
44
host_python/cellular.py
Normal file
44
host_python/cellular.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
|
||||
from ledbar import Ledbar
|
||||
|
||||
PIXELS = 20
|
||||
|
||||
rules = {(1,1,1): 0,
|
||||
(1,1,0): 0,
|
||||
(1,0,1): 0,
|
||||
(1,0,0): 1,
|
||||
(0,1,1): 1,
|
||||
(0,1,0): 1,
|
||||
(0,0,1): 1,
|
||||
(0,0,0): 0}
|
||||
|
||||
iteration = [0]*PIXELS
|
||||
iteration[PIXELS/2] = 1
|
||||
|
||||
def iterate(iteration):
|
||||
new = []
|
||||
for i in range(len(iteration)):
|
||||
if 0 < i < PIXELS-1:
|
||||
top = (iteration[i-1], iteration[i], iteration[i+1])
|
||||
new.append(rules[top])
|
||||
else:
|
||||
new.append(0)
|
||||
return new
|
||||
|
||||
def update(i):
|
||||
return (iteration[i], iteration[i], iteration[i])
|
||||
|
||||
l = Ledbar(PIXELS)
|
||||
work = True
|
||||
t = 0
|
||||
while work:
|
||||
for i in xrange(PIXELS):
|
||||
c = update(i)
|
||||
l.set_pixel(i, c[0], c[1], c[2])
|
||||
work = l.update()
|
||||
t += 1
|
||||
if not (t % 50):
|
||||
iteration = iterate(iteration)
|
Loading…
Add table
Add a link
Reference in a new issue