2020-05-03_john-conways-life.html
001|
002|
003|
004|
005| CellSize = (4, 4)
006|
007| LiveColor = (0, 0, 0)
008| DeadColor = (0xcf,0xdf,0xff)
009|
010| NC = 1280//CellSize[0]
011| NR = 720//CellSize[1]
012|
013| Buf1 = []
014| Buf2 = []
015|
016| def initialize_buffer (percentage=0.5):
017| global Buf1, Buf2
018|
019| Buf1 = [[0 for c in range(NC)] for r in range(NR)]
020| Buf2 = [[0 for c in range(NC)] for r in range(NR)]
021|
022| for r in range(NR):
023| for c in range(NC):
024| Buf1[r][c] = (1 if random() < percentage else 0)
025|
026|
027| def count_neighbors (row, col):
028| total = 0
029|
030|
031| if 0 < row:
032| total += (Buf1[row-1][col-1] if 0 < col else 0)
033| total += (Buf1[row-1][col])
034| total += (Buf1[row-1][col+1] if col < (NC-1) else 0)
035|
036|
037| total += (Buf1[row][col-1] if 0 < col else 0)
038| total += (Buf1[row][col+1] if col < (NC-1) else 0)
039|
040|
041| if row < (NR-1):
042| total += (Buf1[row+1][col-1] if 0 < col else 0)
043| total += (Buf1[row+1][col])
044| total += (Buf1[row+1][col+1] if col < (NC-1) else 0)
045| Buf2[row][col] = total
046|
047| return total
048|
049|
050| def apply_rule (row, col):
051| m = Buf1[row][col]
052| n = Buf2[row][col]
053|
054| if 0 < m:
055| return (1 if (n in [2,3]) else 0)
056|
057| return (1 if n == 3 else 0)
058|
059|
060| def life_cycle ():
061|
062|
063| for r in range(NR):
064| for c in range(NC):
065| count_neighbors(r,c)
066|
067|
068| for r in range(NR):
069| for c in range(NC):
070| Buf1[r][c] = apply_rule(r,c)
071|
072|
073| def do_life (cycles=20, percentage=0.5):
074| initialize_buffer(percentage)
075|
076| for n in range(cycles):
077| life_cycle()
078| fn = 'life-%05d.png' % n
079| create_image_file(fname=fn, fpath=ImgsPath)
080|
081|
082| def create_image_file (fname, fpath=ImgsPath, mode='PNG'):
083| filename = path.join(fpath, fname)
084| dims = (NC*CellSize[0], NR*CellSize[1])
085|
086| im = Image.new('RGB', dims, (255,255,255))
087| draw = ImageDraw.Draw(im)
088|
089| for y in range(NR):
090| for x in range(NC):
091| v = Buf1[y][x]
092| cc = LiveColor if 0<v else DeadColor
093| x0 = x * CellSize[0]
094| y0 = y * CellSize[1]
095| for yy in range(CellSize[1]):
096| for xx in range(CellSize[0]):
097| draw.point((x0+xx,y0+yy), fill=cc)
098|
099| im.save(filename, mode)
100|
101|
102| def apply_rule (row, col):
103| m = Buf1[row][col]
104| n = Buf2[row][col]
105|
106| if 0 < m:
107| v = (1 if (n in [2,3]) else 0)
108| else:
109| v = (1 if n == 3 else 0)
110| Buf1[r][c] = v
111|
112| return v
113|
114|
115| def life_cycle ():
116|
117|
118| for r in range(NR):
119| for c in range(NC):
120| count_neighbors(r,c)
121|
122|
123| for r in range(NR):
124| for c in range(NC):
125| apply_rule(r,c)
126|
127|
128| def count_neighbors (row, col):
129| total = 0
130| left = (Cols-1) if col == 0 else (col-1)
131| right = (col+1) if col < (Cols-1) else 0
132| top = (Rows-1) if row == 0 else (row-1)
133| bot = (row+1) if row < (Rows-1) else 0
134|
135|
136| total += Buf1[top][left]
137| total += Buf1[top][col]
138| total += Buf1[top][right]
139|
140|
141| total += Buf1[row][left]
142| total += Buf1[row][right]
143|
144|
145| total += Buf1[bot][left]
146| total += Buf1[bot][col]
147| total += Buf1[bot][right]
148| Buffer2[row][col] = total
149|
150| return total
151|
152|
153|