JS扫雷原理性代码
Posted 易小川
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS扫雷原理性代码相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>寻雷</title> <style type="text/css"> <!-- td { font-size: 18px; font-weight: bold; color: #FF0000; text-align: center; height: 25px; width: 25px; border-top-width: 1px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #0000FF; border-right-color: #0000FF; border-bottom-color: #0000FF; border-left-color: #0000FF; } div { font-family: "宋体", "新宋体", "黑体"; font-size: 12px; font-weight: bold; color: #006600; float: none; margin-right: auto; margin-left: auto; height: 100px; width: 500px; text-align: center; } --> </style> <script language="javascript"> var data=new Array( [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0] ); var count=0; do{ var a=1+Math.round(8*(Math.random())); var b=1+Math.round(8*(Math.random())); if(data[a][b]==0){ data[a][b]="shit"; count+=1; } }while(count<10) for(var i=1;i<10;i++){ for(var j=1;j<10;j++){ if(data[i][j]!="shit"){ var counter=0; if(data[i-1][j-1]=="shit") counter+=1; if(data[i-1][j]=="shit") counter+=1; if(data[i-1][j+1]=="shit") counter+=1; if(data[i][j-1]=="shit") counter+=1; if(data[i][j+1]=="shit") counter+=1; if(data[i+1][j-1]=="shit") counter+=1; if(data[i+1][j]=="shit") counter+=1; if(data[i+1][j+1]=="shit") counter+=1; data[i][j]=counter; } } } function pop(){ event.bubbles=true; var number=parseInt(event.srcElement.getAttribute("id")); var j=number % 10; var i=(number-j)/10; if(data[i][j]=="shit"){ alert("中雷了"); check(); return; } if(data[i][j]==0){ for(var p=i-1;p<i+2;p++){ for(var q=j-1;q<j+2;q++){ var r="" +p+""+q; var oElement=document.getElementById(r); if(oElement){ oElement.innerHTML=data[p][q]; } } } return; } document.getElementById(event.srcElement.getAttribute("id")).innerHTML=data[i][j]; } function check(){ for(var i=1;i<10;i++){ for(var j=1;j<10;j++){ var no="" + i+""+j; document.getElementById(no).innerHTML=data[i][j]; } } } </script> </head> <body> <table id="lei" border="1" align="center" onclick="pop();" > <tr> <td id="11"> </td> <td id="12"> </td> <td id="13"> </td> <td id="14"> </td> <td id="15"> </td> <td id="16"> </td> <td id="17"> </td> <td id="18"> </td> <td id="19"> </td> </tr> <tr> <td id="21"> </td> <td id="22"> </td> <td id="23"> </td> <td id="24"> </td> <td id="25"> </td> <td id="26"> </td> <td id="27"> </td> <td id="28"> </td> <td id="29"> </td> </tr> <tr> <td id="31"> </td> <td id="32"> </td> <td id="33"> </td> <td id="34"> </td> <td id="35"> </td> <td id="36"> </td> <td id="37"> </td> <td id="38"> </td> <td id="39"> </td> </tr> <tr> <td id="41"> </td> <td id="42"> </td> <td id="43"> </td> <td id="44"> </td> <td id="45"> </td> <td id="46"> </td> <td id="47"> </td> <td id="48"> </td> <td id="49"> </td> </tr> <tr> <td id="51"> </td> <td id="52"> </td> <td id="53"> </td> <td id="54"> </td> <td id="55"> </td> <td id="56"> </td> <td id="57"> </td> <td id="58"> </td> <td id="59"> </td> </tr> <tr> <td id="61"> </td> <td id="62"> </td> <td id="63"> </td> <td id="64"> </td> <td id="65"> </td> <td id="66"> </td> <td id="67"> </td> <td id="68"> </td> <td id="69"> </td> </tr> <tr> <td id="71"> </td> <td id="72"> </td> <td id="73"> </td> <td id="74"> </td> <td id="75"> </td> <td id="76"> </td> <td id="77"> </td> <td id="78"> </td> <td id="79"> </td> </tr> <tr> <td id="81"> </td> <td id="82"> </td> <td id="83"> </td> <td id="84"> </td> <td id="85"> </td> <td id="86"> </td> <td id="87"> </td> <td id="88"> </td> <td id="89"> </td> </tr> <tr> <td id="91"> </td> <td id="92"> </td> <td id="93"> </td> <td id="94"> </td> <td id="95"> </td> <td id="96"> </td> <td id="97"> </td> <td id="98"> </td> <td id="99"> </td> </tr> </table> </body> </html>
以上是关于JS扫雷原理性代码的主要内容,如果未能解决你的问题,请参考以下文章