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; jenovanextmyBoard {} {};
Example Usage
Browser-Only
// Using WebPackvar jenova = ; var initialBoard = 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0; { var ctx = canvas width = canvaswidth height = canvasheight cellHeight = height/ boardlength cellWidth = width / board0length; // Loop through the board and draw each cell board; // Finally Generate a new board, with a callback to redraw it jenovanextboard {} { ; };} window;
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; { jenovanextboard {} { io; ; };}
Client:
var gameOfLifeCanvas = document;var socket = io; socket;
Notes
The test cases also serve as pseudo-documentation A websocket example can be found here: https://github.com/TheIronDeveloper/websocket-of-life