栏目搜索
 
 
 
 
你的位置:首页 > JavaScript > javascript扫雷游戏示例 >
 

javascript扫雷游戏示例

发布者:[本站编辑] | 来源:[]

javascript扫雷游戏示例_电脑维修资料库


<html>
<head>
<title>javascript 地雷</title>

<style>
input.no {
 background-color:eeeeee;
 border:none;
 width:20;
 height:20;
 color:blue;
}
input.ye {
 background-color:eeeeee;
 border:none;
 width:20;
 height:20;
 color:red;
}
input {
 width:20;
 height:20;
 color:red;
}
input.mode0 {
 width:50;
 height:30;
 border-style:inset
}
input.mode1 {
 width:50;
 height:30;
 color:green;
}
</style>
</head>
<body bgcolor=#000000>
<div id=dilei style=position:absolute;left:0;top:5>
<table border=0 cellspacing=0 cellpadding=0>
  <script language=javascript>
<!--
var x,y=0;
while(document.write(<tr>),y++<20)
for(x=0;x++<20;document.write(<td ><font size=-5><input id=+x+_+y+ type=button>))
;

//-->
</script>
</table>
</div>
<div id=toolbar style=position:absolute;top:5;left:401;/*height:300;*/background-color:green;width:200>
<input id=mode type=button value=挖雷 class=mode1  onclick=javascript: modetab();><nobr><span id=modenow style=color:blue>现在可以揭开方块</span>
</div>
</body>
<script language=javascript>
<!--//34
var row,col,i=0;
var rowsize=20,colsize=20;
var minenum=45;  //地雷数
var mode=1;


var minex=new array(minenum), miney=new array(minenum);
var mine= new array(rowsize),decouv= new array(rowsize);
for(i=0;i<20;i++){
  mine=new array(colsize);
  decouv= new array(colsize);
}


//初始化
 
for (row=1;row<=rowsize;row++) { 
for (col=1;col<=colsize;col++) { 
mine=0; 
decouv=0; 

}; 
//布地雷 math.random( )
i=0;
while (i<minenum){
minex=math.round(math.random()*1000)%rowsize;
miney=math.round(math.random()*1000)%colsize;
if(mine>>==9) continue;
mine>>=9;
decouv>>=9;
i++;
}
for(i=0;i<minenum;i++){
for(j=1;j<=8;j++){
switch(j){
case 1:
divindexx=minex-1;divindexy=miney-1;
break;
case 2:
divindexx=minex-1;divindexy=miney;
break;
case 3:
divindexx=minex-1;divindexy=miney+1;
break;
case 4:
divindexx=minex;divindexy=miney-1;
break;
case 5:
divindexx=minex;divindexy=miney+1;
break;
case 6:
divindexx=minex+1;divindexy=miney-1;
break;
case 7:
divindexx=minex+1;divindexy=miney;
break;
case 8:
divindexx=minex+1;divindexy=miney+1;
}
if(divindexx<0||divindexy<0||divindexx>=rowsize||divindexy>=colsize) continue;
decouv+=1;
mine=decouv;
 }
}
//模式切换

function modetab(){
if(mode==1){
mode=0;
document.all(mode).classname=mode0;
document.all(mode).value=探测;
document.all(modenow).innertext=现在可以标记雷区+mode;
}else{
mode=1;
document.all(mode).classname=mode1;
document.all(mode).value=挖雷;
document.all(modenow).innertext=现在可以揭开方块+mode;
}
}

 

function try(){
var srcelem=event.srcelement;
if(srcelem.tagname!=input||srcelem.classname==mode0||srcelem.classname==mode1) return;
if(srcelem.classname==no||srcelem.classname==ye)return;
trydown(srcelem);
}
function trydown(elem){
var  indexx,indexy;
var ss=elem.id.split(_);
indexx=ss<0>.valueof()-1;
indexy=ss<1>.valueof()-1;

if(mode==0){
if(elem.value==){
elem.value=!;
mine=decouv;
decouv=18;
return;}
else{
elem.value=;
decouv=mine;
return;
}
}
if(decouv>8){
if(decouv>=18)
return;
elem.value=*;
elem.classname=ye;
decouv=9
return;
}
elem.classname=no;
elem.value=+mine;
if(decouv>0){
decouv=9
return;
 }
if(decouv==0)
{
elem.value=;
trygo(indexx,indexy);
}
return;
}
function try2(elm){
elm.value=!;
}
function judge4(x,y){
if(decouv!=-1&&decouv!=0){
return 0;}
var i=0,j=0,divindexx,divindexy;
for(i=1;i<=4;i++){
switch(i){//
case 1://up
divindexx=x;divindexy=y-1;j=1;
break;
case 2://left
divindexx=x-1;divindexy=y;j=2;
break;
case 3://down
divindexx=x;divindexy=y+1;j=3;
break;
case 4://right
divindexx=x+1;divindexy=y;j=4;
  }
if(divindexx>=0&&divindexy>=0&&divindexx<rowsize&&divindexy<colsize&&decouv>=0&&decouv<=8){
return j;
  } 
 }
 return 0;
}
function trygo(x,y){
var stackx=new array(),stacky=new array();
var i=0;
var test=50;
var divindexx=x,divindexy=y;
stackx=divindexx;stacky=divindexy;
while(test){
 var elem;
 eval(elem=document.all(\\+(divindexx+1)+_+(divindexy+1)+\\));
 elem.classname=no;
 if(decouv<=0)
  decouv= -1;
 else{
  elem.value=+mine;
  decouv= 9;
  }

 switch(judge4(divindexx,divindexy)){
 case 1://up
 divindexy--;
 break;
 case 2://left
 divindexx--;
 break;
 case 3://down
 divindexy++;
 break;
 case 4://right
 divindexx++;
 break;
 case 0:
 i=i-1;
 if(i<0){
  return;
 }
 divindexx=stackx;divindexy=stacky;
 continue;
  }
 i=i+1;stackx=divindexx;stacky=divindexy;
 }
}
document.onclick =try;
//-->
</script>
</html>

 
</t

 
返回列表 返回JavaScript
 
  推荐文章
 
     暂无