Skip to content

sondregj/conway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6be8bb9 · Oct 4, 2019

History

27 Commits
Oct 4, 2019
Oct 2, 2019
Oct 2, 2019
Sep 26, 2019
Sep 26, 2019
Sep 26, 2019
Sep 26, 2019
Sep 26, 2019
Sep 26, 2019
Oct 2, 2019
Oct 2, 2019
Oct 4, 2019
Oct 4, 2019
Oct 2, 2019
Sep 26, 2019

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