This tutorial is thought as an exercise to get a grip of the concepts looked at during the previous tutorial, such as two-dimensional arrays and the principles around Cellular automata. The exercises bellow are suggestions, if you want to try something yourself in class, or you have any thing you may want to look at in more detail, you are welcome to try that instead.
Conway’s “Game of Life”. As we mentioned last tutorial, it it possible to modify the code of the basic CA to write the rules for Conway’s “Game of Life”. There are also a number of implementations in Processing that you may look at if you want some clues, but of course the challenge is to try to write it yourself. The basic rules for the “Game of Life” are these (from wikipedia’s definition):
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
If you look at the code form the previous tutorial, there are 2 basic changes that you need to do, and a third one to make it a bit more exciting.
- First, you need to change the way the neighbours are counted. Right now, when each cell counts its neighbours (that is, the amount of neighbours that are set to “true”) it also counts itself. To avoid counting itself, you can do a couple of things: You can check in the small nested for() loops that we use to iterate through the neighbours in the “countNeighbours()” function if the neighbouring cell we are considering has the same indices as the column and row of the cell at the centre, and then don’t add it. You can also add all neighbours as now, and then subtract 1 if the cell at the centre was set to “true”. Think a bit about it, there are a number of different ways of writing each methods. I will help you with it in class.
- Once you have changed the way of counting the neighbours, you need to change the rules so they correspond to those explained above for the “Game of Life”. Possibly the easiest way of doing this is implementing the rules above using if() conditions in the “applyRules()” function (instead of the “look-up table” rules used there originally). Another change you need to do is to make “applyRules()” to take more than one parameter, since the rules of the “Game of Life” need also the current state of the cell…
- In principle you code now implements the rules for the “Game of Life” CA, but starting with only one cell on in the middle does not produce very interesting results (the first cell “dies” in the first iteration, and that’s that…). You can try to set more interesting starting conditions, for example by using the code we used in the first 2D array example to fill it with random numbers.
Here is the code of a possible implementation (with a few extra changes also for making it run fast, by changing or deleting the frameRate() call, and not drawing the rectangles of each cell ): GameOfLife . As we have seen today in class there are many different ways of writing the code so it does the same thing, remember.If you are doing it at home, try to do the exercise first… to begin looking at the code runs against having an exercise in the first place!
1 Dimensional CA. Rather than using a 2D CA, you can try to think how to do a 1D CA, like those in Stephen Wolfram’s A New Kind of Science There are lots of code laying around for that (for example under “File->Examples…->Topics->Cellular Automata->Wolfram” or under “File->Examples…->Books->Nature of Code->chp7_CA”). I have written a simple implementation which is heavily commented, that you can also have a look at, downloadable from here: CA1D. The basic idea of a 1D Cellular automata is that you have a one dimensional array representing a sequence of cells and their states (in the simplest case “on” or “off”, “true” or “false”), and you calculate the states of each cell at next step depending of the states of its 2 closest neighbours, one to the left, and one to the right, and the cells current state. Usually the states are displayed on top of each other, showing the evolution in time of the cellular automata in 2 dimensions (the Y axis is used as the time axis), like this:
If you feel brave, and perhaps you are really kin on calculus (not too complicated, but one needs to be able to understand the notation) you can have a go at Reaction-Diffusion models, like those explined by Karl Sims here. You can see here some 3D printed work by Nervous System. Reaction Diffusion models were first proposed by Alan Turing to explain processes of morphogenesis, that is, process of formation, specially in biology. Turing proposed a basic model that explained the underlying mechanisms behind the emergence of shape and pattern in biology. His model could easily reproduce patterns and textures common in nature, like the spots of the leopard, or the stripes of a zebra. I have implemented an example of Turing’s Reaction Diffusion model in Processing that you can download here: TuringReactDiff. It is based on Java code by Christopher G.Jennings that you can find here.
If you have any thing you want to look at in more detail, or some project you are working on, just bring it on!