分享
 
 
 

javascript扫雷游戏,版本二

王朝html/css/js·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

重新写了一遍

这次不使用Button元素

只用了一个table

而且加入了输赢判断,双键的试探功能,优化了打开空白区域的算法,还有关卡的设定 :)

总之,现在已经比较完善了

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<meta name="Author" content="Gray·Chan(graycarl)">

<meta name="Version" content="2.0">

<meta name="Date" content="2003.4.8">

<meta name="Date" content="2003.4.9">

<meta name="Corpright" content="http://gray.ipaopao.com">

<title>扫雷游戏</title>

<style>

<!--

td {overflow:hidden;

font-family: Arial Black; font-size: 11px;

text-align: center;

border-left: 2 solid #FFFFFF;

border-right: 2 solid #808080;

border-top: 2 solid #FFFFFF;

border-bottom: 2 solid #808080;

background-color: #C0C0C0;

cursor:default}

td.down {border-left: 3 solid #C0C0C0;

border-right: 1 solid #808080;

border-top: 3 solid #C0C0C0;

border-bottom: 1 solid #808080;}

td.marked {font-family: Wingdings; color: #ff0000}

-->

</style>

</head>

<body>

<div id=DWC0 style="BACKGROUND-COLOR: silver;font-size:12px;color:red;HEIGHT: 26px; LEFT: 201px; POSITION: absolute; TOP: 121px; WIDTH: 173px">

请稍候,正在装载……</div>

<div id=DWC1 style="HEIGHT: 150px; LEFT: -5px; POSITION: absolute; TOP: -16px; VISIBILITY: hidden; WIDTH: 157px"><font face="Webdings" color=#ffe0c1

style="FONT-SIZE: 150px">*</font></div>

<div id=DWC2 style="COLOR: red; FONT-SIZE: 12px; HEIGHT: 18px; LEFT: 31px; POSITION: absolute; TOP: 14px; VISIBILITY: hidden; WIDTH: 103px">地雷

数:<span ID=MINENUMSCREEN style="FONT-FAMILY: Arial Black; FONT-SIZE: 14px">0</span></div>

<div id=DWC3 style="HEIGHT: 299px; LEFT: 1px; POSITION: absolute; TOP: 31px; VISIBILITY: hidden; WIDTH: 419px"><!--STATUS = 0 按钮

状态--><!--STATUS = 1 固定按钮状态--><!--STATUS = 2 点开状态--><!--MINE = 0 无雷

--><!--MINE = 1 有雷-->

<SCRIPT language=javascript version="1.1">

var ROWS=18;

var COLS=22;

var MINE=0;

var STATUS=0;

var MINEPRAM=20;

var Elems=new Array(ROWS);

var Stack=new Array(ROWS*COLS);

var StackPoint=-1;

var MINECOUNT=0;

var LocationHash=document.location.hash;

var idx=LocationHash.indexOf('#');

if(idx>=0) LocationHash=LocationHash.substr(1);

if(!isNaN(LocationHash)&&LocationHash!="")MINEPRAM=parseInt(LocationHash);

var NextLevelLocation=document.location.href;

idx=NextLevelLocation.indexOf('#');

if(idx>=0) NextLevelLocation=NextLevelLocation.substr(0,idx);

NextLevelLocation+="#"+Math.round(MINEPRAM*0.8);

document.writeln("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#808080\" width="+20*COLS+" height="+20*ROWS+">")

for(var i=1;i<=ROWS;i++){

Elems[i-1]=new Array(COLS);

document.writeln(" <tr>");

for(var j=1;j<=COLS;j++){

if(Math.floor(Math.random()*MINEPRAM)==0){

MINE=1;

STATUS=0;

MINECOUNT++;

}

else{

MINE=0;

STATUS=0;

}

document.writeln(" <td id=\"btn"+j+"_"+i+"\" I=\""+i+"\" J=\""+j+"\" STATUS=\""+STATUS+"\" MINE=\""+MINE+"\" MINENUM=\"0\" Class=\"\"

width=15 height=15>&nbsp;</td>");

}

document.writeln(" </tr>");

}

document.writeln("</table>");

document.all.DWC0.innerText="请稍候,正在准备游戏信息……";

</SCRIPT>

</div>

<script language="javascript" version="1.1">

var downElem=null;

var YOrder,XOrder;

var clickStyle=0;

function GetSTATUSNum(Elem,XOrder,YOrder){

var STATUS=0;

if(Elem.getAttribute("STATUS")=="1") return 99;

var curElem;

try{

curElem=Elems[XOrder-1][YOrder-1];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder-1][YOrder];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder-1][YOrder+1];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder][YOrder-1];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder][YOrder+1];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder+1][YOrder-1];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder+1][YOrder];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

