- Author:
- Daniel Bali
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- Compo:
- classic
- Demo link:
- https://js1k.com/2013-spring/demo/1478
- Shortlink:
- https://js1k.com/1478
- Blog post:
- please update here!
- Bytes:
- 376
- Chars:
- 376
- Submission
W=c.width=c.height=520;S=64;m=[];m[W-1]=m[W]=m[W+1]=m[457]=m[392]=t=1;onkeydown=function(){t^=1};onmouseup=function(e){m[(e.clientY/8-1|0)*S+e.clientX/8-1|0]=1};setInterval("for(a.clearRect(i=0,0,W,W);i<S*S;++i)a.fillRect(i%S*8,8*(i/S|0),m[i]*8,8);for(c=m.slice(i=0);!t&i<S*S;m[i]=c[i]?!(n>3|n<2):n==3,++i)n=c[i-S-1]+c[i-S]+c[i-S+1]+c[i-1]+c[i+1]+c[i+S-1]+c[i+S]+c[i+S+1]",99)
- Description
- Simple Conway's Game of Life implementation
- Click anywhere to add a cell
- Press any button to stop/start the simulation
- Base64 encoded
Vz1jLndpZHRoPWMuaGVpZ2h0PTUyMDtTPTY0O209W107bVtXLTFdPW1bV109bVtXKzFdPW1bNDU3XT1tWzM5Ml09dD0xO29ua2V5ZG93bj1mdW5jdGlvbigpe3RePTF9O29ubW91c2V1cD1mdW5jdGlvbihlKXttWyhlLmNsaWVudFkvOC0xfDApKlMrZS5jbGllbnRYLzgtMXwwXT0xfTtzZXRJbnRlcnZhbCgiZm9yKGEuY2xlYXJSZWN0KGk9MCwwLFcsVyk7aTxTKlM7KytpKWEuZmlsbFJlY3QoaSVTKjgsOCooaS9TfDApLG1baV0qOCw4KTtmb3IoYz1tLnNsaWNlKGk9MCk7IXQmaTxTKlM7bVtpXT1jW2ldPyEobj4zfG48Mik6bj09MywrK2kpbj1jW2ktUy0xXStjW2ktU10rY1tpLVMrMV0rY1tpLTFdK2NbaSsxXStjW2krUy0xXStjW2krU10rY1tpK1MrMV0iLDk5KQ==
- Original source
// Screen setup
c.width = 520; c.height = 520;
// Listeners
onkeydown = keydown;
addEventListener('mouseup', click);
// Loop
setInterval(simulate, 100);
// Variables
var size = 64;
var map = new Array(size*size);
var started = false;
// Initial cells
map[519] = map[520] = map[521] = map[457] = map[392] = 1;
// Draw map
function drawMap() {
a.clearRect(0,0,512,512);
for (i = 0; i<size; ++i) {
for (j = 0; j<size; ++j) {
if (map[i*size+j]) {
a.fillRect(j*8, i*8, 8, 8);
}
}
}
}
// Simulate world
function simulate() {
if (!started) return;
// Clone map
var clone = map.slice(0);
for(y = 0; y<size; ++y) {
for (x = 0; x<size; ++x) {
// Count neighbors
neighbors = 0;
for (i = -1; i <= 1; ++i) {
for (j = -1; j <= 1; ++j) {
if (!(i == 0 && j == 0) && y+i < size && y+i >= 0 && x+j < size && x+j >= 0) {
if (clone[(y+i)*size+x+j]) {
neighbors++;
}
}
}
}
// Modify cells
if (clone[y*size+x]) {
if (neighbors > 3 || neighbors < 2) {
map[y*size+x] = 0;
}
} else {
if (neighbors == 3) {
map[y*size+x] = 1;
}
}
}
}
drawMap();
}
// Add cell
function click(event) {
x = (event.clientX-8)/8|0;
y = (event.clientY-8)/8|0;
map[y*size+x] = 1;
drawMap();
}
// Change started
function keydown(event) {
started = !started;
}
// Draw initial map
drawMap();