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.
- 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.
- Use git-clone to clone the repository:
$ git clone http://github.com/SKCrawford/game-of-life.git- Install the node modules:
$ yarn- Build and start the production server:
$ yarn start
- Install either socket.io or the standalone socket.io client.
- Prepare the socket.
const socket = io('http://localhost:8000');- Prepare the DTO/configuration object. The DTO should contain two properties:
seedanddelay.seedis the grid of cells as a two-dimensional array of booleans.delayis 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,
};- Using the socket, subscribe to the
initevent to get the unique ID.
socket.on('init', id => {
/* ... */
})- Using the socket, emit the DTO to the
idreceived from theinitevent.
socket.on('init', id => {
socket.emit(id, dto);
})- To receive the results, subscribe to the
idreceived from theinitevent.
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);
});
})interface Dto {
/** The initial grid of cells. */
readonly seed: boolean[][];
/** The delay in milliseconds. */
readonly delay: number;
}interface Payload {
/** The evolved grid. */
readonly grid: boolean[][];
/** The number indicating the lifecycle of the corresponding grid. */
readonly tick: number;
}Pull requests will not be accepted as this is a personal project.