In [1]:
from zasim import cagen
from zasim.display import console
from IPython.core.display import display, clear_output, publish_png
from time import sleep
In [2]:
sim_obj = cagen.GameOfLife(size=(40,20))
Obviously, a OneDimensionalConsolePainter won't do us much good for a Game of Life, so instead we use a TwoDimensionalConsolePainter. By supplying auto_output=False, we can let the simulator do a few steps and then look at the output, rather than have every single step shown to us:
In [3]:
dsp = console.TwoDimConsolePainter(sim_obj, auto_output=False)
In [4]:
for i in xrange(15):
sim_obj.step()
Now, to display the configuration, we could just convert the display object to a string and print the result, but using IPythons display function gives us a nice table instead.
Here we have two consecutive configurations displayed:
In [5]:
display(dsp)
sim_obj.step()
display(dsp)
Using clear_output and sleep, we get a more traditional display:
In [6]:
for i in xrange(15):
clear_output()
display(dsp)
sleep(0.2)
sim_obj.step()
The implementation of Game of Life in zasim allows you to supply the following parameters:
You can plug in those values as a dictionary into the life_params argument to the constructor to the GameOfLife class. You don't have to supply all of them. If you leave any out, the default values from Conway's Game of Life will be used instead.
Feel free to try around with different life_params.
In [7]:
simb = cagen.GameOfLife(size=(30, 15), life_params=dict(reproduce_min=1, reproduce_max=2))
dspb = console.TwoDimConsolePainter(simb, auto_output=False)
for i in xrange(20):
simb.step()
display(dspb)
for i in range(3):
simb.step()
display(dspb)
In [8]:
from zasim.display import qt
In [9]:
# we turn on activity and histogram tracking now, so that we can display them later
simc = cagen.GameOfLife(size=(100,100), histogram=True, activity=True)
dspc = qt.TwoDimQImagePainter(simc, scale=3)
for i in range(10):
# do a few steps, so that the first few images we display look typical
# for a game of life.
simc.step()
In [10]:
for i in range(2):
simc.step()
display(dspc)
In [11]:
# find out what attributes are available for display.
# only some make sense for a histogram, though. Using a histogram painter for
# cconf or nconf, for example, will not yield useful results. Those are the
# configurations themselves.
print simc.t.attrs
w, h = (300, 100)
act = qt.HistogramPainter(simc, "activity", w, h)
hst = qt.HistogramPainter(simc, "histogram", w, h)
In [12]:
for i in range(300): simc.step() # fill up the histogram and activity images
display(act)
display(hst)
Additionally, the dspc object and the histogram painters now offer the possibility to save the image to a file using their export(filename) method.