JSFiddle - React, Tailwind, and code Playground
by gRoberts
HTML
<canvas id="set" width="16" height="16"></canvas>
JavaScript
var interval = null;
(function() {
function log(line) {
window.startTime = window.startTime || new Date().getTime();
var now = new Date().getTime(),
since = now - window.startTime;
console.log(since + '\n' + line);
}
/**
* Setup an empty grid using the x/y co-ords.
* @param x int - width of grid.
* @param y int - height of grid.
* @return array - grid of zero'd multi-dimension arrays.
*/
function buildEmptyGrid(x, y) {
// The empty array to store everything in.
var grid = [];
// Loop through each row.
for(var idx_y = 0; idx_y < y; idx_y++) {
// Loop through cells.
for(var idx_x = 0; idx_x < x; idx_x++) {
// Create the new row if it does not already exist.
if (!grid[idx_y]) {
grid[idx_y] = [];
}
// Add an empty cell.
grid[idx_y][idx_x] = 0;
}
}
// Return the grid.
return grid;
}
/**
* Output the grid using 0's and 1's.
* @param grid array - The grid to output.
* @return string - Grid of 0's and 1's representing the grid.
*/
function mapGrid(grid) {
// The empty variable to store everything in.
var output = '';
// Loop through each row in the grid.
for(var row in grid) {
// Loop through each cell in the row.
for(var cell in grid[row]) {
// Add the cell status to the output.
output += grid[row][cell];
}
// Add a new line after each row.
output += "\n";
}
// Return the output.
return output;
}
function calculateNeighbours(grid, w, h, x, y) {
var neighbours = (grid[y][x] == 0 ? -1 : 0);
for(var idx_y = -1; idx_y <= 1; idx_y++) {
for(var idx_x = -1; idx_x <= 1; idx_x++) {
if...