# Simple CA simulator in Python
#
# *** Game of Life Rule ***
#
# Copyright 2008-2012 Hiroki Sayama
# sayama@binghamton.edu
#
# adapted for the Pocket project by Toshihiro Tanizawa
# on 2018-07-15(Sun) 16:49

# import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.cm
import matplotlib.animation as animation

import matplotlib.pyplot as PL
import random as RD
import scipy as SP

# === initial setting ==============================================
RD.seed()
width = 60
height = 60
initProb = 0.2

# ==================================================================
# set up the figure, the axis, and the plot element
fig = PL.figure()
ax  = fig.add_subplot(111, aspect='equal', autoscale_on=False,
                      xlim=(0, width), ylim=(0, height))
ax.grid(False)
ax.set_axis_off()
line, = ax.plot([], [], 'o-')

# === init() ======================================================
def init():
    global config, nextConfig

    config = SP.zeros([height, width])
    for x in range(width):
        for y in range(height):
            if RD.random() < initProb:
                state = 1
            else:
                state = 0
            config[y, x] = state

    ax.cla()
    ax.pcolor(config, cmap = matplotlib.cm.Greens)
    ax.set_title('CA: Game of Life (t = 0)')
    line.set_data([], [])

    nextConfig = SP.zeros([height, width])

    return line,
# =======================================================================

# === animate() =========================================================
def animate(i):
    global config, nextConfig

    for x in range(width):
        for y in range(height):
            state = config[y, x]
            numberOfAlive = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    numberOfAlive += config[(y+dy)%height, (x+dx)%width]
            if state == 0 and numberOfAlive == 3:
                state = 1
            elif state == 1 and (numberOfAlive < 3 or numberOfAlive > 4):
                state = 0
            nextConfig[y, x] = state

    config, nextConfig = nextConfig, config

    ax.set_axis_off()
    ax.set_title('CA: Game of Life (t = ' + str(i) + ')')
    ax.pcolor(config, cmap = matplotlib.cm.Greens)
    line.set_data([], [])

    return line,
# ======================================================================

# import pycxsimulator
# pycxsimulator.GUI().start(func=[init,draw,step])

# === rendering part ===================================================

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, blit=True)

# save the animation as an mp4. This requires ffmpeg or mencoder to be installed.
# The extra_args ensure that the x264 codec is used,
# so that the video can be embedded in html5. You may need to adjust this
# for your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
# anim.save('ca_GoL.mp4', extra_args=['-vcodec', 'libx264'])

# =====================================================================


if __name__ == '__main__':
    PL.show()

# <<< End >>>

d64e82cd33085676244b03b9e1ca256c1171fb27
<mask token>


def animate(i):
    global config, nextConfig
    for x in range(width):
        for y in range(height):
            state = config[y, x]
            numberOfAlive = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    numberOfAlive += config[(y + dy) % height, (x + dx) % width
                        ]
            if state == 0 and numberOfAlive == 3:
                state = 1
            elif state == 1 and (numberOfAlive < 3 or numberOfAlive > 4):
                state = 0
            nextConfig[y, x] = state
    config, nextConfig = nextConfig, config
    ax.set_axis_off()
    ax.set_title('CA: Game of Life (t = ' + str(i) + ')')
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    line.set_data([], [])
    return line,


<mask token>

<mask token>


def init():
    global config, nextConfig
    config = SP.zeros([height, width])
    for x in range(width):
        for y in range(height):
            if RD.random() < initProb:
                state = 1
            else:
                state = 0
            config[y, x] = state
    ax.cla()
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    ax.set_title('CA: Game of Life (t = 0)')
    line.set_data([], [])
    nextConfig = SP.zeros([height, width])
    return line,


def animate(i):
    global config, nextConfig
    for x in range(width):
        for y in range(height):
            state = config[y, x]
            numberOfAlive = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    numberOfAlive += config[(y + dy) % height, (x + dx) % width
                        ]
            if state == 0 and numberOfAlive == 3:
                state = 1
            elif state == 1 and (numberOfAlive < 3 or numberOfAlive > 4):
                state = 0
            nextConfig[y, x] = state
    config, nextConfig = nextConfig, config
    ax.set_axis_off()
    ax.set_title('CA: Game of Life (t = ' + str(i) + ')')
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    line.set_data([], [])
    return line,


<mask token>

<mask token>
RD.seed()
<mask token>
ax.grid(False)
ax.set_axis_off()
<mask token>


def init():
    global config, nextConfig
    config = SP.zeros([height, width])
    for x in range(width):
        for y in range(height):
            if RD.random() < initProb:
                state = 1
            else:
                state = 0
            config[y, x] = state
    ax.cla()
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    ax.set_title('CA: Game of Life (t = 0)')
    line.set_data([], [])
    nextConfig = SP.zeros([height, width])
    return line,


