Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dat-life

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dat-life

Conway's Game of Life

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 19 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc