Neverending Package Mountain

    dat-life

    0.1.0 • Public • Published

    Conway's Game of Life

    A TypeScript library for Conway's Game of Life that supports a typical implementation, multi-color versions and other variations on the standard game.

    This library does not support any mechanism to output the board; users are responsible for its display, this library merely calculates the state of the game board at each generation.

    See examples/life.js for an example of a console application that will draw the board.

    Usage

    1. Create a new instance of the Life object - with optional width and height of the game board:

       const life = new Life(width, height);
      
    2. Set the initial conditions. The initial board is blank, but you can either randomize the layout of the populants of the board:

       life.randomize();
      

      Or you can place the population explicitly - given the x/y coordinates of the board, you can set a cell to have a live populant:

       life.set(x, y, 1);
      

      Or to have a no populant:

       life.set(x, y, 0);
      
    3. Get the board data. You can get the value of a single cell by its x/y coordinates:

       const alive = life.get(x, y);
      
    4. Move on to the next generation.

       life.next();
      

      Then you can continue displaying the board (go to step 3).

    Example

    Add the dat-life package (eg, npm install dat-life). Then:

    const { Life } = require('dat-life');
     
    const boardWidth = 80; // width of the game board
    const boardHeight = 24; // height of the game board
     
    const life = new Life(boardWidth, boardHeight);
    life.randomize(); // set up the initial board with random populantss
     
    while (true) {
        for (let y = 0; y < boardHeight; y++) {
            for (let x = 0; x < boardWidth; x++) {
                if (life.get(x, y)) {
                    process.stdout.write('*');
                }
                else {
                    process.stdout.write(' ');
                }
            }
     
            process.stdout.write('\n');
        }
     
        processs.stdout.line('\n');
        life.next();
    }

    Color mode

    This package supports the standard 2- and 4-color versions of the Game of Life (called immigration and quad-life, respectively).

        life.setColors(2); // immigration
        life.setColors(4); // quad-life

    The default 2- and 4-color versions both handle the birth and death of cells like the default Game of Life does - meaning that cells live and die according to the default rules, but coloring is based on unrelated rules.

    This package also supports a new "decay" mode, which changes the way way that alive cells die in color mode.

        life.setDecay(true); // decay mode

    In decay mode, cells do not simply die from overcrowding or exposure. Instead, cells are reduced by a color value. (So if a cell is at color 3 but has overcrowding, by default when the cell has overcrowding, it would move from color 3 to dead. In decay mode, the color is simply reduced - so color 3 changes to color 2.)

    Decay mode is especially useful for games that begin in packed or highly populated initial conditions where you don't want every cell to die immediately from overpopulation.

    License

    dat-life is released under the MIT license.

    See the license file for the full license text.

    Install

    npm i dat-life

    DownloadsWeekly Downloads

    0

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    13.3 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar