An open API service indexing awesome lists of open source software.

https://github.com/sondregj/conway

🧫 Cellular Automata
https://github.com/sondregj/conway

cellular-automata conway functional game-of-life typescript

Last synced: 5 months ago
JSON representation

🧫 Cellular Automata

Awesome Lists containing this project

README

          


🧫




Conway

Cellular Automata



npm (latest)


npm bundle size


GitHub contributors


License


Gitmoji

A simple cellular automaton implementation, in TypeScript.

## Usage

Install by running `npm install @sondregj/conway`

```javascript
import { advance } from '@sondregj/conway'

const world = {
cells: [
[{ alive: false }, { alive: false }, { alive: true }],
[{ alive: true }, { alive: false }, { alive: true }],
[{ alive: false }, { alive: true }, { alive: true }],
],
}

const day1 = advance(world)
```

You can also define custom rule functions.

```javascript
import { advance } from '@sondregj/conway'

const world = {
cells: [
[{ alive: false }, { alive: false }, { alive: true }],
[{ alive: true }, { alive: false }, { alive: true }],
[{ alive: false }, { alive: true }, { alive: true }],
],
}

const rules = (board, cell, x, y) => !cell.alive

const day1 = advance(world, rules)
```

A convenience function for initializing boards is also included.

```javascript
import { initializeBoard, advance } from '@sondregj/conway'

const genesis: Board = initializeBoard(64, 64, { random: true })

const day1 = advance(genesis)
```

The following TypeScript types are included.

```typescript
import { Board, BoardTick, Cell, RuleFunction } from '@sondregj/conway'

const world: Board = {
cells: [
[{ alive: false }, { alive: false }, { alive: true }],
[{ alive: true }, { alive: false }, { alive: true }],
[{ alive: false }, { alive: true }, { alive: true }],
],
}

const cell: Cell = { alive: true }

const advance: BoardTick = (board: Board): Board => board

const rules: RuleFunction = (board: Board, cell: Cell, x: number, y: number): boolean =>
true
```

## License

MIT © 2019 Sondre Gjellestad