1. 程式人生 > >javascript寫的掃雷(asp版)

javascript寫的掃雷(asp版)

 

<!--#include file="conn.asp" -->
<html>
<title>掃雷</title>
<head>
<style type="text/css">
<!--
.table2 {
 border-top-width: 1px;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-bottom-style: 1px;
 border-top-color: #000000;
 border-right-color: #000000;
 border-bottom-color: #000000;
 border-left-color: #000000;
 border-top-style: 1px;
 border-right-style: 1px;
 border-left-style: 1px;
 font-size: 14px;
}
-->
</style>
<script type="text/javascript">
var ctime=0
var ttime
function timedCount()
{
  document.getElementById('timetxt').value=ctime
  ctime=ctime+1
  ttime=setTimeout("timedCount()",1000)
 
 
}
function finished()
{ var i,j;
  for(j=0;j<10;j++)
    {for (i=0;i<10;i++)
     {
       if (barr[j][i]==0)
         {return 0}
     }
    
    }
  return 1
}
function mark0(i,j)
{/*如果在(i,j) (0=<i,j<N) 內地雷數目為0,並把連續的區域都顯示*/
 var n,i1,j1;
        var ijinc=new Array(-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1);
 var x1=document.getElementById('myTable').rows[i].cells
            x1[j].innerHTML=0
        barr[i][j]=1
 for(n=0;n<8;n++)/*掃描周邊方格內的數值是否為0*/
  {
  i1=i+ijinc[n*2+0];
  j1=j+ijinc[n*2+1];
  if(i1>=0&&i1<10&&j1>=0&&j1<10)
                    {
                          if(aarr[i1][j1]==0&&barr[i1][j1]==0)
                            {mark0(i1,j1)}
                          else
                            {
                              var x1=document.getElementById('myTable').rows[i1].cells
                              x1[j1].innerHTML=aarr[i1][j1]
                              barr[i1][j1]=1   
                             }
                     }
                 }
       
}
function show_coords(event)
{
  x=parseInt((event.clientX-15)/21)
  y=parseInt((event.clientY-19)/22)
  //alert("X 座標: " + x + ", Y 座標: " + y)
  if(x>=0&&x<10&&y>=0&&y<10&&ctime>0)
  { 
    if (event.button==2)
    {
      //alert("您點選了滑鼠右鍵!")
      var x1=document.getElementById('myTable').rows[y].cells
          x1[x].innerHTML='*'
       if(aarr[y][x]==9)
         {barr[y][x]=1}
    }
    else
    {
      var x1=document.getElementById('myTable').rows[y].cells
          x1[x].innerHTML=aarr[y][x]
      barr[y][x]=1  
      if(aarr[y][x]==9)
       {
         ctime=ctime+20
         alert("您點選了地雷!")
         return
       }
       if(aarr[y][x]==0)
       {
         mark0(y,x)
       }
    }
    if(finished()==1)
     {
       clearTimeout(ttime)
       alert("您完成了任務!")
     }
  }
}

function setmine( i,j)
{
   //*在(i,j) (0=<i,j<N) 放一枚地雷,並設定周邊空格內地雷數目*/
   var n,i1,j1;
   var ijinc=new Array(-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1);
 if (aarr[i][j]==9)
           {return 0}
 aarr[i][j]=9;/*標記為地雷*/
 for(n=0;n<8;n++)/*標記周邊方格內的數值即地雷數*/
 {
  i1=i+ijinc[n*2+0];
  j1=j+ijinc[n*2+1];
  if((i1>=0)&&(i1<10) &&(j1>=0)&&(j1<10) &&(aarr[i1][j1]<9))
                  {aarr[i1][j1]++;}
 }
        return 1;
}

function ajaxbegin()
 {
 var xmlHttp;
 
 try
    { // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    { // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {
       try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
       catch (e)
         {
         alert("您的瀏覽器不支援AJAX!");
         return false;
         }
      }
    }
    xmlHttp.open("GET","useradd.asp",true);
    xmlHttp.send(null);
 
 }
function ajaxend()
 {
 var xmlHttp;
 
 try
    { // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    { // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {
       try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
       catch (e)
         {
         alert("您的瀏覽器不支援AJAX!");
         return false;
         }
      }
    }
    xmlHttp.open("GET","usersub.asp",true);
    xmlHttp.send(null);
 
 }


</script>
</head>

<body  onmousedown="show_coords(event)" oncontextmenu="window.event.returnvalue=false; return false;" onbeforeunload="ajaxend()">
<table id="myTable" width="220" border="1"  class="table2" >
<script type="text/javascript">

  var i=0,j=0
  var time=10
  var aarr=new Array(0,0,0,0,0,0,0,0,0,0)
  for (i=0;i<10;i++)
     {
       aarr[i]=new Array(0,0,0,0,0,0,0,0,0,0)
     }
  var barr=new Array(0,0,0,0,0,0,0,0,0,0)
  for (i=0;i<10;i++)
     {
       barr[i]=new Array(0,0,0,0,0,0,0,0,0,0)
     }

  while(time>0)
  {
    i=parseInt(10*Math.random())
    j=parseInt(10*Math.random())
    if(setmine(i,j)==1)
    {time=time-1}
  }
 
  for(j=0;j<10;j++)
    { 
     for (i=0;i<10;i++)
     {
       document.write("<td>"+"&nbsp"+"</td>")
     }
     document.write("<tr>")
    }
    document.write("</table>")
    ajaxbegin()
</script>
 <form>
 <input type="button" value="開始" onClick="timedCount()">
 <input type="text" id="timetxt">
 </form>

 <p>單擊看,右鍵標記,點選地雷加20秒,F5重新開始!</p>
<%
sql="select  * from data where id=1"
set rs=conn.execute(sql)
total=rs("totaluser")+1
online=rs("onlineuser")+1
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write("訪問次數:")
response.write(total)
response.write("  線上:")
response.write(online)

%>
</body>
</html>