10年前写的js版挖地雷

这是10年前写的js版挖地雷,好像是ie5.5时代吧,没什么兼容性这么一说,也没jquery一说,纯原生,拿出来改写一下,增加兼容性,代码就不优化了,bug也有,不调了,将就看吧,刚入行时候写的。

<html>
<head>

</head>
<body>
<table>
    <tr>
        <td id="showstatus">快来挖吧,左击掏空地,右击标地雷,双击快速排雷</td><td id="showtime"></td>
    </tr>
    <tr>
        <td colspan="2" id="tempshow"><input type="radio" name="radio1" value="8">8*8<input type="radio" name="radio1" value="16">16*16</td>
    </tr>
</table>
<table id="minetable" align="center" border="1" cellpadding="4" cellspacing="0" bordercolordark="#000000" bordercolorlight="#000000">
</table>
<script language="javascript">
    var isbegin=false;
    var timecounter=0;
    var allcount=10;
    var tableLength=8;
    var MineArray=new Array();
    var none_mine=tableLength*tableLength-allcount;
    var realcount=0;
    var virtcount=0;
    var blankcount=0;
    var gamestatus=true;
    var mineMakercount=0;
    var mytable = document.getElementById('minetable');
    var showstatus = document.getElementById('showstatus');
    var tempshow = document.getElementById('tempshow');
function initfunction(){
    for(var i=-1;i<=tableLength;i++){
        MineArray[i]=new Array();
        for(var j=-1;j<=tableLength;j++){
            MineArray[i][j]=0;
        }
    }
    while(mineMakercount<allcount){
        random1 = Math.ceil(Math.random()*tableLength);
        random2 = Math.ceil(Math.random()*tableLength);
        if(MineArray[random1-1][random2-1] != 1){
            MineArray[random1-1][random2-1]=1;
            mineMakercount++;
        }
    }
}

function digform(CorrentE,isdbl){
	target = gettarget(getevt(event));
    if(target.getAttribute('type')=="radio"){
        modelchange(target.value);
        return false;
    }
    if(!isbegin){
        isbegin=true;
        initfunction();
        counter();
    }
    if(!gamestatus)
        return false;
    var tempcount=0;
    var allmarked=0;
    var virtmarked=0;
    var RIndex=CorrentE.parentElement.rowIndex;
    var CIndex=CorrentE.cellIndex;
    var errorX=0;
    var errorY=0;
    if(MineArray[RIndex][CIndex]==1 && isdbl!=1){
        CorrentE.innerHTML="◎";
        CorrentE.style.background="red";
        alert("game over1");
        gamestatus=false;
        isbegin=false;
    }else if(MineArray[RIndex][CIndex]==0 || MineArray[RIndex][CIndex]==5){
        if(MineArray[RIndex][CIndex]==0)
            blankcount++;
        MineArray[RIndex][CIndex]=3;
        for(var i=RIndex-1;i<=RIndex+1;i++){
            for(var j=CIndex-1;j<=CIndex+1;j++){
                if(MineArray[i][j]==1 || MineArray[i][j]==2)
                    tempcount++;
                if(MineArray[i][j]==2)
                    allmarked++;
                if(MineArray[i][j]==4){
                    virtmarked++;
                    errorX=i;
                    errorY=j;
                }
            }
        }
        if(isdbl==1 && virtmarked!=0){
            mytable.rows[errorX].cells[errorY].innerHTML="◎";
            mytable.rows[errorX].cells[errorY].style.background="red";
            alert("game over2");
            gamestatus=false;
            isbegin=false;
            virtmarked=0;
            return false
        }
        if(tempcount!=0)
            CorrentE.innerHTML=" "+tempcount;
        CorrentE.style.background="#dddddd";
        if(tempcount==0 || allmarked==tempcount){
            for(var i=RIndex-1;i<=RIndex+1;i++){
                for(var j=CIndex-1;j<=CIndex+1;j++){
                    if(i>=0 && i<tableLength && j>=0 && j<tableLength)
                        digform(mytable.rows[i].cells[j]);
                }
            }
        }
        tempcount=0;
        allmarked=0;
        virtmarked=0;
        if(blankcount==none_mine){
            tailkiller();
            gamestatus=false;
            isbegin=false;
        }
        showstatus.innerHTML="还有"+(allcount-virtcount)+"颗雷";
    }
}

