<html> <head> <title>扫雷</title> <mce:script language="javascript"><!-- var dataArray =new Array(); var rowCount; var colCount; var count; function AddRow() { //var tableMain =document.getElementById("TblMain"); //var newRow=tableMain.insertRow(tableMain.rows.length); newRow=TblMain.insertRow(TblMain.rows.length); newRow.bgColor="#ffcc00"; var cel0=newRow.insertCell(0); var cel1=newRow.insertCell(1); cel0.innerText="1234"; cel1.innerHTML="<input type='button' value='delete row' onclick='DelRow(this.id)' id=1>"; } function DelRow(obj) { alert("test"); obj.deleteRow(obj.id); } function CreateTable() { rowCount=document.getElementById("TxtRow").value; colCount=document.getElementById("TxtCol").value; count=document.getElementById("TxtCount").value; if(isNaN(rowCount)) { alert("行数必须为数字"); return; } if(isNaN(colCount)) { alert("列数必须为数字"); return; } //alert("rowCount="+rowCount+";colCount="+colCount); var form=document.getElementById("MainForm"); if(document.getElementById("MainTable")!=null) { document.getElementById("MainTable").innerText=""; var table=document.getElementById("MainTable"); } else { var table=document.createElement("Table"); table.id="MainTable"; table.cellpadding=3 table.cellspacing=3 table.border=1; } //alert("start"); for(i=0;i<rowCount;i++) { //alert("i="+i); newRow=table.insertRow(table.rows.length); dataArray[i]=new Array(); for(j=0;j<colCount;j++) { //alert("j="+j) //var tempcel="cel"+i; var tempcel=newRow.insertCell(j); //cel0.innerHTML="<td bgcolor=#cccccc width=40 height=60></td>"; tempcel.id="cel"+eval(i*colCount+j); //tempcel.innerHTML="<td bgcolor=#cccccc width=10 height=16><input type='text' id="+"Txt"+eval(i*colCount+j+1)+" width=8 height=14></td>"; tempcel.width=10; tempcel.height=16; tempcel.bgcolor=10; //alert(tempcel.id); } } form.appendChild(table); //get random numbers if(isNaN(count)) { alert("雷数必须为数字"); return; } if(count>rowCount*colCount) { alert("雷数设置过大"); return; } //var tempArray=new Array(); for(i=0;i<count;i++) { var tempNum=Math.floor(Math.random()*colCount*rowCount); //if(tempArray[tempNum]=="a") if(dataArray[Math.floor(tempNum/colCount)][tempNum%colCount]=="a") { i--; } else { dataArray[Math.floor(tempNum/colCount)][tempNum%colCount]="a"; } } /* for(i=1;i<count+1;i++) { if(i> 1) { do { tempArray[i-1]=Math.floor(Math.random()*100); goon=false; for(ix=0;ix <i-1;ix++) { if(tempArray[ix]==tempArray[i-1]) { goon=true; break; } } if(!goon) break; } while(true); } else tempArray[i-1]=Math.floor(Math.random()*100); } for(i=0;i<count;i++) { //alert(tempArray[i]); //alert(Math.floor(tempArray[i]/colCount)); //alert(tempArray[i]%colCount); dataArray[Math.floor(tempArray[i]/colCount)][tempArray[i]%colCount]="a"; } */ var cellData; for(i=0;i<rowCount;i++) { for(j=0;j<colCount;j++) { if(dataArray[i][j]!="a") { cellData=0; if(i>0&&j>0) { if(dataArray[i-1][j-1]=="a") { cellData+=1; } } if(i>0) { if(dataArray[i-1][j]=="a") { cellData+=1; } } if(i>0&&j<colCount-1) { if(dataArray[i-1][j+1]=="a") { cellData+=1; } } if(j>0) { if(dataArray[i][j-1]=="a") { cellData+=1; } } if(j<colCount-1) { if(dataArray[i][j+1]=="a") { cellData+=1; } } if(i<rowCount-1&&j>0) { if(dataArray[i+1][j-1]=="a") { cellData+=1; } } if(i<rowCount-1) { if(dataArray[i+1][j]=="a") { cellData+=1; } } if(i<rowCount-1&&i<colCount-1) { if(dataArray[i+1][j+1]=="a") { cellData+=1; } } dataArray[i][j]=cellData; } } } for (var i=0; i<MainTable.rows.length; i++) for (var j=0; j<MainTable.rows[i].cells.length; j++) { MainTable.rows[i].cells[j].bgColor ='#cccccc'; //if(dataArray[i][j]==null) //{ //dataArray[i][j]=0; //} //MainTable.rows[i].cells[j].innerText=dataArray[i][j]; } //alert(dataArray[9][9]); document.getElementById("LblLeftCount").innerText=count ; } function OnClick() { /*alert((event.srcElement.id).substring(0,3)); if((event.srcElement.id).substring(0,3)=="cel" || (event.srcElement.id).substring(0,3)=="Mai") { for (var i=0; i<MainTable.rows.length; i++) for (var j=0; j<MainTable.rows[i].cells.length; j++) MainTable.rows[i].cells[j].bgColor = MainTable.rows[i].cells[j] == event.srcElement ? "#ffffff" :'#cccccc'; }*/ if((event.srcElement.id).substring(0,3)=="cel") { var leftCount=document.getElementById("LblLeftCount").innerText; if(event.srcElement.bgColor=="#cccccc") { var tempNum=(event.srcElement.id).substring(3); //alert(tempNum); document.getElementById(event.srcElement.id).bgColor='#ffffff'; //document.getElementById(event.srcElement.id).innerHTML="<font color=#ff3366 size=0.05>▲</font>"; //alert(tempNum); //alert(Math.floor(tempNum/colCount)+" "+eval(tempNum%colCount-1)); var tempX=Math.floor(tempNum/colCount); var tempY=tempNum%colCount; var tempData=dataArray[Math.floor(tempNum/colCount)][tempNum%colCount]; if(tempData=="a") { document.getElementById(event.srcElement.id).innerHTML="<font color=#ff3366 size=0.05>●</font>"; for(i=0;i<rowCount;i++) { for(j=0;j<colCount;j++) { if(dataArray[i][j]=="a") { document.getElementById("cel"+eval(i*colCount+j)).innerHTML="<font color=#ff3366 size=0.05>●</font>"; } } } if(confirm("失败,重新开始吗?")) CreateTable(); } else if(tempData==0) { //SetData(tempX,tempY); document.getElementById(event.srcElement.id).innerHTML="<font color=#ffffff size=0.05>■</font>"; } else { document.getElementById(event.srcElement.id).innerHTML="<font color=#000000 size=0.05>"+tempData+"</font>"; } } //alert(Math.random()); if(leftCount==0) { var finishFlg=true; for(i=0;i<rowCount;i++) { for(j=0;j<colCount;j++) { if(dataArray[i][j]=="a") { if(document.getElementById("cel"+eval(i*colCount+j)).bgColor!="#ff0000") { finishFlg=false; } } } } if(finishFlg) { if(confirm("恭喜过关,重新开始吗?")) { CreateTable(); } } } } } function OnRightClick() { //alert("start"); var leftCount=document.getElementById("LblLeftCount").innerText; if((event.srcElement.id).substring(0,3)=="cel") { if(event.srcElement.bgColor=="#cccccc") { if(leftCount>0) { event.srcElement.bgColor="#ff0000"; leftCount=parseInt(leftCount)-1; document.getElementById("LblLeftCount").innerText =leftCount; } } else if(event.srcElement.bgColor=="#ff0000") { event.srcElement.bgColor="#00ff00"; } else if(event.srcElement.bgColor=="#00ff00") { event.srcElement.bgColor="#cccccc"; leftCount=parseInt(leftCount)+1; document.getElementById("LblLeftCount").innerText=leftCount; } } if(leftCount==0) { var finishFlg=true; for(i=0;i<rowCount;i++) { for(j=0;j<colCount;j++) { if(dataArray[i][j]=="a") { if(document.getElementById("cel"+eval(i*colCount+j)).bgColor!="#ff0000") { finishFlg=false; } } } } if(finishFlg) { if(confirm("恭喜过关,重新开始吗?")) { CreateTable(); } } } } function SetData(dotX,dotY) { var rowCount=document.getElementById("TxtRow").value; var colCount=document.getElementById("TxtCol").value; document.getElementById("cel"+eval(dotX*colCount+dotY)).innerHTML="<font color=#ffffff size=0.05>■</font>"; if(dotX>0&&dotY>0) { if(dataArray[dotX-1][dotY-1]==0) { SetData(dotX-1,dotY-1); } else if(dataArray[dotX-1][dotY-1]!="a") { document.getElementById("cel"+eval((dotX-1)*colCount+dotY-1)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX-1][dotY-1]+"</font>"; } } if(dotX>0) { if(dataArray[dotX-1][dotY]==0) { SetData(dotX-1,dotY); } else if(dataArray[dotX-1][dotY]!="a") { document.getElementById("cel"+eval((dotX-1)*colCount+dotY)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX-1][dotY]+"</font>"; } } if(dotX>0&&dotY<colCount-1) { if(dataArray[dotX-1][dotY+1]==0) { SetData(dotX-1,dotY+1); } else if(dataArray[dotX-1][dotY+1]!="a") { document.getElementById("cel"+eval((dotX-1)*colCount+dotY+1)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX-1][dotY+1]+"</font>"; } } if(dotY>0) { if(dataArray[dotX][dotY-1]==0) { SetData(dotX,dotY-1); } else if(dataArray[dotX][dotY-1]!="a") { document.getElementById("cel"+eval(dotX*colCount+dotY-1)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX][dotY-1]+"</font>"; } } if(dotY<colCount-1) { if(dataArray[dotX][dotY+1]==0) { SetData(dotX,dotY+1); } else if(dataArray[dotX][dotY+1]!="a") { document.getElementById("cel"+eval(dotX*colCount+dotY+1)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX][dotY+1]+"</font>"; } } if(dotX<rowCount-1&&dotY>0) { if(dataArray[dotX+1][dotY-1]==0) { SetData(dotX+1,dotY-1); } else if(dataArray[dotX+1][dotY-1]!="a") { document.getElementById("cel"+eval((dotX+1)*colCount+dotY-1)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX+1][dotY-1]+"</font>"; } } if(dotX<rowCount-1) { if(dataArray[dotX+1][dotY]==0) { SetData(dotX+1,dotY); } else if(dataArray[dotX+1][dotY]!="a") { document.getElementById("cel"+eval((dotX+1)*colCount+dotY)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX+1][dotY]+"</font>"; } } if(dotX<rowCount-1&&dotY<colCount-1) { if(dataArray[dotX+1][dotY+1]==0) { SetData(dotX+1,dotY+1); } else if(dataArray[dotX+1][dotY+1]!="a") { document.getElementById("cel"+eval((dotX+1)*colCount+dotY+1)).innerHTML="<font color=#000000 size=0.05>"+dataArray[dotX+1][dotY+1]+"</font>"; } } } // --></mce:script> </head> <body onclick="OnClick()" oncontextmenu="OnRightClick();return false;"> <form id="MainForm"> <div align="left">行<input type="text" id="TxtRow" maxlength="2" size="5">列<input type="text" id="TxtCol" maxlength="2" size="5"> 雷数目<input type="text" id="TxtCount" maxlength="2" size=5> <label id=LblCount>剩余雷数</label> <label id=LblLeftCount></label> </div> <br /> <!-- <table id="TblMain" bgcolor="#cccccc" cellpadding="0" cellspacing="0" border="1" onclick="OnClick()"> <tr bgcolor="#cccccc"> <td bgcolor="#cccccc" width="2" height="4"><font size=0.05 color=#ff3366>1</font></td> </tr> </table> --> <input type="button" onclick="CreateTable()" value="创建游戏" id="btnCreate"> <br /> </form> </body> </html>