npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@limpirium/game-of-life

v0.3.0

Published

Simple implementation of Conway's Game Of Life

Downloads

7

Readme

Conway's Game Of Life

This is just yet another implementation of Conway's Game Of Life written in Typescript.

It primarily started as a little excercise with Javascript Generator using a functional approach.

Concepts

  • Cell is a [number, number] tuple denoting coordinates on the grid.
  • Neighbors is a function with parameter cell: Cell calculating the neighbors of the given cell.
  • Generation consists of all cells: Cell[] which are alive, and its epoch: number.
  • Generations is a Generator yielding Generations either for direct iteration with for...of, or for sequential consumption with its next() function. If you do not want to continue, i.e. do not care about any values, optionally pass false to next().
  • World is a factory function with parameters cells: Cell[] for the initial Generation and epochMax?: number which creates and returns the corresponding Generations.
  • Game is a factory function with parameters Neighbors which creates and returns a World.

The defining parameter of a Game is the way how neighbors for a Cell are calculated by means of a Neighbors function. Most importantly it indirectly determines the available coordinates which a Cell can occupy, i.e. if the grid of the Game is infinite, finite (but unbounded) or bounded.

There are helper factory functions creating World functions with the some default Neighbors functions. The default Neighbors calculate Neighbors for a Cell in the literal sense, i.e. the Cell[] ((usually of length 8)) directly adjacent to the given Cell:

  • bounded with paramater max: Cell defining the maximum allowed x and y coordinates. The minimum [0, 0] is implicit. The game has hard (unconnected) bounds out ouf which no Cell can exist. This behavior is best conceptualized as rectangle or square.

  • finite with paramater max: Cell defining the maximum allowed x and y coordinates. The minimum [0, 0] is implicit. The game has connected bounds. A cell that would spawn outside any of these bounds (top, right, bottom, left) will spawn on the opposite side of the grid. This behavior is best conceptualized as a torus, (or) like a level in Pacman .

  • infinite with no parameters. There are no boulds. Keep in mind that this especially means that a Cell can have negative coordinates. This behavior is best conceptualized as an infinite plane.

Typescript

This package is build with Typescript and comes with its own type definitions. Hense this project uses javascript generators only available since ES2015. So in order order to use within a Typescript based project make sure to set at least "target": "es2015" in your tsconfig.json.

{
  "compilerOptions": {
    "target": "es2015"
    // ... and more
  }
}

Basic usage example

// Typescript example with "module": "commonjs" in tsconfig.json

import { games, Cell } from '@limpirium/game-of-life';
// const world = games.bounded([10, 10]); // or
// const world = games.finite([10, 10]); // or
const world = games.infinite();
const glider: Cell[] = [
  [2, 1],
  [3, 2],
  [1, 3],
  [2, 3],
  [3, 3],
];

// direct iteration
let generations = world(
  glider,
  1000, // set a maximum of 1000 epochs or iteration will probably run forever
);
for (let generation of generations) {
  console.log(generation);
}

// manual, sequential access
generations = world(glider);
console.log(generations.next()); // -> {value: {cells: [...], epoch: 0}, done: false}
console.log(generations.next()); // -> {value: {cells: [...], epoch: 1}, done: false}
console.log(generations.next()); // -> {value: {cells: [...], epoch: 2}, done: false}
// to stop pass `false` to `next()`
console.log(generations.next(false)); // -> {value: {cells: [...], epoch: 3}, done: true}
console.log(generations.next()); // -> {value: undefined, done: true}

Todos / next steps

  • check in browser
  • write some tests
  • stop generation if there are no changes from one generation to the other
  • provide a library of some famous initial Cell[]

Ideas

  • possibility for custom birth/death conditions
  • custom Neighbors functions and explore how this effects the Game Of Life. Haven't tried it or checked for research myself yet, but may be interesting 🤔 or maybe not 🤷