function rightclick(CorrentE,f){

    if(!isbegin){
        isbegin=true;
        initfunction();
        counter();
    }

    target = gettarget(getevt(event));

    if(target.getAttribute('type')=="td" && f==null){
        CorrentE=target;
    }else if(CorrentE!=null && f){
    }else{
        return false;
    }
    if(!gamestatus)
        return false;
    var RIndex=CorrentE.parentElement.rowIndex;
    var CIndex=CorrentE.cellIndex;
    if(MineArray[RIndex][CIndex]==1){
        MineArray[RIndex][CIndex]=2;
        realcount++;
        virtcount++;
        CorrentE.innerHTML="◎";
        CorrentE.style.background="#dddddd";
    }else if(MineArray[RIndex][CIndex]==2){
        realcount--;
        virtcount--;
        MineArray[RIndex][CIndex]=1;
        CorrentE.innerHTML=" ";
        CorrentE.style.background="#ffffff";
    }else if(MineArray[RIndex][CIndex]==0){
        MineArray[RIndex][CIndex]=4
        virtcount++;
        CorrentE.innerHTML="◎";
        CorrentE.style.background="#dddddd";
    }else if(MineArray[RIndex][CIndex]==4){
        MineArray[RIndex][CIndex]=0;
        virtcount--;
        CorrentE.innerHTML=" ";
        CorrentE.style.background="#ffffff";
    }
    if(realcount==allcount){
        tailkiller();
        gamestatus=false;
        isbegin=false;
    }
    showstatus.innerHTML="还有"+(allcount-virtcount)+"颗雷";
    return false
}
function modelchange(minemodel){
    isbegin=false;
    timecounter=0;
    realcount=0;
    virtcount=0;
    blankcount=0;
    gamestatus=true;
    mineMakercount=0;
    if(minemodel==16){
        allcount=40;
        tableLength=16;
        MineArray=new Array();
        none_mine=tableLength*tableLength-allcount;
    }else if(minemodel==8){
        allcount=10;
        tableLength=8;
        MineArray=new Array();
        none_mine=tableLength*tableLength-allcount;
    }
    var templength=mytable.rows.length;
    for(var i=0;i<templength;i++){
        mytable.deleteRow();
    }
    for(var i=0;i<tableLength;i++){
        newrows=mytable.insertRow();
        for(var j=0;j<tableLength;j++){
            newcells=newrows.insertCell();
            newcells.setAttribute("type","td");
            newcells.innerHTML="&nbsp;&nbsp;";
        }
    }
}
function counter(){
    showtime.innerHTML="用时:"+timecounter++;
    if(isbegin)
        setTimeout("counter()",1000);
}

function dblclick(){
	target = gettarget(getevt(event));
    if(target.getAttribute('type')=="td"){
        var RIndex=target.parentElement.rowIndex;
        var CIndex=target.cellIndex;
        if(MineArray[RIndex][CIndex]==3){
            MineArray[RIndex][CIndex]=5;
            digform(target,1);
        }
    }
    return false;
}

function tailkiller(){
    for(var i=0;i<mytable.rows.length;i++)
        for(var j=0;j<mytable.rows[i].cells.length;j++){
            if(MineArray[i][j]==0){
                digform(mytable.rows[i].cells[j]);
            }
            if(MineArray[i][j]==1)
                rightclick(mytable.rows[i].cells[j],1);
        }
}

function leftclick(event){
	target = gettarget(getevt(event));
    if(target.getAttribute('type')=="radio" || target.getAttribute('type')=="td")
        digform(target,0);
}

function getevt(event){
        event = (event ? event : window.event);
        if (event == null) {
            var $E = function(){
                var c = $E.caller;
                while (c.caller) 
                    c = c.caller;
                return c.arguments[0]
            };
            __defineGetter__("event", $E);
        }
        return event;
    }
function gettarget(event){
	return event.target || event.srcElement;
}

document.oncontextmenu=rightclick;
document.onclick=leftclick;
document.ondblclick=dblclick;
</script>
</body>
</html>

 

发表评论