def animate(i):
    global config, nextConfig
    for x in range(width):
        for y in range(height):
            state = config[y, x]
            numberOfAlive = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    numberOfAlive += config[(y + dy) % height, (x + dx) % width
                        ]
            if state == 0 and numberOfAlive == 3:
                state = 1
            elif state == 1 and (numberOfAlive < 3 or numberOfAlive > 4):
                state = 0
            nextConfig[y, x] = state
    config, nextConfig = nextConfig, config
    ax.set_axis_off()
    ax.set_title('CA: Game of Life (t = ' + str(i) + ')')
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    line.set_data([], [])
    return line,


<mask token>
if __name__ == '__main__':
    PL.show()

<mask token>
RD.seed()
width = 60
height = 60
initProb = 0.2
fig = PL.figure()
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(0,
    width), ylim=(0, height))
ax.grid(False)
ax.set_axis_off()
line, = ax.plot([], [], 'o-')


def init():
    global config, nextConfig
    config = SP.zeros([height, width])
    for x in range(width):
        for y in range(height):
            if RD.random() < initProb:
                state = 1
            else:
                state = 0
            config[y, x] = state
    ax.cla()
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    ax.set_title('CA: Game of Life (t = 0)')
    line.set_data([], [])
    nextConfig = SP.zeros([height, width])
    return line,


def animate(i):
    global config, nextConfig
    for x in range(width):
        for y in range(height):
            state = config[y, x]
            numberOfAlive = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    numberOfAlive += config[(y + dy) % height, (x + dx) % width
                        ]
            if state == 0 and numberOfAlive == 3:
                state = 1
            elif state == 1 and (numberOfAlive < 3 or numberOfAlive > 4):
                state = 0
            nextConfig[y, x] = state
    config, nextConfig = nextConfig, config
    ax.set_axis_off()
    ax.set_title('CA: Game of Life (t = ' + str(i) + ')')
    ax.pcolor(config, cmap=matplotlib.cm.Greens)
    line.set_data([], [])
    return line,


anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50,
    blit=True)
if __name__ == '__main__':
    PL.show()

# Simple CA simulator in Python
#
# *** Game of Life Rule ***
#
# Copyright 2008-2012 Hiroki Sayama
# sayama@binghamton.edu
#
# adapted for the Pocket project by Toshihiro Tanizawa
# on 2018-07-15(Sun) 16:49

# import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.cm
import matplotlib.animation as animation

import matplotlib.pyplot as PL
import random as RD
import scipy as SP

# === initial setting ==============================================
RD.seed()
width = 60
height = 60
initProb = 0.2

# ==================================================================
# set up the figure, the axis, and the plot element
fig = PL.figure()
ax  = fig.add_subplot(111, aspect='equal', autoscale_on=False,
                      xlim=(0, width), ylim=(0, height))
ax.grid(False)
ax.set_axis_off()
line, = ax.plot([], [], 'o-')

# === init() ======================================================
def init():
    global config, nextConfig

    config = SP.zeros([height, width])
    for x in range(width):
        for y in range(height):
            if RD.random() < initProb:
                state = 1
            else:
                state = 0
            config[y, x] = state

    ax.cla()
    ax.pcolor(config, cmap = matplotlib.cm.Greens)
    ax.set_title('CA: Game of Life (t = 0)')
    line.set_data([], [])

    nextConfig = SP.zeros([height, width])

    return line,
# =======================================================================

# === animate() =========================================================
def animate(i):
    global config, nextConfig

    for x in range(width):
        for y in range(height):
            state = config[y, x]
            numberOfAlive = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    numberOfAlive += config[(y+dy)%height, (x+dx)%width]
            if state == 0 and numberOfAlive == 3:
                state = 1
            elif state == 1 and (numberOfAlive < 3 or numberOfAlive > 4):
                state = 0
            nextConfig[y, x] = state

    config, nextConfig = nextConfig, config

    ax.set_axis_off()
    ax.set_title('CA: Game of Life (t = ' + str(i) + ')')
    ax.pcolor(config, cmap = matplotlib.cm.Greens)
    line.set_data([], [])

    return line,
# ======================================================================

# import pycxsimulator
# pycxsimulator.GUI().start(func=[init,draw,step])

# === rendering part ===================================================

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, blit=True)

# save the animation as an mp4. This requires ffmpeg or mencoder to be installed.
# The extra_args ensure that the x264 codec is used,
# so that the video can be embedded in html5. You may need to adjust this
# for your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
# anim.save('ca_GoL.mp4', extra_args=['-vcodec', 'libx264'])

# =====================================================================


if __name__ == '__main__':
    PL.show()

# <<< End >>>
