2.3.0 • Published 8 years ago
jenova v2.3.0
Jenova
A game of life module that is intended to work both in a server side and client side environment.
Implementation
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 consisting of 0s and 1s.
Example Board:
var myBoard = [
[0, 0, 1],
[0, 1, 1],
[0, 0, 0]
];
jenova.next(myBoard, {}, function(nextBoard){});
Example Usage
Browser-Only
// 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]
];
function generateBoard(board, canvas) {
var ctx = canvas.getContext('2d'),
width = canvas.width,
height = canvas.height,
cellHeight = height/ board.length,
cellWidth = width / board[0].length;
// Loop through the board and draw each cell
board.forEach(function(row, rowIndex) {
row.forEach(function(col, colIndex) {
ctx.fillStyle = col ? '#ccc' : '#fff';
ctx.fillRect(colIndex*cellWidth, rowIndex*cellHeight, cellWidth, cellHeight);
ctx.strokeRect(colIndex*cellWidth, rowIndex*cellHeight, cellWidth, cellHeight);
});
});
// Finally Generate a new board, with a callback to redraw it
jenova.next(board, {}, function(newBoard) {
setTimeout(generateBoard.bind(this, newBoard, canvas), 200);
});
}
window.requestAnimationFrame(generateBoard.bind(this, initialBoard, document.getElementById('myCanvas')));
Server-to-Browser (using WebSockets)
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)
});
Notes
The test cases also serve as pseudo-documentation A websocket example can be found here: https://github.com/TheIronDeveloper/websocket-of-life