October 28, 2009

Conway’s Game of Life (in Java)

Just recently I’ve become interested in Conway’s Game of Life. Basically it’s a set of rules governing the life cycle of cellular automata. Even more basically it’s a 2D grid where the number of living cells around another cell decide whether it lives or dies (or comes to life).

For example: imagine a chess board. Now pick a square. If that square has fewer than 2 living neighbours (the 8 surrounding squares) then it either stays or becomes dead, as if from lonliness. If it has more than 4 living neighbours then it dies, as if from overcrowding. If it has exactly 2 or 3 living neighbours and is already alive then it stays alive. If it has exactly 3 living neighbours and it was dead then it becomes alive.

And that’s it. But because it plays out over many turns (or iterations) it allows for certain patterns to repeat, or entire formations to move across the grid as cells become dead or alive in turn.

Anyway, my fascination with this led me to write a small Java game of life simulator. I’ve provided the code here

It’s not good or tidy code, but it might interest you if you’re a starting programmer.

Game of Life with 3 Gliders

Game of Life with 3 Gliders

An interesting shape to start off with is the glider (O is dead, X is alive):

OOX

XOX

OXX

put that in the top left and it will move diagonally down and right.

Experiment and have fun :)

Filed under: musings
Tags: , ,
komakino @ 10:34 pm

Leave a Reply

You must be logged in to post a comment.

x