<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="this is my page" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<html xmlns="http://www.w3.org/1999/xhtml" ><head>
<style type="text/css">
<!-- 
BODY
{
}
.td_img{
HEIGHT: 21px;
WIDTH: 21px;
margin: 2px;
}
.td_start{
BACKGROUND-COLOR: slategray;
CURSOR: pointer;
HEIGHT: 25px;
WIDTH: 25px;
border:1px solid #0a0a0c;
}
.td_null{
BACKGROUND-COLOR: white;
CURSOR: default ;
HEIGHT: 25px;
WIDTH: 25px;
border:1px solid #7a7a7c;
}
.td_mine{
BACKGROUND-COLOR: firebrick;
CURSOR: default;
HEIGHT: 25px;
WIDTH: 25px;
border:1px solid #7a7a7c;
}.GT
{
BORDER-BOTTOM: deepskyblue thin solid;
BORDER-LEFT: deepskyblue thin solid;
BORDER-RIGHT: deepskyblue thin solid;
BORDER-TOP: deepskyblue thin solid;
CURSOR: default
}
-->
</style>
    <title>无标题页</title>
    <script type="text/javascript">
    var width=5;
    var height=3;
    var mineCount=2;
    function startgame(mCount, width , height)
    {
       createtable();
       ranmine();
    }
    
    //创建表格
    function createtable()
    {
         width=parseInt(document.getElementById("width").value);
         height=parseInt(document.getElementById("height").value);
         mineCount=parseInt(document.getElementById("mineCount").value);
         if(mineCount*2>width*height)
         {
             alert("mineCount is more!");
             return;
         }
         else
         {
             var content="";
             var count=0;
             var oTable=document.getElementById("tableId") ;
             var oTbody=document.createElement("tbody");
             var orows=oTable.rows.length;
             for(var i=0;i<orows;i++)
             {
                 oTable.deleteRow(0);
             } 
             
             for(var i=1;i<=height;i++)
             {
                var _tr=document.createElement("tr");
                
                for(var j=1;j<=width;j++)
                {
                   count++;
                   var _td=document.createElement("td");
                   _td.className="td_start";
                   _td.oncontextmenu=function(){ rclick(this);return false;};
                   _td.onclick=function(){lclick(this);};
                   _td.id=count;
                   _td.innerHTML="&nbsp;&nbsp;&nbsp;";
                   _td.char="0";
                   _td.title="";
                   _tr.appendChild(_td);
                }
                oTbody.appendChild(_tr);
             }
            oTable.appendChild(oTbody);
        }
   }
   
  
    
    //左击
    function lclick(obj)
    {
       var count = parseInt(obj.char);
       if(count==-1)
       {
         gameover();
       }
       else if(count>0)
       {
          obj.innerHTML=count;
          obj.className="td_null";
       }
       else if(count==0)
       {
          clearnull(obj);
       }
       var Midmax=width*height;
       var midcount=0;
       for(var i=1;i<=Midmax;i++)
       {
          if(document.getElementById(i).className=="td_null")//除了炸弹所以已被选掉的炸弹
          {
              midcount++;
          }
          if(document.getElementById(i).className=="td_mine")
          {
             alert('死了吧,傻逼');
             window.clearInterval(time);
             return;
          }
       }
       
       if((midcount+mineCount)==Midmax)
       {
          alert("厉害了吧!");
          window.clearInterval(time);
       }
       
       
    }
    
    //右击
    function rclick(obj)
    {
       if(obj.className=="td_start")
       {
          if(obj.innerHTML == "&nbsp;&nbsp;&nbsp;")
          {
             obj.innerHTML ="<div class='td_img' style='background-color:Black' ></div>" ;
          }
          else if( obj.firstChild.style.backgroundColor=="black")
          {
             obj.innerHTML ="<div class='td_img' style='background-color:Blue' ></div>" ;
          }
          else if(obj.firstChild.style.backgroundColor=="blue")
          {
             obj.innerHTML = "&nbsp;&nbsp;&nbsp;";
          }
       }
    }
    
    
    
    //游戏结束
    function gameover()
    {
       var maxid=width*height;
       for(var i=1;i<=maxid;i++)
       {
          var cell=document.getElementById(i);
          cell.className="td_null";
          var count=cell.char;
          if(count>0)//没有炸弹
          {
             cell.innerHTML=cell.char;
          }
          else if(count==0)
          {
             cell.innerHTML="&nbsp;&nbsp;&nbsp;";
          }
          else if (count<0)//有炸弹的
          {
             cell.innerHTML="<div class='td_img' style='background-color:Red' ></div>"
             cell.className="td_mine";
          }
       }
    }
    
