代码拉取完成,页面将自动刷新
/**
* Conway's Game of Life
* A "cellular automaton" zero-player mathematical game.
*
* Author: Leo Deng
* URL: https://github.com/myst729/game-of-life
*/
// Evolve the next generation of cells.
var onmessage = function(e) {
var cells = e.data;
var next = [];
var neighbors;
var my_mod = function(x,m) {
return (m+x) % m;
}
for(var i = 0, l = cells.length; i < l; i++) {
next[i] = [];
for(var j = 0, k = cells[i].length; j < k; j++) {
/*
if(i === 0 || j === 0 || i === l-1 || j === k-1) {
next[i][j] = 0; // shim cells
} else
*/
{
// Get the number of live neighbors (8 neighbors in total).
neighbors = cells[my_mod(i-1, l)][my_mod(j-1, k)] +
cells[my_mod(i-1, l)][my_mod(j , k)] +
cells[my_mod(i-1, l)][my_mod(j+1, k)] +
cells[my_mod(i , l)][my_mod(j-1, k)] +
cells[my_mod(i , l)][my_mod(j+1, k)] +
cells[my_mod(i+1, l)][my_mod(j-1, k)] +
cells[my_mod(i+1, l)][my_mod(j , k)] +
cells[my_mod(i+1, l)][my_mod(j+1, k)];
if(cells[i][j] === 0 && neighbors === 3) {
// Any dead cell with exactly 3 live neighbors becomes a live cell, as if by reproduction.
next[i][j] = 1;
} else if(cells[i][j] === 1 && neighbors > 1 && neighbors < 4) {
// Any live cell with 2 or 3 live neighbors lives on to the next generation.
next[i][j] = 1;
} else {
// Live cell dies, caused by under-population (fewer than 2 live neighbors) or overcrowding (more than 3 live neighbors).
// Dead cell remains dead.
next[i][j] = 0;
}
}
}
}
postMessage(next);
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。