jenova
Version:
A game of life module that is intended to work both in a server side and client side environment.
110 lines (78 loc) • 2.73 kB
Markdown
A game of life module that is intended to work both in a server side and client side environment.
This module exposes 3 functions.
1. **compress** - Given a board array, return a compressed value and board-width
2. **expand** - Given a compressed value and width, return a board array
3. **next** - Given a board array, calculate the next "step", and return it in a callback function
4. **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**:
```javascript
var myBoard = [
[],
[],
[]
];
jenova.next(myBoard, {}, function(nextBoard){});
```
```javascript
// Using WebPack
var jenova = require("jenova");
var initialBoard = [
[],
[],
[],
[]
];
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:**
```javascript
var initialBoard = [
[],
[],
[],
[]
];
function generateBoard(board) {
jenova.next(board, {}, function(newBoard) {
io.emit('newBoard', jenova.compress(newBoard));
setTimeout(generateBoard.bind(this, newBoard), 1000);
});
}
```
**Client:**
```javascript
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)
});
```
[](https://github.com/TheIronDeveloper/jenova/tree/master/test) also serve as pseudo-documentation
A websocket example can be found here: https://github.com/TheIronDeveloper/websocket-of-life