npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dat-life

v0.2.0

Published

Conway's Game of Life

Downloads

33

Readme

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.