Skip to content

sondregj/conway

Repository files navigation

🧫

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

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.

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.

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

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

const day1 = advance(genesis)

The following TypeScript types are included.

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