Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/meme2048/js/grid.js
1836 views
1
function Grid(t,i){this.size=t,this.cells=i?this.fromState(i):this.empty()}Grid.prototype.empty=function(){for(var t=[],i=0;i<this.size;i++)for(var e=t[i]=[],r=0;r<this.size;r++)e.push(null);return t},Grid.prototype.fromState=function(t){for(var i=[],e=0;e<this.size;e++)for(var r=i[e]=[],l=0;l<this.size;l++){var o=t[e][l];r.push(o?new Tile(o.position,o.value):null)}return i},Grid.prototype.randomAvailableCell=function(){var t=this.availableCells();if(t.length)return t[Math.floor(Math.random()*t.length)]},Grid.prototype.availableCells=function(){var t=[];return this.eachCell((function(i,e,r){r||t.push({x:i,y:e})})),t},Grid.prototype.eachCell=function(t){for(var i=0;i<this.size;i++)for(var e=0;e<this.size;e++)t(i,e,this.cells[i][e])},Grid.prototype.cellsAvailable=function(){return!!this.availableCells().length},Grid.prototype.cellAvailable=function(t){return!this.cellOccupied(t)},Grid.prototype.cellOccupied=function(t){return!!this.cellContent(t)},Grid.prototype.cellContent=function(t){return this.withinBounds(t)?this.cells[t.x][t.y]:null},Grid.prototype.insertTile=function(t){this.cells[t.x][t.y]=t},Grid.prototype.removeTile=function(t){this.cells[t.x][t.y]=null},Grid.prototype.withinBounds=function(t){return t.x>=0&&t.x<this.size&&t.y>=0&&t.y<this.size},Grid.prototype.serialize=function(){for(var t=[],i=0;i<this.size;i++)for(var e=t[i]=[],r=0;r<this.size;r++)e.push(this.cells[i][r]?this.cells[i][r].serialize():null);return{size:this.size,cells:t}};
2
3