A game of life module that is intended to work both in a server side and client side environment.
This module exposes 3 functions.
- compress - Given a board array, return a compressed value and board-width
- expand - Given a compressed value and width, return a board array
- next - Given a board array, calculate the next "step", and return it in a callback function
- draw - Given a Dom node, and some config, render a game of life board animation.
All three functions make use of an matrix. The default behavior expects 1s and 0s, but you can change how the config updates.
Example Board:
var myBoard = [
[0, 0, 1],
[0, 1, 1],
[0, 0, 0]
];
jenova.next(myBoard, {}, function(nextBoard){});
// Using WebPack
var jenova = require("jenova");
var initialBoard = [
[0, 1, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0]
];
jenova.draw('myCanvasId', initialBoard);
Server:
var initialBoard = [
[0, 1, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0]
];
function generateBoard(board) {
jenova.next(board, {}, function(newBoard) {
io.emit('newBoard', jenova.compress(newBoard));
setTimeout(generateBoard.bind(this, newBoard), 1000);
});
}
Client:
var gameOfLifeCanvas = document.getElementById('gameOfLife');
var socket = io.connect('http://localhost:3000');
socket.on('newBoard', function (compressedBoard) {
var newBoard = jenova.expand(compressedBoard.compressed, compressedBoard.width);
generateBoard(newBoard, gameOfLifeCanvas)
});
- The test cases also serve as pseudo-documentation.
- A websocket example can be found here: https://github.com/TheIronDeveloper/websocket-of-life
- Additionally, the
/examples
directory is vanilla express app with an example ofjenova.draw