<HTML>
<HEAD>
<TITLE>Javascript 地雷</TITLE>
<style>
inputno {
backgroundcolor:eeeeee;
border:none;
width:;
height:;
color:blue;
}
inputye {
backgroundcolor:eeeeee;
border:none;
width:;
height:;
color:red;
}
input {
width:;
height:;
color:red;
}
inputmode {
width:;
height:;
borderstyle:inset
}
inputmode {
width:;
height:;
COLOR:GREEN;
}
</style>
</HEAD>
<BODY BGCOLOR="#">
<div ID=dilei style="position:absolute;left:;top:">
<TABLE BORDER= CELLSPACING= CELLPADDING=>
<SCRIPT LANGUAGE="JavaScript">
<!
var xy=;
while(documentwrite("<tr>")y++<)
for(x=;x++<;documentwrite("<td ><font size=>
<input ID="+x+"_"+y+" type=button>"))
;
//>
</SCRIPT>
</TABLE>
</div>
<div ID=ToolBar style="position:absolute;top:;left:;
/*height:;*/backgroundcolor:green;width:">
<input id=Mode type=button value="挖雷" class=mode
onclick="javascript: modeTab();">
<nobr>
<span id=modenow style="color:blue">现在可以揭开方块</span>
</div>
</BODY>
<SCRIPT LANGUAGE="JavaScript">
<!//
var RowColi=;
var RowSize=ColSize=;
var mineNum=; //地雷数
var mode=;
var mineX=new Array(mineNum) mineY=new Array(mineNum);
var Mine= new Array(RowSize)Decouv= new Array(RowSize);
for(i=;i<;i++){
Mine[i]=new Array(ColSize);
Decouv[i]= new Array(ColSize);
}
//初始化
for (Row=;Row<=RowSize;Row++) {
for (Col=;Col<=ColSize;Col++) {
Mine[Row][Col]=;
Decouv[Row][Col]=;
}
};
//布地雷 Mathrandom( )
i=;
while (i<mineNum){
mineX[i]=Mathround(Mathrandom()*)%RowSize;
mineY[i]=Mathround(Mathrandom()*)%ColSize;
if(Mine[mineX[i]][mineY[i]]==) continue;
Mine[mineX[i]][mineY[i]]=;
Decouv[mineX[i]][mineY[i]]=;
i++;
}
for(i=;i<mineNum;i++){
for(j=;j<=;j++){
switch(j){
case :
divIndexX=mineX[i];divIndexY=mineY[i];
break;
case :
divIndexX=mineX[i];divIndexY=mineY[i];
break;
case :
divIndexX=mineX[i];divIndexY=mineY[i]+;
break;
case :
divIndexX=mineX[i];divIndexY=mineY[i];
break;
case :
divIndexX=mineX[i];divIndexY=mineY[i]+;
break;
case :
divIndexX=mineX[i]+;divIndexY=mineY[i];
break;
case :
divIndexX=mineX[i]+;divIndexY=mineY[i];
break;
case :
divIndexX=mineX[i]+;divIndexY=mineY[i]+;
}
if(divIndexX<||divIndexY<||divIndexX>=RowSize||divIndexY>=ColSize) continue;
Decouv[divIndexX][divIndexY]+=;
Mine[divIndexX][divIndexY]=Decouv[divIndexX][divIndexY];
}
}
//模式切换
function modeTab(){
if(mode==){
mode=;
documentall("Mode")className="mode";
documentall("Mode")value="探测";
documentall("modenow")innerText="现在可以标记雷区"+mode;
}else{
mode=;
documentall("Mode")className="mode";
documentall("Mode")value="挖雷";
documentall("modenow")innerText="现在可以揭开方块"+mode;
}
}
function TRY(){
var SRCElem=eventsrcElement;
if(SRCElemtagName!="INPUT"||SRCElemclassName=="mode"||
SRCElemclassName=="mode") return;
if(SRCElemclassName=="no"||SRCElemclassName=="ye")return;
tryDown(SRCElem);
}
function tryDown(Elem){
var indexXindexY;
var ss=Elemidsplit("_");
indexX=ss[]valueOf();
indexY=ss[]valueOf();
if(mode==){
if(Elemvalue==""){
Elemvalue="!";
Mine[indexX][indexY]=Decouv[indexX][indexY];
Decouv[indexX][indexY]=;
return;}
else{
Elemvalue="";
Decouv[indexX][indexY]=Mine[indexX][indexY];
return;
}
}
if(Decouv[indexX][indexY]>){
if(Decouv[indexX][indexY]>=)
return;
Elemvalue="*";
ElemclassName="ye";
Decouv[indexX][indexY]=
return;
}
ElemclassName="no";
Elemvalue=""+Mine[indexX][indexY];
if(Decouv[indexX][indexY]>){
Decouv[indexX][indexY]=
return;
}
if(Decouv[indexX][indexY]==)
{
Elemvalue="";
tryGo(indexXindexY);
}
return;
}
function TRY(Elm){
Elmvalue="!";
}
function judge(xy){
if(Decouv[x][y]!=&&Decouv[x][y]!=){
return ;}
var i=j=divIndexXdivIndexY;
for(i=;i<=;i++){
switch(i){//
case ://up
divIndexX=x;divIndexY=y;j=;
break;
case ://left
divIndexX=x;divIndexY=y;j=;
break;
case ://down
divIndexX=x;divIndexY=y+;j=;
break;
case ://right
divIndexX=x+;divIndexY=y;j=;
}
if(divIndexX>=&&divIndexY>=&&divIndexX<RowSize&&divIndexY
<ColSize&&Decouv[divIndexX][divIndexY]>=&&Decouv[divIndexX][divIndexY]<=){
return j;
}
}
return ;
}
function tryGo(xy){
var stackX=new Array()stackY=new Array();
var i=;
var test=;
var divIndexX=xdivIndexY=y;
stackX[i]=divIndexX;stackY[i]=divIndexY;
while(test){
var Elem;
eval("Elem=documentall(""+(divIndexX+)+"_"+(divIndexY+)+"")");
ElemclassName="no";
if(Decouv[divIndexX][divIndexY]<=)
Decouv[divIndexX][divIndexY]= ;
else{
Elemvalue=""+Mine[divIndexX][divIndexY];
Decouv[divIndexX][divIndexY]= ;
}
switch(judge(divIndexXdivIndexY)){
case ://up
divIndexY;
break;
case ://left
divIndexX;
break;
case ://down
divIndexY++;
break;
case ://right
divIndexX++;
break;
case :
i=i;
if(i<){
return;
}
divIndexX=stackX[i];divIndexY=stackY[i];
continue;
}
i=i+;stackX[i]=divIndexX;stackY[i]=divIndexY;
}
}
documentonclick =TRY;
//>
</SCRIPT>
</HTML>