JavaScript写的扫雷程序(二)
var colortable=new COLOR("#C0C0C0","#0000FF","#00FF00","#FF0000","#007F7F","#000000","#123456","#654321","#456123");

function Mine(havemine,opened,flag,number)
{
this.havemine=havemine;
this.opened=opened;
this.flag=flag;
this.number=number;
return this;
}

function MineArray(row,col)
{
var indx=0;
this.length=(row-1)*10+col;
for(var x=0;x<row;x++)
 for(var y=0;y<col;y++)
 {
  indx=(x*10)+y;
  this[indx]=new Mine(false,false,false,0);
 }
return this;
}

function MA(row,col)
{
 this.row=row;
 this.col=col;
 return this;
}

function MAarray(n)
{
 this.length=n;
 for(var i=0;i<n;i++)
  this[i]=new MA(0,0);
 this.count=0;
 return this;
}

var minelist=new MineArray(10,10);
var gameover=false;

function Click()
{
var clickbox;
var i,j,k,m;
var id;
if(gameover)return;
clickbox=window.event.srcElement;
if(clickbox.id.substring(0,2)>="00"&&clickbox.id.substring(0,2)<="99")
{
  if(clickbox.id.substring(2,3)=="x");
  else if(clickbox.id.substring(2,3)=="f");
  else{
   i=Math.floor(clickbox.id/10);
   j=clickbox.id-10*i;
  if(!minelist[i*10+j].opened)
  if(minelist[i*10+j].havemine)
  {
   clickbox.style.display="none";
   document.all(clickbox.id+"m").style.backgroundColor="#FF0000";
   document.all(clickbox.id+"m").style.display="";
   minelist[i*10+j].opened=true;
   for(i=0;i<10;i++)
   for(j=0;j<10;j++)
    if(minelist[i*10+j].havemine&&!minelist[i*10+j].opened&&!minelist[i*10+j].flag)
    {
     id=""+i+j;
     document.all(id).style.display="none";
     document.all(id+"m").style.display="";
    }
    else if(!minelist[i*10+j].havemine&&minelist[i*10+j].flag)
    {
     id=""+i+j;
     document.all(id+"f").innerHTML='<IMG src='+errorimg.src+' border=0 width=30 height=30>';
    }
    gameover=true;
    GameOver(false);
  }
  else
  {
   var ma=new MAarray(100);
   ma.count=1;
   ma[0].row=i;
   ma[0].col=j;
   for(k=0;k<ma.count;k++) //打开空白!
   {
    id=""+ma[k].row+ma[k].col;
    document.all(id).style.display="none";
    document.all(id+"x"+minelist[ma[k].row*10+ma[k].col].number).style.display="";
    minelist[ma[k].row*10+ma[k].col].opened=true;
    if(minelist[ma[k].row*10+ma[k].col].number==0)
    {
     for(i=ma[k].row-1;i<ma[k].row+2;i++)
     for(j=ma[k].col-1;j<ma[k].col+2;j++)
     {
      if(i>=0&&i<10&&j>=0&&j<10)
       if(!minelist[i*10+j].opened)
      {
       for(m=k;m<ma.count;m++)
        if(i==ma[m].row&&j==ma[m].col)break;
       if(m>=ma.count){
       ma[ma.count].row=i;
       ma[ma.count].col=j;
       ma.count++;
       }
      }
     }
    }
   }
   opened+=k;
   if(opened+minenumber==100)
   {
    gameover=true;
    for(i=0;i<10;i++)
     for(j=0;j<10;j++)
      if(minelist[i*10+j].havemine&&!minelist[i*10+j].flag)
      {
       document.all(""+i+j).style.display="none";
       document.all(""+i+j+"f").style.display="";
      }
    GameOver(true);
   }
  }
  }
 }
}