Skip to content

SKCrawford/game-of-life

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Game of Life

Description

This app is a socket.io server that implements Conway's Game of Life, a cellular automaton that simulates population lifecycles. The grid of cells, represented by a 2D array of booleans, is governed by a set of rules that indicate whether cells should live or die. Each grid evolution is represented by a tick number. Given an initial grid and the time between ticks in milliseconds, the server will provide the grid's evolutions at the desired rate.

Rules

  • Each cell has 8 neighbors (even the cells on the edges or in the corners).
  • Cells die when they have 0, 1, or 4+ live neighbors.
  • Cells revive or stay alive when they have 3 live neighbors.
  • Cells maintain their last state when they have 2 live neighbors.

Server

Installation

  1. Use git-clone to clone the repository:
$ git clone http://github.com/SKCrawford/game-of-life.git
  1. Install the node modules:
$ yarn

Usage

  1. Build and start the production server:
$ yarn start

Client

Installation

  1. Install either socket.io or the standalone socket.io client.

Usage

  1. Prepare the socket.
const socket = io('http://localhost:8000');
  1. Prepare the DTO/configuration object. The DTO should contain two properties: seed and delay. seed is the grid of cells as a two-dimensional array of booleans. delay is the time between ticks in milliseconds as a number. Be sure to surround your shape with all dead (false) cells as it will loop toroidally.
const dto = {
    seed: [
        [false, false, false, false, false],
        [false, false, false, false, false],
        [false,  true,  true,  true, false],
        [false, false, false, false, false],
        [false, false, false, false, false],
    ],
    delay: 2000,
};
  1. Using the socket, subscribe to the init event to get the unique ID.
socket.on('init', id => {
    /* ... */
})
  1. Using the socket, emit the DTO to the id received from the init event.
socket.on('init', id => {
    socket.emit(id, dto);
})
  1. To receive the results, subscribe to the id received from the init event.
socket.on('init', id => {
    /* emit the dto to id here */
    socket.on(id, payload => {
        console.log('Tick:', payload.tick);
        console.log(payload.grid);
    });
})

All together, a successful client call will look like:

// Prepare the socket and test grid
const socket = io('http://localhost:8000');
const dto = {
    seed: [
        [false, false, false, false, false],
        [false, false, false, false, false],
        [false,  true,  true,  true, false],
        [false, false, false, false, false],
        [false, false, false, false, false],
    ],
    delay: 2000,
};

// Request the unique ID, then emit and subscribe to it
socket.on('init', id => {
    // Seed the test grid to the ID channel
    socket.emit(id, dto);

    // Print the grid's evolutions from the ID channel
    socket.on(id, payload => {
        console.log('Tick:', payload.tick);
        console.log(payload.grid);
    });
})

Interfaces

DTO

interface Dto {
    /** The initial grid of cells. */
    readonly seed: boolean[][];

    /** The delay in milliseconds. */
    readonly delay: number;
}

Payload

interface Payload {
    /** The evolved grid. */
    readonly grid: boolean[][];

    /** The number indicating the lifecycle of the corresponding grid. */
    readonly tick: number;
}

Contributing

Pull requests will not be accepted as this is a personal project.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors