-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameOfLife.js
1 lines (1 loc) · 8.92 KB
/
GameOfLife.js
1
(function(){var t,e,i,n,r,o,u,s,a,l,h,d,g,c,f,p,w,b,v,m,S,y,C,k,H,M,T,x,z,W,B,R,G;g=function(t){return 0===t?0:1},f=function(t,e,i){return e>=0&&i>=0&&e<t.length&&i<t[0].length?t[e][i]++:void 0},p=function(t,e,i){return t[(e+t.length)%t.length][(i+t[1].length)%t[1].length]++},c=p,G=function(t,e,i){return e>=0&&i>=0&&e<t.length&&i<t[0].length?t[e][i]=0:void 0},T=function(t,e,i){return"rgb("+t+","+e+","+i+")"},x=function(t,e,i,n){return"rgba("+t+","+e+","+i+","+n+")"},e=function(t,e,i){var n,r,o,u,s,a,l,h;switch(u=Math.floor(6*t),r=6*t-u,s=i*(1-e),a=i*(1-r*e),h=i*(1-(1-r)*e),u%6){case 0:l=i,o=h,n=s;break;case 1:l=a,o=i,n=s;break;case 2:l=s,o=i,n=h;break;case 3:l=s,o=a,n=i;break;case 4:l=h,o=s,n=i;break;case 5:l=i,o=s,n=a}return T(Math.floor(255*l),Math.floor(255*o),Math.floor(255*n))},t=jQuery,W=[[!1,!1,!1,!0,!1,!1,!1,!1,!1],[!1,!1,!0,!0,!1,!1,!1,!1,!1]],k=[[!1,!1,!1,!1,!1],[!1,!0,!0,!0,!1],[!1,!0,!1,!0,!1],[!1,!0,!0,!0,!1],[!1,!1,!1,!1,!1]],R=function(e){return t(e).css({visibility:"visible",opacity:0}),t(e).animate({opacity:1})},B=function(e){return t(e).animate({opacity:0},function(){return t(e).css({visibility:"hidden"})})},this.help=function(){return z.helpShown=!z.helpShown,z.paused=z.helpShown,z.helpShown?R(".helpBox"):B(".helpBox")},this.toggleRule=function(t,e){return W[t][e]=!W[t][e]},this.toggleNeighborhood=function(t,e){return k[t][e]=!k[t][e]},this.moreCells=function(){return z.gridSpacing*=.9,n*=.9,z.gridWidth=u.width/gridSpacing,z.gridHeight=u.width/gridSpacing,d()},this.fewerCells=function(){return z.gridSpacing/=.9,n/=.9,z.gridWidth=u.width/gridSpacing,z.gridHeight=u.width/gridSpacing},S={x:0,y:0,down:[!1,!1,!1,!1,!1,!1,!1,!1,!1],getX:function(){return this.x},getY:function(){return this.y},getButtonX:function(){return Math.floor(this.x/o)},getButtonY:function(){return Math.floor(this.y/r)},getGridX:function(){return Math.floor(this.x/gridSpacing)},getGridY:function(){return Math.floor(this.y/gridSpacing)},distanceTo:function(t,e){return Math.sqrt(Math.pow(t-this.x,2)+Math.pow(e-this.y,2))}},b=function(t){var e,i,n,r,o,u,s,a,l,h,d,g,f,p;if(S.x=t.pageX,S.y=t.pageY,e=S.getGridX(),i=S.getGridY(),S.down[1])for(requestAnimationFrame(z.drawImmediately),z.userHasCreatedCells||(z.userHasCreatedCells=!0,B("#tutorialCreateCells"),z.userHasChangedRules||R("#tutorialChangeRules")),n=o=a=e-z.brushSize,l=e+1+z.brushSize;l>=a?l>o:o>l;n=l>=a?++o:--o)for(r=u=h=i-z.brushSize,d=i+1+z.brushSize;d>=h?d>u:u>d;r=d>=h?++u:--u)c(ages,n,r);if(S.down[3]){for(z.userHasDeletedCells=!0,requestAnimationFrame(z.drawImmediately),p=[],n=s=g=e-z.brushSize,f=e+1+z.brushSize;f>=g?f>s:s>f;n=f>=g?++s:--s)p.push(function(){var t,e,o,u;for(u=[],r=t=e=i-z.brushSize,o=i+1+z.brushSize;o>=e?o>t:t>o;r=o>=e?++t:--t)u.push(G(ages,n,r));return u}());return p}},d=function(){return z.ages=H()},t(window).resize(function(){var t,e;return u.width=window.innerWidth,u.height=window.innerHeight,e=50,t=u.height/9,z.gridWidth=u.width/gridSpacing,z.gridHeight=u.width/gridSpacing,d()}),t(function(){var e,i,n,r,o,u,s,a,l,h,d,g,c,f,p,w,v,y,C;for(t("#ruleTable").append('<tr>\n <th title="This column determines how dead cells can come to life" style="height:30px;" class="tableHeader">Dead</th>\n <th title="This column determines how live cells can stay alive" style="height:30px;" class="tableHeader">Alive</th>\n</tr>'),r=a=0,p=9;p>=0?p>a:a>p;r=p>=0?++a:--a)i="ruleButton",W[0][r]&&(i+=" down"),n="ruleButton",W[1][r]&&(n+=" down"),t("#ruleTable").append('<tr>\n <td title="When this button is illuminated, dead cells with '+r+" neighbors will come to life.\nWhen this button is dark, dead cells with "+r+' neighbors will stay dead."\ntype="button" class=" '+i+' " onclick="toggleRule(0, '+r+')">'+r+'</td>\n<td title="When this button is illuminated, live cells with '+r+" neighbors will stay alive.\nWhen this button is dark, live cells with "+r+' neighbors will die."\ntype="button" class=" '+n+' " onclick="toggleRule(1, '+r+')">'+r+"</td>\n</tr>");for(o=t("#neighborhoodOptionsTable>tbody"),w=function(){y=[];for(var t=0;m>=0?m>t:t>m;m>=0?t++:t--)y.push(t);return y}.apply(this).reverse(),l=0,g=w.length;g>l;l++){for(s=w[l],o.append("<tr>"),v=function(){C=[];for(var t=0;m>=0?m>t:t>m;m>=0?t++:t--)C.push(t);return C}.apply(this).reverse(),d=0,c=v.length;c>d;d++)u=v[d],e="neighborhoodButton",k[u][s]&&(e+=" down"),o.append('<td type="button" class=" '+e+' " onclick="toggleNeighborhood( '+u+","+s+' )"></td>');o.append("</tr>")}return t(".ruleButton").click(function(){var t;return z.userHasChangedRules?void 0:(z.userHasChangedRules=!0,B("#tutorialChangeRules"),t=1e3,setTimeout(function(){return R("#tutorialLeftCol")},t),setTimeout(function(){return B("#tutorialLeftCol")},t+=4e3),setTimeout(function(){return R("#tutorialRightCol")},t),setTimeout(function(){return B("#tutorialRightCol")},t+=4e3),setTimeout(function(){return R("#tutorialRow")},t),setTimeout(function(){return B("#tutorialRow")},t+=4e3),setTimeout(function(){return R("#tutorialMouseOver")},t),setTimeout(function(){return B("#tutorialMouseOver")},t+=4e3))}),t("#speedOptionsMinButton").click(function(){return t("#speedOptionsDiv").slideToggle()}),t("#gridSizeOptionsMinButton").click(function(){return t("#gridSizeOptionsDiv").slideToggle()}),t("#brushOptionsMinButton").click(function(){return t("#brushOptionsDiv").slideToggle()}),t("#neighborhoodOptionsMinButton").click(function(){return t("#neighborhoodOptionsDiv").slideToggle()}),t("#myCanvas").mousedown(function(t){return S.down[t.which]=!0,b(t)}),t("#myCanvas").mouseup(function(t){return S.down[t.which]=!1,z.help?(z.help=!1,z.paused=!1):void 0}),t("#myCanvas").mousemove(function(t){return b(t)}),t("#pauseButton").click(function(){return z.paused=!z.paused,t(this).html(z.paused?"Play":"Pause")}),t("#1x1").click(function(){return z.brushSize=0}),t("#2x2").click(function(){return z.brushSize=.5}),t("#3x3").click(function(){return z.brushSize=1}),t("#5x5").click(function(){return z.brushSize=2}),t("#9x9").click(function(){return z.brushSize=4}),t(".neighborhoodButton,.minButton,.ruleButton").addClass("toggle"),t(".toggle").click(function(){return t(this).toggleClass("down")}),t(".radio").click(function(){return t(this).siblings().removeClass("down"),t(this).addClass("down")}),t("#5x5").click(),t("#fast").click()}),z="undefined"!=typeof exports&&null!==exports?exports:this,u=document.getElementById("myCanvas"),a=u.getContext("2d"),this.setDelay=function(t){return z.delay=t},i=function(){var t;return t=1e5,a.fillRect(-t,-t,2*t,2*t)},v=function(){var t,e,i,n,r;for(r=[],t=i=0,n=z.gridWidth;n>=0?n>i:i>n;t=n>=0?++i:--i)r.push(function(){var t,i,n;for(n=[],e=t=0,i=z.gridHeight;i>=0?i>t:t>i;e=i>=0?++t:--t)n.push(0);return n}());return r},this.clearGrid=function(){var t,e,i,n,r;for(r=[],t=i=0,n=z.gridWidth;n>=0?n>i:i>n;t=n>=0?++i:--i)r.push(function(){var i,n,r;for(r=[],e=i=0,n=z.gridHeight;n>=0?n>i:i>n;e=n>=0?++i:--i)r.push(z.ages[t][e]=0);return r}());return r},M=function(t,e){var i,n,r,o;for(console.log(z.gridWidth),o=[],i=r=0;t>=0?t>r:r>t;i=t>=0?++r:--r)o.push(function(){var t,i;for(i=[],n=t=0;e>=0?e>t:t>e;n=e>=0?++t:--t)i.push(Math.floor(Math.random()+.4));return i}());return o},H=function(){return M(z.gridWidth,z.gridHeight)},this.randomizeGrid=function(){var t,e,i,n,r;for(r=[],t=i=0,n=z.gridWidth;n>=0?n>i:i>n;t=n>=0?++i:--i)r.push(function(){var i,n,r;for(r=[],e=i=0,n=z.gridHeight;n>=0?n>i:i>n;e=n>=0?++i:--i)r.push(z.ages[t][e]=Math.floor(Math.random()+.4));return r}());return r},s=function(){var t,e,i,n,r,o,u,s,a,l,h,d,f,p;for(i=v(),n=o=0,h=z.gridWidth;h>=0?h>o:o>h;n=h>=0?++o:--o)for(r=u=0,d=z.gridHeight;d>=0?d>u:u>d;r=d>=0?++u:--u)if(0!==z.ages[n][r])for(t=s=0;m>=0?m>s:s>m;t=m>=0?++s:--s)for(e=a=0;m>=0?m>a:a>m;e=m>=0?++a:--a)k[t][e]&&c(i,n+t-2,r+e-2);for(p=[],n=l=0,f=z.gridWidth;f>=0?f>l:l>f;n=f>=0?++l:--l)p.push(function(){var t,e,o;for(o=[],r=t=0,e=z.gridHeight;e>=0?e>t:t>e;r=e>=0?++t:--t)o.push(W[g(z.ages[n][r])][i[n][r]]?z.ages[n][r]++:z.ages[n][r]=0);return o}());return p},h=function(){var t,i,r,o,u,s,l,h;for(r=(new Date).getTime()/1e4,h=[],o=s=0,l=z.gridWidth;l>=0?l>s:s>l;o=l>=0?++s:--s)h.push(function(){var s,l,h;for(h=[],u=s=0,l=z.gridHeight;l>=0?l>s:s>l;u=l>=0?++s:--s)t=z.ages[o][u],0!==t?(i=Math.sqrt(t),i*=.2,a.fillStyle=e((i+r)%1,1,1),h.push(a.fillRect(z.gridSpacing*o,z.gridSpacing*u,z.gridSpacing-n,z.gridSpacing-n))):h.push(void 0);return h}());return h},w=0,l=function(){var t;return a.fillStyle=T(0,0,0),i(),h(),0!==z.ages[S.getGridX()][S.getGridY()]&&(a.fillStyle=x(255,255,255,.7),a.fillRect(S.getGridX()*z.gridSpacing,S.getGridY()*z.gridSpacing,z.gridSpacing-n,z.gridSpacing-n)),t=(new Date).getTime(),w+z.delay<t&&!z.paused&&(s(),w=t),setTimeout(l)},z.drawImmediately=function(){return h()},z.delay=0,z.userHasCreatedCells=!1,z.userHasChangedRules=!1,z.userHasDeletedCells=!1,setTimeout(function(){return R("#tutorialCreateCells")},1e3),u.width=window.innerWidth,u.height=window.innerHeight,o=50,r=u.height/9,z.gridSpacing=15,n=.2*z.gridSpacing,z.gridWidth=u.width/z.gridSpacing,z.gridHeight=u.width/z.gridSpacing,y=0,C=0,m=5,z.ages=H(),z.brushSize=2,z.helpShown=!1,z.paused=!1,a.font="20px Georgia",l()}).call(this);