John Conway's Life

JAVA IMPLEMENTATION OF LIFE

After recently re-reading Paul Davies' book The Mind of God (an excellent book, although I don't agree with some of its conclusions) I was reminded of programming John Conway's Game of Life on a SINCLAIR ZX80, almost 30 years ago, from a listing in a magazine. The ZX80 — a little-known precursor to the ZX81, and later, ZX Spectrum — was a tiny kit-built computer with only 1K of memory. Given that there was only 1K of memory, which was also shared by the video display, the program was an incredibly compact implementation, written in Sinclair BASIC...

I wrote this Java version specifically with compactness in mind, and as the start of a more elaborate project (just for fun, I guessed I could write the whole thing, including toroidal borders, in about 25 lines of Java, while still maintaining reasonably good coding standards). To avoid bothering with graphics, the results are simply printed to System.out, but if you size the X and Y constants to fit into a Windows Command Prompt ("cmd.exe") window, then each refresh of the display will redraw the entire window contents, providing a rather flickery ZX80-ish animation.

Notes:
 • The universe is held in the 3-dimensional int array u; the first 2 dimensions represent the X and Y dimensions, and the third provides a second frame in which to calculate the next generation.
 • A glider is the classic Life pattern, made up of 5 cells; the universe is initialized with a single glider.
 • The for "tick" loop is currently set to perform 100 iterations... obviously this could be changed to a much greater number, or changed to a do..while loop with some sort of conditional exit.
 • The universe is toroidal (i.e. like the display on an Asteroids game), which is implemented by the cryptic-looking yd, yi, xd, xi variables.
 • The variable c is the number of live neighbors surrounding a cell.
 • The Thread.sleep(200) is in milliseconds, so the frame rate is set to approximately 5 frames per second, not considering the processing time of the calculations and display.


//* John Conway's Life
//* Andrew Carson 2009

package www;

public class Life {

    public static void main(String[] args) { new Life(); }

    private static final int X = 79, Y = 24;

    public Life() {
        int[][][] u = new int[X][Y][2];

        // create a glider
        u[4][4][0] = 1; u[5][4][0] = 1; u[6][4][0] = 1;
        u[6][3][0] = 1; u[5][2][0] = 1;

        for (int tick = 0; tick < 100; tick++) {
            for (int y = 0; y < Y; y++)
                for (int x = 0; x < X; x++)
                    System.out.print(((u[x][y][1] = u[x][y][0])
                            == 1?"O":" ") + (x==X-1?"\n":""));

            // build the next generation
            for (int y = 0; y < Y; y++)
                for (int x = 0; x < X; x++) {
                    int yd = y-1<0?Y-1:y-1, yi = y+1==Y?0:y+1;
                    int xd = x-1<0?X-1:x-1, xi = x+1==X?0:x+1;
                    int c = u[xd][yd][1] + u[x][yd][1]
                            + u[xi][yd][1] + u[xd][y][1]
                            + u[xi][y][1] + u[xd][yi][1]
                            + u[x][yi][1] + u[xi][yi][1];
                    
                    if (u[x][y][1] == 1)
                        u[x][y][0] = c == 2 || c == 3 ? 1 : 0;
                    else
                        u[x][y][0] = c == 3 ? 1 : 0;
                }
            
            try { Thread.sleep(200); } catch (Exception e) { }
        }
    }
}


The following "cmd.exe" screenshot (actually a merge of 3 screenshots), running on Windows XP, shows the glider in full flight:




HOME

INFO

CONTACT

MORE

FOOLISHNESS

LINKS

SITE MAP

Copyright © 2009 Andrew Carson. All Rights Reserved.  CSS2-3 Validated.