try{

curElem=Elems[XOrder+1][YOrder+1];

if(curElem.getAttribute("STATUS")=="1") STATUS++;

}catch(e)

{

}

return STATUS;

}

function GetMINENum(Elem,XOrder,YOrder){

var MINE=0;

if(Elem.getAttribute("MINE")=="1") return 99;

var curElem;

try{

curElem=Elems[XOrder-1][YOrder-1];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder-1][YOrder];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder-1][YOrder+1];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder][YOrder-1];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder][YOrder+1];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder+1][YOrder-1];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder+1][YOrder];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

try{

curElem=Elems[XOrder+1][YOrder+1];

if(curElem.getAttribute("MINE")=="1") MINE++;

}catch(e)

{

}

return MINE;

}

function GetMINENumEx(Elem){

return parseInt(Elem.getAttribute("MINENUM"));

}

function markBtn(Elem){

if(Elem==null) return;

if(Elem.getAttribute("STATUS")=="2") return;

if(Elem.getAttribute("STATUS")=="1") {

Elem.setAttribute("STATUS","0");

document.all.MINENUMSCREEN.innerText = (++MINECOUNT);

Elem.innerText=" ";

Elem.className="";

return;

}

if(Elem.getAttribute("STATUS")=="0") {

Elem.setAttribute("STATUS","1");

document.all.MINENUMSCREEN.innerText = (--MINECOUNT);

Elem.innerText="P";

Elem.className="marked";

return;

}

}

function autoOpenBtns(Elem){

var curElem;

var XOrder,YOrder;

if(Elem.getAttribute("STATUS")!="2") return;

YOrder=parseInt(Elem.getAttribute("I"))-1;

XOrder=parseInt(Elem.getAttribute("J"))-1;

if(GetSTATUSNum(Elem,YOrder,XOrder)!=GetMINENumEx(Elem)) return;

try{

curElem=Elems[YOrder-1][XOrder-1];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder-1][XOrder];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder-1][XOrder+1];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder][XOrder-1];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder][XOrder+1];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder+1][XOrder-1];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder+1][XOrder];

openBtns(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder+1][XOrder+1];

openBtns(curElem);

}catch(e)

{

}

}

function push(Elem){

if(Elem==null) return;

Stack[++StackPoint]=Elem;

return;

}

function pop(){

try{

return Stack[StackPoint--];

}

catch(e){

return null;

}

}

function openBtns(Elem){

var curElem=Elem;

while(curElem!=null){

if(curElem.getAttribute("STATUS")=="0") openBtnsEx(curElem);

curElem=pop();

}

}

function openBtnsEx(Elem){

var XOrder,YOrder;

YOrder=parseInt(Elem.getAttribute("I"))-1;

XOrder=parseInt(Elem.getAttribute("J"))-1;

var MineNum=GetMINENumEx(Elem);

if(MineNum==99) return FailAndOpenAllMine();

Elem.className="down";

Elem.setAttribute("STATUS","2");

if(MineNum>0){

Elem.innerText=MineNum;

Elem.style.color="0000ff";

if(MineNum>1){

Elem.style.color="00ff00";

if(MineNum>2){

Elem.style.color="ff0000";

if(MineNum>3){

Elem.style.color="000099";

if(MineNum>4){

Elem.style.color="009900";

if(MineNum>5){

Elem.style.color="990000";

if(MineNum>6){

Elem.style.color="000033";

if(MineNum>7){

Elem.style.color="003300";

}

}

}

}

}

}

}

}

else{

var curElem;

try{

curElem=Elems[YOrder-1][XOrder-1];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder-1][XOrder];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder-1][XOrder+1];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder][XOrder-1];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder][XOrder+1];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder+1][XOrder-1];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder+1][XOrder];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

try{

curElem=Elems[YOrder+1][XOrder+1];

if(curElem.getAttribute("STATUS")=="0")push(curElem);

}catch(e)

{

}

}

}

