
Wie auch immer, hier meine zusammengeschusterte 3d Version (wirklich 3d

Code: Alles auswählen
from visual import *
import time
class GOL:
def __init__(self, width, height, depth, living_cells):
self.width = width
self.height = height
self.depth = depth
self.living_cells = living_cells
def is_alive( self, pos ):
return pos in self.living_cells
def is_empty( self, pos ):
return not self.is_alive(pos)
def neighbours( self, (x, y, z) ):
return [ self.wrap( (a, b, c) ) for (a,b,c) in
[
(x-1, y-1, z-1), (x, y-1, z-1), (x+1, y-1, z-1),
(x-1, y, z-1), (x, y, z-1), (x+1, y, z-1),
(x-1, y+1, z-1), (x, y+1, z-1), (x+1, y+1, z-1),
(x-1, y-1, z ), (x, y-1, z ), (x+1, y-1, z ),
(x-1, y, z ), (x+1, y, z ),
(x-1, y+1, z ), (x, y+1, z ), (x+1, y+1, z ),
(x-1, y-1, z+1), (x, y-1, z+1), (x+1, y-1, z+1),
(x-1, y, z+1), (x, y, z+1), (x+1, y, z+1),
(x-1, y+1, z+1), (x, y+1, z+1), (x+1, y+1, z+1),
]
]
def wrap( self, (x, y, z) ):
return ( ( (x-1) % self.width ) + 1,
( (y-1) % self.height ) + 1,
( (z-1) % self.depth ) + 1 )
def living_neighbours( self, (x, y, z) ):
return len([ pos for pos in self.neighbours((x,y,z))
if self.is_alive(pos) ] )
def survivors( self ):
return [ pos for pos in self.living_cells if
self.living_neighbours( pos ) in [4,5,6] ]
def births( self ):
all_living_neighbours = []
for pos in self.living_cells:
for n in self.neighbours( pos ):
if self.is_empty(n) and self.living_neighbours(n) == 4:
all_living_neighbours.append(n)
return set(all_living_neighbours)
def next_gen( self ):
s = self.survivors()
b = self.births()
self.living_cells = []
self.living_cells.extend(s)
self.living_cells.extend(b)
if __name__ == '__main__':
def show(b):
for pos in b:
box(pos=pos)
def run(g):
i = 0
while 1:
i += 1
print "generation: %s living: %s" % (i, len(g.living_cells))
for o in scene.objects:
if o.__class__ != curve:
o.visible=0
del(o)
show(g.living_cells)
time.sleep(0.1)
g.next_gen()
def draw_box(l, rad=1):
for p in [ [ (0,0,0), (l,0,0), (l,l,0), (0,l,0), (0,0,0) ],
[ (0,0,l), (l,0,l), (l,l,l), (0,l,l), (0,0,l) ],
[ (0,0,0), (0,0,l) ],
[ (l,0,0), (l,0,l) ],
[ (l,l,0), (l,l,l) ],
[ (0,l,0), (0,l,l) ] ]:
curve(pos=p, radius=rad)
scene.autocenter = 1
scene.autozoom = 1
draw_box(100, rad=0.5)
g = GOL(100, 100, 100, [(50,48,50),(48,49,50),(50,49,50),(49,50,50),(50,50,50)])
run(g)
Die Startkonfiguration ist ein 2d Glider. Wird nach kurzer Zeit seeeeehr langsam.