日期:2014-05-16 浏览次数:20323 次
<html> <head> <title></title> </head> <body> <script type="text/javascript"> function box(height,width,cellSize){ this.height=height; this.width=width; this.cellSize=cellSize; this.createTable(); this.createStyle(); this.map=[ [ [[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]], [[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]] ], [ [[1,1],[1,1]] ], [ [[0,0,0],[0,1,0],[1,1,1]], [[1,0,0],[1,1,0],[1,0,0]], [[0,0,0],[1,1,1],[0,1,0]], [[1,0,0],[1,1,0],[1,0,0]] ], [ [[1,0,0],[1,0,0],[1,1,0]], [[0,0,0],[1,1,1],[1,0,0]], [[1,1,0],[1,0,0],[1,0,0]], [[0,0,0],[0,1,0],[1,1,1]] ], [ [[0,0,1],[0,0,1],[0,1,1]], [[0,0,0],[1,0,0],[1,1,1]], [[0,1,1],[0,1,0],[0,1,0]], [[0,0,0],[1,1,1],[1,0,0]] ], [ [[0,0,0],[1,1,0],[0,1,1]], [[0,1,0],[1,1,0],[1,0,0]] ], [ [[0,0,0],[0,1,1],[1,1,0]], [[0,1,0],[0,1,1],[0,0,1]] ] ]; this.init(); } box.prototype={ $:function(ID){return document.getElementById(ID);}, createTable:function(){ var table=document.createElement("table"); table.border=0; var tbody=document.createElement("tbody"); tbody.id="body"; table.appendChild(tbody); var i,j; for(i=0;i<this.height;i++){ tbody.insertRow(i); for(j=0;j<this.width;j++){ tbody.rows[i].insertCell(j); tbody.rows[i].cells[j].className="space"; tbody.rows[i].cells[j].width=this.cellSize; tbody.rows[i].cells[j].height=this.cellSize; } } document.body.appendChild(table); }, createStyle:function(){ var style=document.createElement("style"); style.type="text/css"; var css=".space{background:#999999;}"+ ".now{background:#990000;}"+ ".ok{background:#ff0000}"; try{ style.styleSheet.cssText=css; } catch(ei){ style.appendChild(document.createTextNode(css)); } document.getElementsByTagName("head")[0].appendChild(style); }, getBox:function(){ var mapFirst=Math.floor(Math.random()*this.map.length); var mapSecond=Math.floor(Math.random()*this.map[mapFirst].length); return this.map[mapFirst][mapSecond]; }, boxStart:function(minBox,row,cell){ var i,j; for(i=0;i<minBox.length;i++){ for(j=0;j<minBox[i].length;j++){ if(minBox[i][j]&&this.$("body").rows[row+i]){ this.$("body").rows[row+i].cells[cell+j].className="now"; } } } }, clearSpace:function(minBox){ for(var x=minBox.length-1;x>=0;x--){ if(Number(minBox[x].join(""))){ break; } } return x; }, clearNow:function(td){ for(var i=0;i<td.length;i++){ td[i].className=td[i].className==="now"?"space":td[i].className; } }, clearOk:function(td){ for(var i=0;i<td.length;i++){ td[i].className=td[i].className==="now"?"ok":td[i].className; } }, testBorder:function(minBox,x,y,lr){ var k,l; for(k=0;k<minBox.length;k++){ for(l=0;l<minBox[k].length;l++){ if(this.$("body").rows[x+k]){ if(this.$("body").rows[x+k].cells[y+l