// life.cola
//
// Game of life
//
// Copyright (C) 2002 Melvin Smith
//
public class Life {
    public string Generate(string input) {
        int cell, neighbours, i;
        int len = strlen(input);
        int pos, offset;
        string birth = "   *     ";
        string death = "  **     ";
        string output = input;
        for(cell=0; cell < len; cell++) {
            neighbours = 0;
            i = cell + len;
            if(input[(i - 1) % len] == "*") neighbours++;
            if(input[(i + 1) % len] == "*") neighbours++;
            if(input[(i - 17) % len] == "*") neighbours++;
            if(input[(i + 17) % len] == "*") neighbours++;
            if(input[(i - 16) % len] == "*") neighbours++;
            if(input[(i + 16) % len] == "*") neighbours++;
            if(input[(i - 15) % len] == "*") neighbours++;
            if(input[(i + 15) % len] == "*") neighbours++;
            if (input[cell] == "*")
                output[cell] = death[neighbours];
            else if(neighbours == 3) {
                output[cell] = "*";
            }
        } 
        return output;
    }

    static void Main() {
        string world = "   **   * * *   " +
                    " **  *  *       " +
                    "             *  " +
                    "        *    ** " +
                    "  *     *       " +
                    "     ****       " +
                    "     ****       " +
                    "        *       " +
                    "         *  **  " +
                    "     *  *       " +
                    "        *    *  " +
                    "          *     " +
                    "                " +
                    "            *   " +
                    "     *          " +
                    "     *         *"
                    ;
        int i, j;
        for(i = 0; i < 500; i++) {
            // Print World
            for(j = 0; j < 16; j++)
                puts(substr(world, j * 16, 16) + "\n");
            puts("----------------\n");
            world = Generate(world);
        }
    }
}
