File: CellularAutomata.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (91 lines) | stat: -rw-r--r-- 2,387 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from SimPy.Simulation import *
"""CellularAutomata.py
Simulation of two-dimensional cellular automata. Plays game of Life.
"""

class Autom(Process):
    def __init__(self,coords):
        Process.__init__(self)
        self.x=coords[0]
        self.y=coords[1]
        self.state=False

    def nrActiveNeighbours(self,x,y):
        nr=0
        coords=[(xco+x,yco+y) for xco in (-1,0,1) for yco in (-1,0,1) if not (xco==0 and yco==0)]

        for a_coord in coords:
            try:
                if cells[a_coord].state: 
                    nr += 1
            except KeyError:
                ## wrap around
                nux=divmod(a_coord[0],size)[1]
                nuy=divmod(a_coord[1],size)[1]
                if cells[(nux,nuy)].state: nr += 1
        return nr

    def decide(self,nrActive):
        return  (self.state and (nrActive == 2 or nrActive == 3) or (nrActive==3))
                
    def celllife(self):
        while True:
            #calculate next state
            temp=self.decide(self.nrActiveNeighbours(self.x,self.y))
            yield hold,self,0.5
            #set next state
            self.state=temp
            yield hold,self,0.5

class Show(Process):
    def __init__(self):
        Process.__init__(self)

    def picture(self):
        while True:
            print "Generation %s" %now()
            for i in range(size):
                for j in range(size):
                    if cells[(i,j)].state:
                        print "*",
                    else:
                        print ".",
                print
            print
            yield hold,self,1

size=20
cells={}
initialize()
for i in range(size):
    for j in range(size):
        a=cells[(i,j)]=Autom((i,j))
        activate(a,a.celllife())

##R-pentomino
cells[(9,3)].state=True
cells[(10,3)].state=True
cells[(9,4)].state=True
cells[(8,4)].state=True
cells[(9,5)].state=True

cells[(5,5)].state=True
cells[(5,6)].state=True
cells[(4,5)].state=True
cells[(4,6)].state=True
cells[(4,7)].state=True
cells[(10,10)].state=True
cells[(10,11)].state=True
cells[(10,12)].state=True
cells[(10,13)].state=True
cells[(11,10)].state=True
cells[(11,11)].state=True
cells[(11,12)].state=True
cells[(11,13)].state=True

print 'CellularAutomata'
s=Show()
whenToStartShowing=10
activate(s,s.picture(),delay=whenToStartShowing)
nrGenerations=30
simulate(until=nrGenerations)