//单击之后清除没有炸弹的部分(局部) 
function clearnull(it)
{
    if(it.innerHTML != "&nbsp;&nbsp;&nbsp;")
    {
        return ; 
    }    it.className = "td_null" ;
    
    var w = 0 ; 
    var h = 0 ;
    var mId = it.id;     if(mId%width ==0)
    {
        w = width; 
        h = Math.floor(mId/width) ;
    }
    else 
    {
        w= mId%width ;
        h = Math.floor(mId/width) + 1 ;
    }    checkaround(w-1, h-1);
    checkaround(w-1, h);
    checkaround(w-1, h+1);    checkaround(w, h-1);
    checkaround(w, h+1);    checkaround(w+1, h-1);
    checkaround(w+1, h);
    checkaround(w+1, h+1);}//检测清除的范围
function checkaround(w, h)
{    if(checkPoz(w, h))
    {
        var tId = (h-1)*width + w ;
        var cell = document.getElementById(tId+"");        var count = parseInt(cell.char);         if(cell.className == "td_start" && count ==0 )
        {
            clearnull(cell);
        }
        else if(cell.className == "td_start" && count > 0 )
        {
            cell.innerHTML = count ;
            cell.className = "td_null" ;
        }
    }
}//放置炸弹
function ranmine()
{
    var count = 0 ;//炸弹初始化
    var mine = new Array(mineCount);//炸弹总数
    var maxId = width * height ;    //宽*高形成表格
    var flag = true ; 
    while(count < mineCount)
    {        flag = true; 
        var r = parseInt(Math.random()*maxId );//生成不重复的随即数
        for(i=0; i<count; i++)
        {
            if(r== mine[i] || r==0 || mine[i]==0)
            flag = false;
        }        if(flag == true){
            mine[count] = r;
            count ++; 
        }    }//end while    for(i=0; i<mineCount ;i++)
    {
        document.getElementById(mine[i]).char="-1"; 
        document.getElementById(mine[i]).title="-1";
    }    for(i=1 ; i<=maxId ;i++)
    {
        getminecount(i); 
    }

  
  //设置不是炸弹需要显示的数字,除了0
  function getminecount(mId)
  {
    var count = parseInt(document.getElementById(mId).char);
    if(count ==-1)
    {
        // means of mine; 
        return ; 
    }    var w = 0 ; 
    var h = 0 ;    if(mId%width ==0)
    {
        w = width; 
        h = Math.floor(mId/width) ;
    }
    else 
    {
        w= mId%width ;
        h = Math.floor(mId/width) + 1 ;
    }    var c =0; 
    c = c + countmine(w-1, h-1);
    c = c + countmine(w-1, h);
    c = c + countmine(w-1, h+1);    c = c + countmine(w, h-1);
    c = c + countmine(w, h+1);    c = c + countmine(w+1, h-1);
    c = c + countmine(w+1, h);
    c = c + countmine(w+1, h+1);    document.getElementById(mId).char = c ; 
    document.getElementById(mId).title = c ; 
}function countmine(w, h)
{
    if(checkPoz(w, h))
    {
        tId = (h-1)*width + (w) ;
        var cell = document.getElementById(tId);
        var count = parseInt(cell.char); 
        if(count == -1 )
        {
            return 1; 
        }
    }
     return 0 ; 
}
  
//范围的检测
function checkPoz(x,y)
{
   if(x<=0 || x>width || y<=0 || y>height)
   {
       return false;
   }
   else
   {
      return true;
   }
}var timeflag=-1;
var time;
function starttime()
{
   timeflag++;
   var obj=document.getElementById("sptime");
   obj.innerHTML="开始计时:"+timeflag+"秒";
   time=setTimeout("starttime()",1000);
}    </script>
    
</head>
<body>高度:<input type="text" id="width" value="10" /><br />
宽度:<input type="text" id="height" value="10" /><br />
雷数:<input type="text" id="mineCount" value="10" />
<input type="button" id="start" value="开始游戏" onclick="startgame(); clearInterval(time);timeflag=-1;starttime();  " />
<br />
<br />
<span id="sptime" style="margin-left:60px"></span><table cellspacing=1 cellpadding=1 class=gt border=2 style="position:absolute;left:0px;top:121px;" id="tableId"  ></table></body>
</html>