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.
Create a new instance of the
Lifeobject - with optional width and height of the game board:
const life = new Life(width, height);
Set the initial conditions. The initial board is blank, but you can either randomize the layout of the populants of the board:
Or you can place the population explicitly - given the
ycoordinates 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);
Get the board data. You can get the value of a single cell by its
const alive = life.get(x, y);
Move on to the next generation.
Then you can continue displaying the board (go to step 3).
dat-life package (eg,
npm install dat-life). Then:
const Life = ;const boardWidth = 80; // width of the game boardconst boardHeight = 24; // height of the game boardconst life = boardWidth boardHeight;life; // set up the initial board with random populantsswhile truefor let y = 0; y < boardHeight; y++for let x = 0; x < boardWidth; x++if lifeprocessstdout;elseprocessstdout;processstdout;processsstdout;lifenext;
This package supports the standard 2- and 4-color versions of the Game of Life (called immigration and quad-life, respectively).
life; // immigrationlife; // 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
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.
dat-life is released under the MIT license.
See the license file for the full license text.