Game of life (update)

The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is the best-known example of a cellular automaton. The “game” is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input from humans. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.

Shortest code

While I like ActionScript the most, I decided to make a version in flash. It is also fine moment to introduce wonderfl project. Wonderfl is a service where you can build Flash(swf) online. Wonderfl is online, free and social. After registration you are able to write and build your own codes, follow people, favourtie theirs work or even fork other user’s code (improve, change).

My aim was to make the code shortest possible, so you may find it mess, hell yeah it is :). Try to fork my code on wonderfl to achieve shortest possible. Use some tricks to make your code shorter.

// Conway's Game of Life http://en.wikipedia.org/wiki/Conway's_Game_of_Life
// field 38x38 px

package{
    import flash.display.Sprite;
    
    public class GameOfLife extends Sprite{
        public function GameOfLife(){
            with(x)
            for each(i in w=h=38,a=new Array(w*h),b=[63,99,101,127,128,135,136,
                149,150,164,168,173,174,187,188,191,192,201,207,211,212,229,230,
                239,243,245,246,251,253,277,283,291,316,320,355,356])
                a[i]=1;
            addEventListener("enterFrame",function():void{
                with(graphics){
                for(clear(),c=[i=0];i<w*h;i++)
                    X=i%w,Y=int(i/w),j=X-1,k=X+1,l=Y-1,m=Y+1,
                    n=int(j>-1&&l>-1?a[i-37]:0)
                    +int(l>-1?a[i-w]:0)
                    +int(k<w&&l>-1?a[i-39]:0)
                    +int(j>-1?a[i-1]:0)
                    +int(k<w?a[i+1]:0)
                    +int(j>-1&&m<h?a[i+37]:0)
                    +int(m<h?a[i+w]:0)
                    +int(k<w&&m<h?a[i+39]:0),
                    c[i]=a[i]?(n==2||n==3)?1:0:int(n==3),
                    a[i]?drawRect(X*10,Y*10,10,10):beginFill(0);
                    a=c;
                }
            });
        }
    }
}

Fastest code

Here is my attempt for the fastest code, I have used Pixel Bender to achieve best performance, it seems pretty smooth considering, it is capable of 12.000.000 pixels testing in 1 second. You can download gameoflife.pbk file from here. My first try was to use image1 input type, but image1 was not working correctly with sampleNearest() method – that seems to be the fastest sampler method in pixel bender. Then I switched to image4 and with sampleNearest() it resulted in much better performance compared to image1 and sample(). Other code optimizations were done to aviod as much if statements as possible.

Where to go next

Leave a comment

Please be polite and on topic. Your e-mail will never be published.