Noteworthy Programming Masterpiece

# npm

Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

## games-of-life

0.5.4 • Public • Published

# games-of-life

is a Mathematical solution to any Game of Life variation

## Demo

I created this hexagonal Game of Life demo to show that this package generalize the Game of Life in any of its variations. Click the image below to try it!

## Installation

With npm do

You could also use a CDN adding this to your HTML page

## Idea

On the 30th of May 2015, I have participated in a Coderetreat at Milan XPUG.

We have had so much fun coding implementations of the Game of Life.

As a mathematician, I think it is a very interesting problem. I couldn't resist to generalize it and try to solve it in any of its variations.

A function getNeighboursOf, which returns the set of cells adjacent to a given cell, defines the shape of Game of Life universe.

Infact, since

``````getNeighboursOf(cell1) = getNeightboursOf(cell2) ⇒ cell1 = cell2
``````

it can be said that the set of neighbours of a cell is dual to the cell itself, hence the definition of the getNeighboursOf function is equivalent to the definition of the space of a Game of Life universe. Note that it defines the concept of nearness.

In other words,

if you define a getNeighbours function you also shape the space of a Game of Life universe

On the other hand, let be given the definition of an isAlive function, which returns `true` if the given cell is alive, `false` otherwise. It can be easily extended to an areAlive function which, given a list of cells, returns a list of booleans; following a similar identification we used for the getNeighboursOf function, an isAlive function describes the state of a Game of Life universe at a given moment.

The considerations above allow to implement an abstract Game of Life in a functional way, in any of its variations, for example:

• finite grid
• infinite grid
• 2-dimensional, 3-dimensional, n-dimensional
• square, triangular, hexagonal tiles
• cylinder, torus, moebius strip, boy surface

Take a look to createWorld.js for the implementation's details.

The world has a transition rule which defaults to the classicTransitionRule.js.

## Example

A simple example is the infinite grid with two dimensional coordinates.

Define a getNeighboursOf which returns the neighbours of a given cell.

Create a Game of Life world, and get the evolve function

The empty grid is represented by a function that always returns false, so

Try with a single cell at the origin

Ok, a more interesting example is the blinker

You may check that the verticalBlinker evolves in the horyzontalBlinker and vice versa

MIT

## Keywords

### Install

`npm i games-of-life`

4

0.5.4

MIT

234 kB

54

### Homepage

g14n.info/games-of-life

### Repository

github.com/fibo/games-of-life