经典游戏扫雷之js版

闲来无事,js写个扫雷玩玩。兼容IE6,7,8,FF,CHROME。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title> 扫雷 </title>

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

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

</head>

<style>

td{cursor: pointer; text-align: center;}

.table1{border:1px solid #ffffff;}

.table1 td{background-color: #F9DAB0; width:20px; height:20px;}

.table1 .td_hover{background-color: #F5C481;}

.table1 .td_checked{background-color: #CCFFFF;}

.lvl{cursor:pointer;}

#leishu, #shijian{background-color:#000000;color:#ffffff;width:30px;height:20px;text-align:center;}

</style>

<SCRIPT LANGUAGE="JavaScript">

<!--

function $(id){

    return document.getElementById(id);

}

function startWithLv(lvl){

    window.clearInterval(saolei.timeId);

    switch(lvl){

        case 1:

            saolei.instance(9,9,10);

            break;

        case 2:

            saolei.instance(16,16,40);

            break;

        case 3:

            saolei.instance(16,30,99);

            break;

    }

}

window.onload = function(){

    saolei.instance(9,9,10);

}

 

var saolei = {};

/*

* 函数说明:实例化对象

* 参数:rowLen:地图行数,colLen

* 返回值:

*

*/

saolei.instance = function(rowLen,colLen,bombNum){

    this.arrBombs = new Array();   //

    this.tableRows = rowLen;                //地图行数

    this.tableCols = colLen;                //地图列数

    this.BombNum = bombNum;                 //地雷个数

    this.BombLeft = bombNum;                //剩余雷数

    this.iCellIsConfirm = 0;                //被玩家确定无雷的或有雷的格子数

    this.firstClick = true;                 //记录是否的一次点击,的一次点的时候开始记时

    this.curT = 0;                          //当前时间

    this.timeId = 0;                        //计时器

    this.loadMap();

}

/*

* 函数说明:初始化地图

* 参数:a:地图行数,b:地图列数

* 返回值:

*

*/

saolei.loadMap = function(){

    var thisobj = this;

    var cellIndex = 0;                  //单元格索引

    var divObj = $("div1");

    if(divObj == null){

        divObj = document.createElmentById("div");

        document.body.appendChild(divObj);

    }

    divObj.innerHTML = "<table id='table1' class='table1'><tbody></tbody></table>";

    var tbody = divObj.getElementsByTagName("tbody")[0];

    for(var j = 0 ; j < this.tableRows ; j++){

        var tr = document.createElement("tr");

        tbody.appendChild(tr);

        for( k = 0 ; k < this.tableCols ; k++){

            var td = document.createElement("td");

            td.id="td_" + j + "_" + k;

            td.row = j;

            td.col = k;

            td.innerHTML = "&nbsp;";

            td.hasBomb = false;                     //单元格是否有雷,false-无地雷,true-有地雷

            td.state = "0";  //单元格被玩家点击状态,0-无标记 1-标记有雷,显示*,2-无雷,显示数字或空白,3-可能有雷,显示?

            td.onmousedown = function(event){

                var e = event ? event : (window.event ? window.event : null);

                thisobj.tdonclick(this, e.button);

            }

            td.onmouseover = function(event){

                if(this.state != 2){

                    this.className = "td_hover";

                }

            }

            td.onmouseout = function(event){

                if(this.state != 2){

                    this.className = "";

                }

            }

            tr.appendChild(td);

            cellIndex = cellIndex + 1;

        }

    }

    var spanTime = $("shijian");

    spanTime.innerHTML = "000";

    this.showleishu();

    this.init();

}

saolei.init = function(){

    var rowIndex = 0;

    var colIndex = 0;

    var tdobjs = $("table1").getElementsByTagName("td");

    var arrIndex = 1;

    for(var i = 0 ; i < this.tableRows*this.tableCols ; i++){

        this.arrBombs[i] = i;

    }

    for(i = 0 ; i < this.BombNum ; i++){

        arrIndex = parseInt(this.arrBombs.length * Math.random());

        tdobjs[this.arrBombs[arrIndex]].hasBomb = true;

        this.arrBombs.splice(arrIndex,1);

    }

}

saolei.reload = function(){

    this.instance(this.tableRows, this.tableCols, this.BombNum);

}

saolei.tdonclick = function(cellobj, btState){

    if(this.firstClick == true){

        this.firstClick = false;

        this.timeId = window.setInterval("saolei.jishi()",1000);

    }

    //鼠标右键点击

    if(btState == 2){

        if(cellobj.state == 0 && this.BombLeft > 0){

            cellobj.state = 1;

            cellobj.innerHTML = "*";

            this.iCellIsConfirm++;

            this.BombLeft = this.BombLeft - 1;

            this.showleishu();

        }

        else if(cellobj.state == 1){

            cellobj.state = 3;

            cellobj.innerHTML = "?";

            this.iCellIsConfirm--;

            this.BombLeft = this.BombLeft + 1;

            this.showleishu();

        }

        else if(cellobj.state == 3){

            cellobj.state = 0;

            this.iCellIsConfirm++;

            cellobj.innerHTML = "&nbsp;";

        }

        else if(cellobj.state != 2){

            alert("标志牌用完了!");

        }

    }

    //鼠标左键点击

    else if(btState == 1 || btState == 0){

        if(cellobj.state == 0){

            if(cellobj.hasBomb == false){

                cellobj.className = "td_checked";

                cellobj.state = 2;

                this.iCellIsConfirm = this.iCellIsConfirm + 1;

 

                this.showBombCountAround(cellobj);

            }

            else if(cellobj.hasBomb == true){

                var tdobjs = $("table1").getElementsByTagName("td");

                window.clearInterval(this.timeId);

                for(var i = 0 ; i < this.tableRows*this.tableCols ; i++){

                    tdobjs[i].innerHTML = (tdobjs[i].hasBomb==true?"雷":tdobjs[i].innerHTML);

                    tdobjs[i].onmousedown = function(){}

                }

                if(window.confirm("真遗憾,您输了!再接再厉!/n 再玩一局?")){

                    saolei.instance(saolei.tableRows, saolei.tableCols, saolei.BombNum);

                }else{

                    window.close();

                }

            }

        }

    }

    //玩家判断玩所有的格子,系统开始判断玩家是否过关

    if(this.iCellIsConfirm == this.tableRows*this.tableCols){

        this.isWin();

    }

}

//判断是否通关

saolei.isWin = function(){

    var tdobjs = $("table1").getElementsByTagName("td");

    for(var i = 0 ; i < this.tableRows*this.tableCols ; i++){

        if(tdobjs[i].hasBomb == false){

            if(tdobjs[i].state != 2){

                return;

            }

        }else{

            if(tdobjs[i].state != 1){

                return;

            }

        }

    }

    window.clearInterval(this.timeId);

    if(window.confirm("你真棒!共用时“"+this.curT+"”")){

        saolei.instance(saolei.tableRows, saolei.tableCols, saolei.BombNum);

    }else{

        window.close();

    }

}

//判断并显示周围的雷数

saolei.showBombCountAround = function(cellobj){

    var bombCountAround = 0;

    var row = cellobj.row;

    var col = cellobj.col;

    for(var i = row - 1 ; i <= row + 1 ; i++){

        for(var j = col - 1 ; j <= col + 1 ; j++){

            var id = "td_" + i + "_" + j;

            var obj = $(id);

            if(obj != null){

                if(obj.hasBomb == true){

                    bombCountAround = bombCountAround + 1;

                }

            }

        }

    }

    if(bombCountAround != 0){

        cellobj.innerHTML = bombCountAround;

    }

    else{

        for(var i = row - 1 ; i <= row + 1 ; i++){

            for(var j = col - 1 ; j <= col + 1 ; j++){

                var id = "td_" + i + "_" + j;

                var obj = $(id);

                if(obj != null){

                    this.tdonclick(obj, 1);

                }

            }

        }

    }

}

saolei.showleishu = function(){

    var leishu = $("leishu");

    var strBombNum = "";

    if(this.BombLeft >= 0 && this.BombLeft < 10){

        strBombNum = "00" + this.BombLeft;

    }

    else if(this.BombLeft >= 10 && this.BombLeft <100){

        strBombNum = "0" + this.BombLeft;

    }

    leishu.innerHTML = strBombNum;

}

saolei.jishi = function(){

    var spanTime = $("shijian");

    this.curT = this.curT + 1;

    var curT = "";

    if(this.curT >= 0 && this.curT < 10){

        curT = "00" + this.curT;

    }

    else if(this.curT >= 10 && this.curT <100){

        curT = "0" + this.curT;

    }

    else if(this.curT >= 100 && this.curT < 1000){

        curT = this.curT.toString();

    }

    else{

        window.clearInterval(this.timeId);

        if(window.confirm("很遗憾,你超时了!/n 重新开始?")){

            saolei.instance(saolei.tableRows, saolei.tableCols, saolei.BombNum);

        }else{

            window.close();

        }

    }

    spanTime.innerHTML = curT;

}

//显示格子里是否有雷

saolei.showBomb = function(){

    var tdobjs = $("table1").getElementsByTagName("td");

    for(var i = 0 ; i < this.tableRows*this.tableCols ; i++){

        tdobjs[i].innerHTML = (tdobjs[i].hasBomb==true?"雷":"&nbsp;");

    }

    return "启用秘笈成功";

}

//-->

</SCRIPT>

</HEAD>

<BODY οncοntextmenu="return false">

<div>

  <span>难度:</span>

  <span class="lvl" οnclick="startWithLv(1)">低</span>

  <span class="lvl" οnclick="startWithLv(2)">中</span>

  <span class="lvl" οnclick="startWithLv(3)">高</span>

</div>

<div>

  <span>剩余地雷数</span>

  <span id="leishu">000</span>

  <span>时间</span>

  <span id="shijian">000</span>

</div>

<div id="div1"></div>

</BODY>

</HTML>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值