- Author:
- Simon Key
- Twitter:
- @
- GitHub:
- Facebook:
- Google+:
- +
- Reddit:
- /r/
- Pouet:
- Website:
- Compo:
- classic
- Demo link:
- https://js1k.com/2010-first/demo/507
- Shortlink:
- https://js1k.com/507
- Blog post:
- please update here!
- Bytes:
- 1024
- Chars:
- 1024
- Submission
var a=document,c=a.getElementById("c"),h=c.getContext("2d"),m=1,n=Math.floor,o=Math.random,q=[],r=[],t=[],u,v,w;c.width=c.height=640;c.onclick=function(d){u=d.pageX;v=d.pageY;w=c;do{u-=w.offsetLeft;v-=w.offsetTop}while(w=w.offsetParent);i=n(u/8)+n(v/8)*80;q[i]=q[i]?0:1;y(i,q[i])};function y(d,A,x){x=x||q;(x[d]=A)?h.fillStyle="#0a0":h.fillStyle="#ddd";h.fillRect(8*r[d][0],8*r[d][1],8,8)}
function z(){w=[];for(k in q){var d=k;u=0;for(j in t[d])u+=q[t[d][j]];l=u;d=0;(l==3||q[k]&&l==2)&&d++;w[k]=d;d==q[k]||y(k,d,w)}q=w;m&&setTimeout(z,1);m=1}b=["Glider","Random","Stop","Start"];f=[function(){u=n(6400*o());g=[0,-81,78,79,80];for(s in g)y(u+g[s],1)},function(){for(u=0;u<n(q.length*0.3);u++)y(n(6400*o()),1)},function(){m=0},z];a.body.appendChild(a.createElement("br"));for(p in b){e=a.createElement("input");e.type="button";e.value=b[p];e.onclick=f[p];a.body.appendChild(e)}
for(w=0;w<6400;w++){i=w%80;j=(w-i)/80;r[w]=[i,j];t[w]=[];for(u=-1;u<2;u++)for(v=-1;v<2;v++)(u||v)&&t[w].push((i+u+80)%80+(j+v+80)%80*80);y(w,0)}
- Description
- Conway's Game Of Life, the classic cellular automaton.
- Base64 encoded
dmFyIGE9ZG9jdW1lbnQsYz1hLmdldEVsZW1lbnRCeUlkKCJjIiksaD1jLmdldENvbnRleHQoIjJkIiksbT0xLG49TWF0aC5mbG9vcixvPU1hdGgucmFuZG9tLHE9W10scj1bXSx0PVtdLHUsdix3O2Mud2lkdG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ODA7clt3XT1baSxqXTt0W3ddPVtdO2Zvcih1PS0xO3U8Mjt1KyspZm9yKHY9LTE7djwyO3YrKykodXx8dikmJnRbd10ucHVzaCgoaSt1KzgwKSU4MCsoait2KzgwKSU4MCo4MCk7eSh3LDApfQ==
- Original source
var d=document,c = d.getElementById('c'),ca = c.getContext('2d'), r = 1,mf=Math.floor,mr=Math.random,cells=[],coordinates=[],neighbourCells=[],x,y,z;
c.width=c.height=640;
c.onclick = function(e) {
x = e.pageX;y = e.pageY;z = c;
do { x -= z.offsetLeft; y -= z.offsetTop; } while (z = z.offsetParent);
i = mf(x / 8) + mf(y / 8) * 80;
cells[i] = cells[i] ? 0 : 1;
draw(i,cells[i]);
}
function draw(l,v,cl) {
cl = cl || cells;
cl[l] = v;
v ? ca.fillStyle = '#0a0' : ca.fillStyle = '#ddd';
ca.fillRect(8*coordinates[l][0], 8*coordinates[l][1], 8, 8);
}
function countLive(i) {
x = 0;
for (j in neighbourCells[i]) { x += cells[neighbourCells[i][j]]; }
return x;
}
function run() {
z = [];
for (k in cells) {
l = countLive(k);
var b = 0;
(l ==3 || (cells[k] && l==2)) && b++;
z[k] = b;
(b == cells[k]) || draw(k,b,z);
}
cells = z;
r && setTimeout(run,1);
r = 1;
}
function stop() {
r = 0;
}
function random() {
for (x=0;x<mf(cells.length*0.3);x++) { draw(mf(6400*mr()),1);}
}
function glider() {
x = mf(6400*mr()),g = [0, -81, 78, 79, 80]
for (s in g) {
draw(x+g[s],1);
}
}
b = ['Glider','Random','Stop','Start']
f = [glider,random,stop,run]
d.body.appendChild(d.createElement('br'));
for (p in b) {
e = d.createElement('input');
e.type = 'button';
e.value = b[p];
e.onclick = f[p];
d.body.appendChild(e);
}
for (z=0; z<6400; z++) {
i = z%80;
j = (z-i)/80;
coordinates[z] = [i,j];
neighbourCells[z] = [];
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
(x || y) && neighbourCells[z].push( (i + x + 80) % 80 + ((j + y + 80) % 80) * 80);
}
}
draw(z,0);
}