重新写了一遍
这次不使用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> </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>