Object.prototype.clone=function(){var d=this instanceof Array?[]:{},e;for(e in this)this.hasOwnProperty(e)&&(d[e]=this[e]&&"object"===typeof this[e]?this[e].clone():this[e]);return d};
var Piece=function(d,e,c,h){d=d||[[1]];e=e||"#000000";null===c&&(c=-1*d.length);null===h&&(h=d[0].length+1);this.getStructure=function(){return d.clone()};this.getTop=function(){return c};this.getLeft=function(){return h};this.getWidth=function(){return d[0].length};this.getHeight=function(){return d.length};this.getColor=function(){return e};this.drop=function(){c+=1};this.moveLeft=function(){h-=1};this.moveRight=function(){h+=1};this.setLeft=function(e){h=e};this.rotate=function(e){var a=Array(d[0].length),
b,c;for(b=0;b<a.length;b+=1){a[b]=Array(d.length);for(c=0;c<a[0].length;c+=1)"ccw"===e?a[b][c]=d[c][a.length-b-1]:"cw"===e&&(a[b][c]=d[a[0].length-c-1][b])}d=a}},Level=function(d,e,c,h){var k,a,b,i;if(null===d||void 0===d){d=[];for(b=0;b<c;b+=1){d[b]=[];for(i=0;i<e;i+=1)d[b].push(void 0)}}else e=d[0].length,c=d.length;k=function(){var b=Math.floor(e/2);switch(Math.floor(7*Math.random())){case 0:return new Piece([[1],[1],[1],[1]],"#8FEBE9",null,b);case 1:return new Piece([[0,1,1],[1,1,0]],"#3ED936",
null,b-2);case 2:return new Piece([[1,1,0],[0,1,1]],"#F23E22",null,b-2);case 3:return new Piece([[1,1,1],[0,1,0]],"#B21DCC",null,b-2);case 4:return new Piece([[0,1],[0,1],[1,1]],"#194CC2",null,b-1);case 5:return new Piece([[1,0],[1,0],[1,1]],"#F2B022",null,b-1);case 6:return new Piece([[1,1],[1,1]],"#F2ED50",null,b-1)}};a=h||k();this.getWidth=function(){return d[0].length};this.getHeight=function(){return d.length};this.getStructure=function(){return d.clone()};this.moveActiveLeft=function(){this.isObstructedLeft()||
a.moveLeft()};this.moveActiveRight=function(){this.isObstructedRight()||a.moveRight()};this.dropActive=function(){this.isObstructedBottom()||a.drop()};this.rotateActive=function(){a.rotate("cw");a.getLeft()+a.getWidth()>=e&&a.setLeft(e-a.getWidth())};this.getActive=function(){return a.clone()};this.isObstructedLeft=function(){if(0>=a.getLeft())return!0;var e=a.getStructure(),g,c;for(b=0;b<a.getHeight();b+=1)for(c=0;c<a.getWidth();c+=1)if(1===e[b][c]){g=a.getTop();0>g&&(g=0);if(void 0!==d[g+b][a.getLeft()+
c-1])return!0;break}return!1};this.isObstructedRight=function(){if(a.getLeft()+a.getWidth()>=e)return!0;var c=a.getStructure(),g,f;for(b=0;b<a.getHeight();b+=1)for(f=a.getWidth()-1;0<=f;f-=1)if(1===c[b][f]){g=a.getTop();0>g&&(g=0);if(void 0!==d[g+b][a.getLeft()+f+1])return!0;break}return!1};this.isObstructedBottom=function(){var e=a.getStructure(),g,f;for(b=a.getHeight()-1;0<=b;b-=1){g=1+b+a.getTop();if(g>=c)return!0;if(0>g)break;for(f=0;f<=a.getWidth()-1;f+=1)if(1===e[b][f]&&void 0!==d[g][a.getLeft()+
f])return!0}return!1};this.placeActive=function(){var e=a.getStructure(),c;for(b=0;b<a.getHeight();b+=1)for(c=0;c<a.getWidth();c+=1)1===e[b][c]&&0<=b+a.getTop()&&(d[b+a.getTop()][c+a.getLeft()]=a.getColor());a=k()};this.getFullRows=function(){var a=[],g;for(b=0;b<c;b+=1)for(g=0;g<e&&!(void 0===d[b][g]);g+=1)g===e-1&&a.push(b);return a};this.clearRows=function(a){var c=[],f;for(b=0;b<a.length;b+=1){d.splice(a[b],1);for(f=0;f<e;f+=1)c.push(void 0);d.unshift(c)}}},Game=function(d,e,c){var h=0,k=0,a=
"play",b=[],i=!0,l=0,g=new Image,f,q,r,m,n,o,j,p,d=d||document.getElementById("level"),e=e||new Level(null,8,16);d.width=20*e.getWidth();d.height=20*e.getHeight();c=c||0;g.src="block.png";window.addEventListener("keydown",function(b){switch(b.keyCode){case 37:"play"===a&&e.moveActiveLeft();break;case 38:"play"===a&&e.rotateActive();break;case 39:"play"===a&&e.moveActiveRight();break;case 40:"play"===a&&e.dropActive();break;case 19:f();break;case 80:f()}},!1);q=function(){var b,a,c=e.getActive(),d,
f,h;d=c.getStructure();f=c.getLeft();h=c.getTop();j.fillStyle=c.getColor();for(b=0;b<c.getHeight();b+=1)for(a=0;a<c.getWidth();a+=1)1===d[b][a]&&(j.fillRect(20*(a+f),20*(b+h),20,20),j.drawImage(g,20*(a+f),20*(b+h)))};r=function(){var a,c,d,f=e.getStructure();i=i?!1:!0;for(a=0;a<e.getHeight();a+=1){d=!1;for(c=0;c<b.length;c+=1)a===b[c]&&(d=!0);for(c=0;c<e.getWidth();c+=1)if(void 0!==f[a][c])j.fillStyle=d&&i?"#ffffff":f[a][c],j.fillRect(20*c,20*a,20,20),(d&&!i||!d)&&j.drawImage(g,20*c,20*a)}};m=function(){var d,
f;if(e.isObstructedBottom()){d=e.getActive();if(0>d.getTop()&&(a="stop",p=document.getElementById("gameover")))p.style.display="block";e.placeActive()}else e.dropActive();k+=b.length;h=Math.floor(k/10);e.clearRows(b);d=document.getElementById("score");f=document.getElementById("level-number");if(d)d.innerHTML=c;if(f)f.innerHTML=h;b=e.getFullRows();switch(b.length){case 1:c+=40*(h+1);break;case 2:c+=100*(h+1);break;case 3:c+=300*(h+1);break;case 4:c+=1200*(h+1)}"stop"!==a&&setTimeout(m,500-15*h)};
n=function(){j.clearRect(0,0,20*e.getWidth(),20*e.getHeight());j.fillStyle="rgb(0, 31, 0)";q();r();l+=1;"stop"!==a&&setTimeout(n,32)};o=function(){var b=document.getElementById("fps");if(b)b.innerHTML=l;"stop"!==a&&(l=0,setTimeout(o,1E3))};f=this.pause=function(){"play"===a?a="stop":(a="play",n(),m(),o())};if(d.getContext){j=d.getContext("2d");if(p=document.getElementById("gameover"))p.style.display="none";n();o();m()}};
