javascript 扫雷游戏示例
时间:2007-02-17 来源:PHP爱好者
<HTML>
<HEAD>
<TITLE>Javascript 地雷</TITLE>
<style>
input.no {
background-color:eeeeee;
border:none;
width:20;
color:blue;
}
input.ye {
background-color:eeeeee;
border:none;
width:20;
color:red;
}
input {
width:20;
color:red;
}
input.mode0 {
width:50;
border-style:inset
}
input.mode1 {
width:50;
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;/**/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[Row-1][Col-1]=0;
Decouv[Row-1][Col-1]=0;
}
};
//布地雷 Math.random( )
i=0;
while (i<mineNum){
mineX=Math.round(Math.random()*1000)%RowSize;
mineY=Math.round(Math.random()*1000)%ColSize;
if(Mine[mineX][mineY]==9) continue;
Mine[mineX][mineY]=9;
Decouv[mineX][mineY]=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[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=divIndexX;stackY=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;divIndexY=stackY;
continue;
}
i=i+1;stackX=divIndexX;stackY=divIndexY;
}
}
document.onclick =TRY;
//-->
</SCRIPT>
</HTML>
php爱好者站 http://www.phpfans.net 为phper提供一切资讯.