Array.prototype.remove=function(c,b){var a=this.slice((b||c)+1||this.length);this.length=c<0?this.length+c:c;return this.push.apply(this,a)};CanvasRenderingContext2D.prototype.fillRoundedRect=function(b,e,d,a,c){this.save();this.beginPath();this.moveTo(c,0);this.lineTo(d-c,0);this.quadraticCurveTo(d,0,d,c);this.lineTo(d,a-c);this.quadraticCurveTo(d,a,d-c,a);this.lineTo(c,a);this.quadraticCurveTo(0,a,0,a-c);this.lineTo(0,c);this.quadraticCurveTo(0,0,c,0);this.closePath();this.clip();this.fillRect(0,0,d,a);this.restore()};function Game(e,a,d,c,b){this.canvas=e.get(0);this.context=this.canvas.getContext("2d");this.board=new Board(e.attr("width")-(d*2),e.attr("height")-(d*2),d,b,c);this.scoreboard=a;this.streak=5;this.new_pieces=5;this.empty_board_multiply=3;this.total_chances=40;this.reset()}Game.prototype.reset=function(){this.game_pieces=[{type:RedGamePiece,chance:12},{type:GreenGamePiece,chance:12},{type:BlueGamePiece,chance:12},{type:WildGamePiece,chance:3},{type:RadioactiveGamePiece,chance:1}];this.active=false;this.is_selected=false;this.is_moving=false;this.score={pieces:0,score:0,clears:0,moves:0};this.last_score={pieces:0,score:0};this.board.reset()};Game.prototype.start=function(){this.active=true};Game.prototype.addRandomGamePieces=function(b){var g=this.board.getAvailableCellCoords();var h=0;var e=this.new_pieces*(this.board.anyGamePieces()?1:this.empty_board_multiply);for(var f=0;f<this.game_pieces.length;f++){h+=this.game_pieces[f].chance}for(var f=0;f<e&&g.length>0;f++){var a=Math.floor(Math.random()*h);var l;var k=0,d=0;do{k+=this.game_pieces[d].chance;l=this.game_pieces[d++].type}while(k<=a);a=Math.floor(Math.random()*g.length);this.addGamePiece(new l(),g[a]);g.remove(a)}if(g.length==0){this.active=false;return}};Game.prototype.addGamePiece=function(b,a){this.board.addGamePiece(b,a)};Game.prototype.activateGamePiece=function(c){var d=this.total_chances;var a=1;for(var b=0;b<this.game_pieces.length;b++){if(this.game_pieces[b].type===c.type){return false}else{if(this.game_pieces[b].type===WildGamePiece||this.game_pieces[b].type===RadioactiveGamePiece){d-=this.game_pieces[b].chance}else{a++}}}for(var b=0;b<this.game_pieces.length;b++){if(this.game_pieces[b].type!==WildGamePiece&&this.game_pieces[b].type!==RadioactiveGamePiece){this.game_pieces[b].chance=Math.floor(d/a--);d-=this.game_pieces[b].chance}}c.chance=d;this.game_pieces.push(c);return true};Game.prototype.isGamePieceActive=function(b){for(var a=0;a<this.game_pieces.length;a++){if(this.game_pieces[a].type===b){return true}}return false};Game.prototype.setSelection=function(b){if(this.active&&!this.moving){var a=this.board.getCoords(b,this.canvas);if(a!=null){if(this.board.hasGamePiece(a)){this.is_selected=this.board.setGamePieceSelected(a)}else{if(this.is_selected){this.is_moving=true;if(this.board.moveGamePiece(a)){this.last_score.pieces=this.board.getStreaks(this.streak);this.last_score.score=this.calculateScore(this.last_score.pieces);if(this.last_score.pieces==0){this.addRandomGamePieces();this.last_score.pieces+=this.board.getStreaks(this.streak);this.last_score.score=this.calculateScore(this.last_score.pieces)}else{if(!this.board.anyGamePieces()){this.score.clears++;this.addRandomGamePieces();this.last_score.pieces+=this.board.getStreaks(this.streak);this.last_score.score=5*this.calculateScore(this.last_score.pieces)}}this.score.pieces+=this.last_score.pieces;this.score.score+=this.last_score.score;this.score.moves++;if(this.score.pieces>=100&&!this.isGamePieceActive(YellowGamePiece)){this.activateGamePiece({type:YellowGamePiece})}}this.is_selected=false;this.is_moving=false}}}}};Game.prototype.calculateScore=function(a){return Math.round(Math.pow(a,2))};Game.prototype.checkMove=function(b){if(this.active&&!this.is_moving&&this.is_selected){var a=this.board.getCoords(b,this.canvas);if(a!=null){return this.board.getPath(this.board.getCoords(b,this.canvas))}}return null};Game.prototype.render=function(){this.board.clearRender(this.context);this.board.render(this.context);var a='<strong>Total Score:</strong> <span class="score">'+this.score.score+" ("+this.score.pieces+" pcs)</span><br />";a+='<strong>Total Clears:</strong> <span class="clears">'+this.score.clears+"</span><br />";a+='<strong>Total Moves:</strong> <span class="moves">'+this.score.moves+"</span><br />";a+='<strong>Last Score:</strong> <span class="last_score">'+this.last_score.score+" ("+this.last_score.pieces+" pcs)</span>";this.scoreboard.html(a)};Game.prototype.renderBoardItem=function(a){this.board.renderItem(this.context,a)};function Board(b,a,e,c,d){this.width=b;this.height=a;this.padding=e;this.rounded_corners=28;this.rows=c;this.cols=d;this.cellwidth=Math.floor(b/d);this.cellheight=Math.floor(a/c);this.cellpadding=1;this.reset()}Board.prototype.reset=function(){this.cells=[];for(var a=0;a<this.cols;a++){this.cells[a]=[];for(var b=0;b<this.rows;b++){this.cells[a][b]=new Cell()}}};Board.prototype.rowCount=function(){return this.cells.length};Board.prototype.colCount=function(){return this.cells.length>0?this.cells[0].length:0};Board.prototype.getAvailableCellCoords=function(){var b=[];for(var a=0;a<this.cells.length;a++){for(var c=0;c<this.cells[a].length;c++){if(!this.cells[a][c].hasGamePiece()){b.push({x:a,y:c})}}}return b};Board.prototype.getCoords=function(c,a){var b={x:Math.floor((c.pageX-a.offsetLeft-this.padding)/this.cellwidth),y:Math.floor((c.pageY-a.offsetTop-this.padding)/this.cellheight)};return this.isValidCoords(b)?b:null};Board.prototype.isValidCell=function(a,b){return(a>=0&&a<this.cells.length&&b>=0&&b<this.cells[a].length)};Board.prototype.isValidCoords=function(a){return this.isValidCell(a.x,a.y)};Board.prototype.canAddGamePiece=function(a){return(this.isValidCoords(a)&&!this.cells[a.x][a.y].hasGamePiece())};Board.prototype.addGamePiece=function(b,a){this.cells[a.x][a.y].setGamePiece(b)};Board.prototype.getPath=function(a){var e=this.getSelectedGamePieceCoords();if(!this.isValidCell(e.x,e.y)||!this.isValidCell(a.x,a.y)){return null}var c=new Path(e,a);if(!c.hasReachedEnd()){do{var d=c.getLastUsedLink().getCoords();var b={x:d.x,y:d.y-1};if(this.isValidCoords(b)&&!this.hasGamePiece(b)&&!c.isLinked(b)){c.addPossibleLink(b)}b={x:d.x+1,y:d.y};if(this.isValidCoords(b)&&!this.hasGamePiece(b)&&!c.isLinked(b)){c.addPossibleLink(b)}b={x:d.x,y:d.y+1};if(this.isValidCoords(b)&&!this.hasGamePiece(b)&&!c.isLinked(b)){c.addPossibleLink(b)}b={x:d.x-1,y:d.y};if(this.isValidCoords(b)&&!this.hasGamePiece(b)&&!c.isLinked(b)){c.addPossibleLink(b)}}while(c.useBestPossibleLink()&&!c.hasReachedEnd())}return c};Board.prototype.anyGamePieces=function(){for(var a=0;a<this.cells.length;a++){for(var b=0;b<this.cells[a].length;b++){if(this.cells[a][b].hasGamePiece()){return true}}}return false};Board.prototype.clearRender=function(a){a.clearRect(0,0,this.width+(this.padding*2),this.height+(this.padding*2))};Board.prototype.render=function(e){var d=e.createLinearGradient(0,0,this.width+(this.padding*2),this.height+(this.padding*2));d.addColorStop(0,"rgb(224, 224, 224)");d.addColorStop(1,"rgb(192, 192, 192)");e.fillStyle=d;e.fillRoundedRect(0,0,this.width+(this.padding*2),this.height+(this.padding*2),this.rounded_corners);for(var a=0;a<this.cells.length;a++){for(var f=0;f<this.cells[a].length;f++){this.cells[a][f].render(e,this.padding+(this.cellwidth*a)+this.cellpadding,this.padding+(this.cellheight*f)+this.cellpadding,this.cellwidth-(this.cellpadding*2),this.cellheight-(this.cellpadding*2))}}var b=[];for(var a=0;a<this.cells.length;a++){for(var f=0;f<this.cells[a].length;f++){if(!this.cells[a][f].renderGamePiece(e,this.padding+(this.cellwidth*a)+this.cellpadding,this.padding+(this.cellheight*f)+this.cellpadding,this.cellwidth-(this.cellpadding*2),this.cellheight-(this.cellpadding*2))){b.push({cell:this.cells[a][f],x:this.padding+(this.cellwidth*a)+this.cellpadding,y:this.padding+(this.cellheight*f)+this.cellpadding,width:this.cellwidth-(this.cellpadding*2),height:this.cellheight-(this.cellpadding*2)})}}}for(var c=0;c<b.length;c++){b[c].cell.renderGamePieceScaled(e,b[c].x,b[c].y,b[c].width,b[c].height)}};Board.prototype.renderItem=function(a,b){b.render(a,this.padding,this.cellwidth,this.cellheight,this.cellpadding+this.padding)};Board.prototype.hasGamePiece=function(a){return this.cells[a.x][a.y].hasGamePiece()};Board.prototype.setGamePieceSelected=function(b){if(this.cells[b.x][b.y].isGamePieceSelected()){this.cells[b.x][b.y].setGamePieceSelected(false);return false}else{for(var a=0;a<this.cells.length;a++){for(var c=0;c<this.cells[a].length;c++){if(this.cells[a][c].hasGamePiece()){if(b.x==a&&b.y==c){this.cells[a][c].setGamePieceSelected(true)}else{this.cells[a][c].setGamePieceSelected(false)}}}}return true}};Board.prototype.moveGamePiece=function(b){var c=this.getPath(b);var a=this.getSelectedGamePieceCoords();this.cells[a.x][a.y].setGamePieceSelected(false);if(c.hasReachedEnd()){this.cells[b.x][b.y].setGamePiece(this.cells[a.x][a.y].removeGamePiece());return true}return false};Board.prototype.getSelectedGamePieceCoords=function(){for(var a=0;a<this.cells.length;a++){for(var b=0;b<this.cells[a].length;b++){if(this.cells[a][b].hasGamePiece()&&this.cells[a][b].isGamePieceSelected()){return{x:a,y:b}}}}return null};Board.prototype.getStreaks=function(b){var h;var e;for(var a=0;a<this.cells.length;a++){h={type:null,streak:0,radioactive:false};e={streak:0,radioactive:false};for(var g=0;g<this.cells[a].length&&g+(b-(h.streak+1))<this.cells[a].length;g++){this.adjustStreak(h,e,a,g);this.useStreakToRemoveGamePieces(h,b,a,g,0,1)}}for(var g=0;g<this.cells[0].length;g++){h={type:null,streak:0,radioactive:false};e={streak:0,radioactive:false};for(var a=0;a<this.cells.length&&a+(b-(h.streak+1))<this.cells.length;a++){this.adjustStreak(h,e,a,g);this.useStreakToRemoveGamePieces(h,b,a,g,1,0)}}for(var a=b-1;a<this.cells.length;a++){var g=0;h={type:null,streak:0,radioactive:false};e={streak:0,radioactive:false};for(var c=a,d=g;c>=0&&c-(b-(h.streak+1))>=0&&d<this.cells[a].length&&d+(b-(h.streak+1))<this.cells[a].length;c--,d++){this.adjustStreak(h,e,c,d);this.useStreakToRemoveGamePieces(h,b,c,d,-1,1)}}for(var g=1;g<this.cells[this.cells.length-1].length;g++){var a=this.cells.length-1;h={type:null,streak:0,radioactive:false};e={streak:0,radioactive:false};for(var c=a,d=g;c>=0&&c-(b-(h.streak+1))>=0&&d<this.cells[a].length&&d+(b-(h.streak+1))<this.cells[a].length;c--,d++){this.adjustStreak(h,e,c,d);this.useStreakToRemoveGamePieces(h,b,c,d,-1,1)}}for(var a=b-1;a<this.cells.length;a++){var g=this.cells[a].length-1;h={type:null,streak:0,radioactive:false};e={streak:0,radioactive:false};for(var c=a,d=g;c>=0&&c-(b-(h.streak+1))>=0&&d>=0&&d-(b-(h.streak+1))>=0;c--,d--){this.adjustStreak(h,e,c,d);this.useStreakToRemoveGamePieces(h,b,c,d,-1,-1)}}for(var g=1;g<this.cells[this.cells.length-1].length;g++){var a=this.cells.length-1;h={type:null,streak:0,radioactive:false};e={streak:0,radioactive:false};for(var c=a,d=g;c>=0&&c-(b-(h.streak+1))>=0&&d>=0&&d-(b-(h.streak+1))>=0;c--,d--){this.adjustStreak(h,e,c,d);this.useStreakToRemoveGamePieces(h,b,c,d,-1,-1)}}var f=0;for(var a=0;a<this.cells.length;a++){for(var g=0;g<this.cells[a].length;g++){if(this.cells[a][g].shouldRemoveGamePiece()){f++;this.cells[a][g].removeGamePiece()}}}return f};Board.prototype.adjustStreak=function(d,c,b,e){var a=this.cells[b][e];if(a.hasGamePiece()){if(a.getGamePieceType()=="Wild"||a.getGamePieceType()=="Radioactive"){c.streak++;d.streak++;if(a.getGamePieceType()=="Radioactive"){c.radioactive=true;d.radioactive=true}}else{if(a.getGamePieceType()==d.type){c.streak=0;c.radioactive=false;d.streak++}else{d.type=a.getGamePieceType();d.streak=1;d.radioactive=false;if(c.streak>0){d.streak+=c.streak;d.radioactive=c.radioactive}c.streak=0;c.radioactive=false}}}else{d.type=null;d.streak=0;d.radioactive=false;c.streak=0;c.radioactive=false}};Board.prototype.useStreakToRemoveGamePieces=function(g,b,a,h,e,c){if(g.streak>=b){for(var d=a-(e*(g.streak-1)),f=h-(c*(g.streak-1));((e==0)||(e>0&&d<=a)||(e<0&&d>=a))&&((c==0)||(c>0&&f<=h)||(c<0&&f>=h));d+=e,f+=c){this.cells[d][f].setToRemoveGamePiece()}if(g.radioactive){for(var d=0;d<this.cells.length;d++){for(var f=0;f<this.cells[a].length;f++){if(this.cells[d][f].hasGamePiece()&&this.cells[d][f].getGamePieceType()==g.type){this.cells[d][f].setToRemoveGamePiece()}}}}}};function Scoreboard(c,b,a){this.width=c;this.height=b;this.padding=20;this.offset_left=a}Scoreboard.prototype.clearRender=function(a){a.clearRect(this.offset_left,0,this.width,this.height)};Scoreboard.prototype.render=function(c,b,e){c.fillStyle="rgba(240, 240, 240, 1.0)";c.fillRect(this.offset_left,0,this.width,this.height);c.fillStyle="rgba(128, 128, 128, 1.0)";var a=this.offset_left+this.padding;var f=this.padding;var d=this.width-(this.padding*2);c.fillText("Score: "+e.score+" ("+e.pieces+" pcs OR "+(e.pieces>0?Math.round((e.score/e.pieces)*100)/100:0)+" per pc)",a,f,d);f+=20;c.fillText("Last Score: "+b.score+" ("+b.pieces+" pcs)",a,f,d)};function Path(b,a){this.start=b;this.end=a;this.possible_links=[];this.used_links=[];this.used_links.push(new PathLink(null,b,0,Math.abs(b.x-a.x)+Math.abs(b.y-a.y)))}Path.prototype.isLinked=function(b){for(var a=0;a<this.possible_links.length;a++){if(this.possible_links[a].getCoords().x==b.x&&this.possible_links[a].getCoords().y==b.y){return true}}for(var a=0;a<this.used_links.length;a++){if(this.used_links[a].getCoords().x==b.x&&this.used_links[a].getCoords().y==b.y){return true}}return false};Path.prototype.addPossibleLink=function(b){var a=this.getLastUsedLink();if(a!=null){this.possible_links.push(new PathLink(a,b,a.getG()+1,Math.abs(b.x-this.end.x)+Math.abs(b.y-this.end.y)))}};Path.prototype.getLastUsedLink=function(){return this.used_links.slice(-1).shift()};Path.prototype.hasPossibleLinks=function(){return Boolean(this.possible_links.length>0)};Path.prototype.useBestPossibleLink=function(){if(this.hasPossibleLinks()){var c=-1;for(var a=0;a<this.possible_links.length;a++){if(c==-1){c=a}else{var b=this.possible_links[c];if(this.possible_links[a].getF()<b.getF()||(this.possible_links[a].getF()==b.getF()&&this.possible_links[a].getH()<b.getH())){c=a}}}var b=this.possible_links[c];this.possible_links.remove(c);this.used_links.push(b);return true}return false};Path.prototype.hasReachedEnd=function(){var a=this.getLastUsedLink();if(a!=null){return(a.getCoords().x==this.end.x&&a.getCoords().y==this.end.y)}return false};Path.prototype.render=function(b,d,f,a,e){var c=this.getLastUsedLink();if(c!=null){b.fillStyle="rgba(255, 255, 255, 0.5)";while(c.hasParentLink()){b.fillRect((c.getCoords().x*f)+d,(c.getCoords().y*a)+d,f,a);c=c.getParentLink()}}};function PathLink(d,c,b,a){this.parent_link=d;this.coords=c;this.g=b;this.h=a}PathLink.prototype.hasParentLink=function(){return Boolean(this.parent_link!=null)};PathLink.prototype.getF=function(){return this.g+this.h};PathLink.prototype.getG=function(){return this.g};PathLink.prototype.getH=function(){return this.h};PathLink.prototype.getCoords=function(){return this.coords};PathLink.prototype.getParentLink=function(){return this.parent_link};function Cell(){this.game_piece=null;this.to_remove=false}Cell.prototype.hasGamePiece=function(){return Boolean(this.game_piece!=null)};Cell.prototype.isGamePieceSelected=function(){return this.game_piece.isSelected()};Cell.prototype.shouldRemoveGamePiece=function(){return this.to_remove};Cell.prototype.getGamePieceType=function(){return this.game_piece.getType()};Cell.prototype.setGamePiece=function(a){this.game_piece=a};Cell.prototype.setGamePieceSelected=function(a){this.game_piece.setSelected(a)};Cell.prototype.setToRemoveGamePiece=function(){this.to_remove=true};Cell.prototype.removeGamePiece=function(){game_piece=this.game_piece;this.game_piece=null;this.to_remove=false;return game_piece};Cell.prototype.render=function(c,b,e,d,a){c.drawImage($("#cell").get(0),b,e)};Cell.prototype.renderGamePiece=function(c,b,e,d,a){if(this.hasGamePiece()){return this.game_piece.render(c,b,e,d,a)}return false};Cell.prototype.renderGamePieceScaled=function(c,b,e,d,a){if(this.hasGamePiece()){this.game_piece.renderScaled(c,b,e,d,a)}};function GamePiece(){this.padding=0;this.selected=false;this.type=null;this.img_src=null}GamePiece.scale_size=0.25;GamePiece.shadow_blur=4;GamePiece.shadow_offset=2;GamePiece.scale=function(d,b){var a=b.width*GamePiece.scale_size;var c=b.height*GamePiece.scale_size;d.x-=a/2;d.y-=c/2;b.width+=a;b.height+=c};GamePiece.prototype.getType=function(){return this.type};GamePiece.prototype.render=function(c,b,e,d,a){if(this.selected){return false}this.renderImage(c,b+this.padding,e+this.padding,d-(this.padding*2),a-(this.padding*2));return true};GamePiece.prototype.renderImage=function(c,b,e,d,a){c.drawImage($("#"+this.img_src).get(0),b,e,d,a)};GamePiece.prototype.renderScaled=function(c,h,f,b,j){var g={x:h+this.padding,y:f+this.padding};var k={width:b-(this.padding*2),height:j-(this.padding*2)};GamePiece.scale(g,k);var e=g.x+(k.width/2)+GamePiece.shadow_offset;var d=g.y+(k.height/2)+GamePiece.shadow_offset;var a=(k.width<=k.height?k.width:k.height)/2;var i=c.createRadialGradient(e,d,a,e,d,a+GamePiece.shadow_blur);i.addColorStop(0,"rgba(0, 0, 0, 0.5)");i.addColorStop(1,"rgba(0, 0, 0, 0.0)");c.fillStyle=i;c.fillRect(g.x-GamePiece.shadow_blur+GamePiece.shadow_offset,g.y-GamePiece.shadow_blur+GamePiece.shadow_offset,k.width+(GamePiece.shadow_blur*2),k.height+(GamePiece.shadow_blur*2));this.renderImage(c,g.x,g.y,k.width,k.height)};GamePiece.prototype.isSelected=function(){return this.selected};GamePiece.prototype.setSelected=function(a){this.selected=a};function RedGamePiece(){GamePiece();this.type="Red";this.img_src="red_ball"}RedGamePiece.prototype=new GamePiece;function GreenGamePiece(){GamePiece();this.type="Green";this.img_src="green_ball"}GreenGamePiece.prototype=new GamePiece;function BlueGamePiece(){GamePiece();this.type="Blue";this.img_src="blue_ball"}BlueGamePiece.prototype=new GamePiece;function YellowGamePiece(){GamePiece();this.type="Yellow";this.img_src="yellow_ball"}YellowGamePiece.prototype=new GamePiece;function WildGamePiece(){GamePiece();this.type="Wild";this.img_src="wild_ball"}WildGamePiece.prototype=new GamePiece;function RadioactiveGamePiece(){GamePiece();this.type="Radioactive";this.img_src="radioactive_ball"}RadioactiveGamePiece.prototype=new GamePiece;