function FailAndOpenAllMine(){

for(var i=0;i<ROWS;i++){

for(var j=0;j<COLS;j++){

var curElem=

Elems[i][j];if(curElem.getAttribute("STATUS")=="1"&&curElem.getAttribute("MINE")=="0"){

curElem.innerHTML="<font face=\"Webdings\" color=red>r</font>";

curElem.className=

"down";}if(curElem.getAttribute("STATUS")=="0"&&curElem.getAttribute("MINE")=="1"){

curElem.innerHTML="<font face=\"Wingdings\">M</font>";

curElem.className="down";

}

}

}

alert("你失败了! 按[确定]重新开始。");

setTimeout("document.location.reload()",100);

return true;

}

function elemClick(Elem){

if(clickStyle==1) openBtns(Elem);

if(clickStyle==2) markBtn(Elem);

if(clickStyle==3) autoOpenBtns(Elem);

CheckIfVictory();

}

function setElemDown(Elem){

var curElem=null;

YOrder=parseInt(Elem.getAttribute("I"));

XOrder=parseInt(Elem.getAttribute("J"));

if(clickStyle==1){

if(Elem.getAttribute("STATUS")=="0") Elem.className="down";

}

if(clickStyle==2){

}

if(clickStyle==3){

if(Elem.getAttribute("STATUS")=="0") Elem.className="down";

curElem=

eval("document.all.btn"+(XOrder-1)+"_"+(YOrder-1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0")

curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder-1)+"_"+(YOrder));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder-1)+"_"+(YOrder+1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0")

curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder)+"_"+(YOrder-1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder)+"_"+(YOrder+1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder+1)+"_"+(YOrder-1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0")

curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder+1)+"_"+(YOrder));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="down";

curElem=

eval("document.all.btn"+(XOrder+1)+"_"+(YOrder+1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0")

curElem.className="down";

}

}

function docmdown(){

if(event.srcElement.tagName=="TD"){

clickStyle=event.button;

setElemDown(event.srcElement)

downElem=event.srcElement;

}

}

function docmup(){

if(downElem==null) return;

clearbtnState();

if(event.srcElement == downElem){

elemClick(downElem);

}

downElem=null;

}

function clearbtnState(){

if(downElem==null) return;

if(downElem.getAttribute("STATUS")=="0") downElem.className="";

if(clickStyle==3){

var curElem;

curElem=

eval("document.all.btn"+(XOrder-1)+"_"+(YOrder-1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder-1)+"_"+(YOrder));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder-1)+"_"+(YOrder+1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder)+"_"+(YOrder-1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder)+"_"+(YOrder+1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder+1)+"_"+(YOrder-1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder+1)+"_"+(YOrder));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

curElem=

eval("document.all.btn"+(XOrder+1)+"_"+(YOrder+1));if(curElem!=null&&curElem.getAttribute("STATUS")=="0") curElem.className="";

}

}

function docmout(){

clearbtnState();

downElem=null;

}

function DisabledEvent(){

return false;

}

function CheckIfVictory(){

for(var i=0;i<ROWS;i++){

for(var j=0;j<COLS;j++){

var curElem=

Elems[i][j];if(curElem.getAttribute("MINE")=="0"&&curElem.getAttribute("STATUS")!="2"){

return false;

}

}

}

for(var i=0;i<ROWS;i++){

for(var j=0;j<COLS;j++){

var curElem=

Elems[i][j];if(curElem.getAttribute("STATUS")=="0"&&curElem.getAttribute("MINE")=="1"){

markBtn(curElem);

}

}

}

alert("完成任务! 按[确定]开始新的任务");

setTimeout("window.document.close();window.document.open();document.location.replace(\""+NextLevelLocation+"\")",100);

return true;

}

for(var i=0;i<ROWS;i++){

for(var j=0;j<COLS;j++){

Elems[i][j]=eval("document.all.btn"+(j+1)+"_"+(i+1));

}

}

for(var i=0;i<ROWS;i++){

for(var j=0;j<COLS;j++){

var curElem=Elems[i][j];

curElem.setAttribute("MINENUM",GetMINENum(curElem,i,j));

}

}

alert("前方"+MINECOUNT+"个地雷出现!");

document.all.DWC0.style.visibility="hidden";

document.all.DWC1.style.visibility="visible";

document.all.DWC2.style.visibility="visible";

document.all.DWC3.style.visibility="visible";

document.all.MINENUMSCREEN.innerText = MINECOUNT;

document.onmousedown=docmdown;

document.onmouseup=docmup;

document.onmouseout=docmout;

document.oncontextmenu=DisabledEvent;

document.onselectstart=DisabledEvent;

</script>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有