Skip to content

Latest commit

 

History

History
71 lines (60 loc) · 2.89 KB

lifegame.md

File metadata and controls

71 lines (60 loc) · 2.89 KB

Life Game

Overview

Please see the Wikipedia.

Examples

Example 1. Life Game Algorithm

Code

const N = 22;  // Vertical
const M = 78;  // Horizontal
var a = [], b = [];
a[N/2][M/2] = a[N/2-1][M/2] = a[N/2+1][M/2] = a[N/2][M/2-1] = a[N/2-1][M/2+1] = 1;

function life() {
    for (var g = 1; g <= 1000; g++) {
        System.print("Generation %4d\n" % g) if (g == 1000);
        for (var i = 1; i <= N; i++) {
            for (var j = 1; j <= M; j++)
                if (a[i][j]) {
                    System.print("*") if (g == 1000);
                    b[i-1][j-1]++;  b[i-1][j]++;  b[i-1][j+1]++;
                    b[i  ][j-1]++;                b[i  ][j+1]++;
                    b[i+1][j-1]++;  b[i+1][j]++;  b[i+1][j+1]++;
                } else System.print(".") if (g == 1000);
            System.print("\n") if (g == 1000);
        }
        for (var i = 0; i <= N + 1; i++)
            for (var j = 0; j <= M + 1; j++) {
                if (b[i][j] != 2) a[i][j] = (b[i][j] == 3);
                b[i][j] = 0;
            }
    }
}

life();

Result

Generation 1000
..............................................................................
.......**.....................................................................
.......**.....................................**..............................
.............................................*..*.............................
............................**................*.*.............................
............................**.................*........**....................
....................................*...................**....................
...........**.......................*.........................................
...........**.......................*.........................................
..............................................................................
...............................**.............................................
..............................*..*............................................
...............................*.*............................................
................................*.............................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................