Java life simulator

Having played a bit with cellular automata’s – and looking at various other rulesets. I started to wonder what the result would be if every cell had i different ruleset, or just different types of cells interacting.

And what if there were mutations and sexes involved ?

Well getting ideas like that are dangerous, they usually result in some kind of coding project. And well this time I felt the urge to implement some features. Instead of continuing to try to write in c++ with CImg or SDL, which is kinda of new to me. I went back to java. Mainly because I’m more experienced with java and I have some code that I can reuse from earlier projects ( a flamefractal render and a small framework for rendering strange attractors ).

So I started hacking away making nice features and building a framework for experiments. Where there are generic cells and loose coupling using interfaces.

A cell just needs to implement :

  • a constructor taking its coordinates, and the map it belongs to as arguments
  • a isAlive()
  • boolean iterate () which gets it neighbors from the map and decides wheter it should be dead or alive in the next round
  • nextState() instead of using buffers I decided to have a method in a cell that changes it state, and as a ugly temporary hack at the same time returns value to determine its color

Of course in order to apply more interesting rules I need them to have a type, and they should be able to decide their fate based upon their neighbors types. And I still need to figure out a way to implement some kind of generic ruleset, if/when I need a way to mutate.

All the cells are placed in a double array (cells[][] ) and there’s a CellMap – class keeping track of them. I tried optimizing by only looking a cells that are alive, and their neighbors – instead of looking at every cell in each iteration. I used a MVC pattern so the map runs in its own thread and after each iteration it calls the gui to be painted. Its quite fast at iterating, but the painting process takes forever. Alas, the next step is to use a buffered image to speed op the rendering process, or …

After all these fancy designpatterns, threading all it does at the moment is a simple “life-like” cellular automata, but Its made for extending. And It has nice coloring., in earlier experiments i have made a generic gradient class, which came in very handy.

By the way please take a look at my online collection of random strange attractors

Leave a Reply