Game Of Life in 3 Lines of Code
Seeing Jop Wielens' implementation of Conway's Game of Life I wondered if I might be able to figure out a way how to run the whole game entirely with Flash's bitmapData methods. It turns out that it works, and it even works pretty fast. And at its core it just needs 3 lines of code:
calculationMap.applyFilter( gameMap, rect, origin, neighbourBlur );
calculationMap.draw( gameMap, null, colorAdd, "add");
gameMap.paletteMap( calculationMap, rect, origin, zero, zero, rules );
Of course a few objects have to get initialized once, but then it's really just these 3 lines that do all the work. Here you go - click the canvas and press any key to reinitialize the canvas with random values:
View the full source and download it here.
Posted at October 05, 2008 05:33 PM | Further reading
Super!!! I remember when on my 15, I programming this game
Thanks for the compliments Jeremy ;-)
Searching google for other game of life implementations I just stumbled across this solution which uses a quite similar approach as mine, but has been done almost a year ago:
http://www.stdio.co.uk/blog/?cat=4&paged=2
And it looks like there are some other neat experiments, too.
It reminds me the extended version of Conway's Game of Life I did more then 15 years ago (!) following a my bother project. There were resources, fights, environment and evolution (with gene propagation). It was probably my first object oriented program. The pixels were drawn directly on video memory with machine code, on a brand new VGA powered by a 286. Unfortunately that version still outperform the flash bitmapData fifteen years later, but I have to admit the output was without antialias :).
Mmm, that's a classic.
... and I specially like the patterns that appear first when I take a new canvas
Hey Mario... great minds think alike! :-) I really must update my blog too - so many cool things to show.... How about doing A* with BitmapData? I think it's possible but I haven't had the time to write the code.
BTW, thanks for another inspirational talk at Flash On The Beach this year!
Bitmaps method and blend modes are so powerful, glad to see another proof of this in such a clean implementation.
This is great.
I like how you set it up to easily work with any set of rules from the life lexicon. I tried out a few different variations and they all worked nicely... my favorite has always been Coral (45678/3).