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[i]=new Array(ColSize);
  Decouv[i]= new Array(ColSize);
}


//初始化
 
for (Row=1;Row<=RowSize;Row++) { 
for (Col=1;Col<=ColSize;Col++) { 
Mine[Row-1][Col-1]=0; 
Decouv[Row-1][Col-1]=0; 

}; 
//布地雷 Math.random( )
i=0;
while (i<mineNum){
mineX[i]=Math.round(Math.random()*1000)%RowSize;
mineY[i]=Math.round(Math.random()*1000)%ColSize;
if(Mine[mineX[i]][mineY[i]]==9) continue;
Mine[mineX[i]][mineY[i]]=9;
Decouv[mineX[i]][mineY[i]]=9;
i++;
}
for(i=0;i<mineNum;i++){
for(j=1;j<=8;j++){
switch(j){
case 1:
divIndexX=mineX[i]-1;divIndexY=mineY[i]-1;
break;
case 2:
divIndexX=mineX[i]-1;divIndexY=mineY[i];
break;
case 3:
divIndexX=mineX[i]-1;divIndexY=mineY[i]+1;
break;
case 4:
divIndexX=mineX[i];divIndexY=mineY[i]-1;
break;
case 5:
divIndexX=mineX[i];divIndexY=mineY[i]+1;
break;
case 6:
divIndexX=mineX[i]+1;divIndexY=mineY[i]-1;
break;
case 7:
divIndexX=mineX[i]+1;divIndexY=mineY[i];
break;
case 8:
divIndexX=mineX[i]+1;divIndexY=mineY[i]+1;
}
if(divIndexX<0||divIndexY<0||divIndexX>=RowSize||divIndexY>=ColSize) continue;
Decouv[divIndexX][divIndexY]+=1;
Mine[divIndexX][divIndexY]=Decouv[divIndexX][divIndexY];
 }
}
//模式切换

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[indexX][indexY]=Decouv[indexX][indexY];
Decouv[indexX][indexY]=18;
return;}
else{
Elem.value="";
Decouv[indexX][indexY]=Mine[indexX][indexY];
return;
}
}
if(Decouv[indexX][indexY]>8){
if(Decouv[indexX][indexY]>=18)
return;
Elem.value="*";
Elem.className="ye";
Decouv[indexX][indexY]=9
return;
}
Elem.className="no";
Elem.value=""+Mine[indexX][indexY];
if(Decouv[indexX][indexY]>0){
Decouv[indexX][indexY]=9
return;
 }
if(Decouv[indexX][indexY]==0)
{
Elem.value="";
tryGo(indexX,indexY);
}
return;
}
function TRY2(Elm){
Elm.value="!";
}
function judge4(x,y){
if(Decouv[x][y]!=-1&&Decouv[x][y]!=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[divIndexX][divIndexY]>=0&&Decouv[divIndexX][divIndexY]<=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[i]=divIndexX;stackY[i]=divIndexY;
while(test){
 var Elem;
 eval("Elem=document.all(\""+(divIndexX+1)+"_"+(divIndexY+1)+"\")");
 Elem.className="no";
 if(Decouv[divIndexX][divIndexY]<=0)
  Decouv[divIndexX][divIndexY]= -1;
 else{
  Elem.value=""+Mine[divIndexX][divIndexY];
  Decouv[divIndexX][divIndexY]= 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[i];divIndexY=stackY[i];
 continue;
  }
 i=i+1;stackX[i]=divIndexX;stackY[i]=divIndexY;
 }
}
document.onclick =TRY;
//-->
</SCRIPT>
</HTML>

本文作者:



相关阅读:
PHP开源网站程序如何使用的更好
jQuery ajax在GBK编码下表单提交终极解决方案(非二次编码方法)
PHP 分页类代码(简单好用型)
PHPnow轻松打造专业PHP服务器环境
css自动换行 防止撑破div影响排版
经验总结:VMware虚拟机上安装Linux系统
Append打开指定文件夹中的数据文件就象这些文件在当前文件夹中一样
php 生成静态页面的办法与实现代码详细版
ASP.NET实现数据采集
[HTML/CSS/Javascript]WWTJS
Atlas学习手记(12):使用CascadingDropDown控件
MySQL 5.0触发器参考教程
jquery 查找iframe父级页面元素的实现代码
js Clip的奇思妙想之文字拼接效果
快速导航

Copyright © 2016 phpStudy |