GVKun编程网logo

JS实现扫雷项目总结(js实现扫雷项目总结报告)

25

以上就是给各位分享JS实现扫雷项目总结,其中也会对js实现扫雷项目总结报告进行解释,同时本文还将给你拓展Android实现扫雷小游戏、C#实现扫雷游戏、C++实现扫雷小游戏(控制台)、c++实现扫雷小

以上就是给各位分享JS实现扫雷项目总结,其中也会对js实现扫雷项目总结报告进行解释,同时本文还将给你拓展Android实现扫雷小游戏、C#实现扫雷游戏、C++实现扫雷小游戏(控制台)、c++实现扫雷小游戏代码分享等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JS实现扫雷项目总结(js实现扫雷项目总结报告)

JS实现扫雷项目总结(js实现扫雷项目总结报告)

本文实例为大家分享了JS实现扫雷项目的总结,供大家参考,具体内容如下

项目展示图

项目准备

一样的,我们先是准备出三个文件夹,以及根目录下的index.html 文件

然后是两张图片(地雷 和 旗子)

之后是html结构

html

首先是最外层的 游戏内容区域的div 取名id为mine

<div id="mine">
</div>

之后是游戏内容区域中最上面的四个按钮,我们用四个button标签来表示,并且用一个div来包裹起来

并且给初级按钮一个最初的选中的样式

<div>
    <button>初级</button>
    <button>中级</button>
    <button>高级</button>
    <button>重新开始</button>
</div>

之后是 game 的游戏进行区域,也就是雷区

<div>
</div>

最后一个是提示区域

<div>
    剩余雷数:<span></span>
    <br>
    <span>左键扫雷,右键插旗,再次点击右键拔旗</span>
</div>

那么最后,我们整合一下 最后的代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS扫雷</title>
    <link rel="stylesheet" href="CSS/index.css" >
    <link rel="icon" href="favicon.ico" >
</head>

<body>
    <div id="mine">
        <div>
            <button>初级</button>
            <button>中级</button>
            <button>高级</button>
            <button>重新开始</button>
        </div>
        <div>
        </div>
        <div>
            剩余雷数:<span></span>
            <br>
            <span>左键扫雷,右键插旗,再次点击右键拔旗</span>
        </div>
    </div>
    <script src="JS/index.js"></script>
</body>

</html>

CSS

首先呢我们给mine最外层的区域,让主内容居中

#mine {
    margin: 50px auto;
}

之后,给level 的 div 和 里面的 button 添加样式

并且,添加默认的active样式

.level {
    text-align: center;
    margin-bottom: 10px;
}

.level button {
    padding: 5px 15px;
    background-color: #02a4ad;
    border: none;
    color: #fff;
    border-radius: 3px;
    outline: none;
    cursor: pointer;
}

.level button.active {
    background-color: #00abff;
}

之后,我们先重定义了下 table标签 和 td 标签,我们的扫雷主要的游戏区域就是通过table标签来实现

table {
    border-spacing: 1px;
    background-color: #929196;
    margin: 0 auto;
}

td {
    padding: 0;
    width: 20px;
    height: 20px;
    background-color: #ccc;
    border: 2px solid;
    border-color: #fff #a1a1a1 #a1a1a1 #fff;
    text-align: center;
    line-height: 20px;
    font-weight: bold;

}

之后是提示区域的样式

.info {
    margin-top: 10px;
    text-align: center;
}

.tips {
    color: red;
    font-size: 16px;
}

最后,我们预定义一些样式

比如,我们的地雷的样式,以及我们的棋子的样式

.mine {
    background: #d9d9d9 url(../images/mine.png) no-repeat center;
    background-size: cover;
}
.flag {
    background: #ccc url(../images/flag.png) no-repeat center;
    background-size: cover;
}

最后还有一个就是游戏内,一些方块会现实数字,这些数字代表了改方块周围的雷的数量

td.zero {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
}

td.one {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #0332fe;
}

td.two {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #019f02;
}

td.three {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #ff2600;
}

td.four {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #93208f;
}

td.five {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #ff7f29;
}

td.six {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #ff3fff;
}

td.seven {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #3fffbf;
}

td.eight {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #22ee0f;
}

所以综上所述,整合了下css代码,我们的css完整的代码如下

#mine {
    margin: 50px auto;

}

.level {
    text-align: center;
    margin-bottom: 10px;
}

.level button {
    padding: 5px 15px;
    background-color: #02a4ad;
    border: none;
    color: #fff;
    border-radius: 3px;
    outline: none;
    cursor: pointer;
}

.level button.active {
    background-color: #00abff;
}

table {
    border-spacing: 1px;
    background-color: #929196;
    margin: 0 auto;
}

td {
    padding: 0;
    width: 20px;
    height: 20px;
    background-color: #ccc;
    border: 2px solid;
    border-color: #fff #a1a1a1 #a1a1a1 #fff;
    text-align: center;
    line-height: 20px;
    font-weight: bold;

}

.tips {
    color: red;
    font-size: 16px;
}

.mine {
    background: #d9d9d9 url(../images/mine.png) no-repeat center;
    background-size: cover;
}

.flag {
    background: #ccc url(../images/flag.png) no-repeat center;
    background-size: cover;
}

.info {
    margin-top: 10px;
    text-align: center;
}


td.zero {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
}

td.one {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #0332fe;
}

td.two {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #019f02;
}

td.three {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #ff2600;
}

td.four {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #93208f;
}

td.five {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #ff7f29;
}

td.six {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #ff3fff;
}

td.seven {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #3fffbf;
}

td.eight {
    background-color: #d9d9d9;
    border-color: #d9d9d9;
    color: #22ee0f;
}

JavaScript

思路

这次我们打算在原型链上编程

首先,我们先写出mine 的构造函数

Mine 构造函数

function Mine(tr, td, mineNum) {
    this.tr = tr;                                       // tr表示行数  
    this.td = td;                                       // td表示列数  
    this.mineNum = mineNum;     // mineNum表示雷的数量

    this.squares = [];        // 存储所有方块的信息,是一个二维数组,按照行与列的顺序排放,存取都按照行列方式
    this.tds = [];            // 存储所有单元格的DOM
    this.surplusMine = mineNum;          // 剩余雷的数量
    this.allRight = false;       // 右击标注的小红旗是否全部是雷,用来判断用户是否游戏成功
    this.parent = document.querySelector(''.gameBox'');
}

生成随机的雷的排布

我们构造函数中传进去了三个参数,行数,列数,以及需要的雷数,我们的思路是这样的,我们游戏是在table里面进行,table一共有 tr * td 个格子,那么我们可以创建一个 长度为 tr * td 的 数组,然后,给数组赋值,每个值对应一个方格,最后再给数组乱序,取出前 mineNum(需要的雷数) 个, 这mineNum个方格所对应的就是我们的地雷的方格

那么我们写出 我们的js代码

Mine.prototype.randomNum = function () {
    var square = new Array(this.tr * this.td); // 生成一个空数组  长度为格子总数
    for (var i = 0; i < square.length; i++) {
        square[i] = i;
    }
    // 数组乱序
    square.sort(function () {
        return 0.5 - Math.random()
    });
    return square.slice(0, this.mineNum);
};

创建表格

我们虽然上述把雷的位置取出了,但是,我们再创建游戏区域的时候,并没必要,在创建的时候就将逻辑写入,我们可以在点击小方格的时候,来判断其位置是否是雷还是空即可

因此,我们先写出创建表格的 js 代码

Mine.prototype.createDom = function () {
    var This = this;
    var table = document.createElement(''table'');
    for (var i = 0; i < this.tr; i++) {                 // 行
        var domTr = document.createElement(''tr'');
        this.tds[i] = [];
        for (var j = 0; j < this.td; j++) {             // 列
            var domTd = document.createElement(''td'');
            this.tds[i][j] = domTd;                     // 把所有创建的td添加到数组当中
            domTd.pos = [i, j];                         // 把格子对应的行和列村到格子身上,为了下面通过这个值去数组里面取到对应的数据
            domTd.onmousedown = function () {
                This.play(event, this);                 // 大的This 指的是实例对象   小的this指的是点击的domTd 
            };

            // if (this.squares[i][j].type == ''mine'') {
            //     domTd.className = ''mine'';
            // }
            // if (this.squares[i][j].type == ''number'') {
            //     domTd.innerHTML = this.squares[i][j].value;
            // }

            domTr.appendChild(domTd);
        }
        table.appendChild(domTr);
    }
    this.parent.innerHTML = '''';       // 避免多次点击创建多个

    this.parent.appendChild(table);
};

其中的 This.play 是我们点击方块之后的判断,判断是 雷 还是 空

再写play函数之前,我们还有一些其他的逻辑需要编写

初始化函数

Mine.prototype.init = function () {
    // this.randomNum();
    var rn = this.randomNum();                  // 雷在格子里的位置
    var n = -1;                                 // 用来找到对应的索引格子
    for (var i = 0; i < this.tr; i++) {
        this.squares[i] = [];
        for (var j = 0; j < this.td; j++) {
            // 取一个方块在数组里的数据,要使用行与列的形式存取
            // 找方块周围的方块的时候,要用坐标的形式去取
            // 行与列的形式,和坐标的形式,x,y是刚好相反的
            n++;
            if (rn.indexOf(n) != -1) {
                // 如果这个条件成立,说明现在循环到的索引在雷的数组里面找到了,那就表明这个索引对应的是个雷
                this.squares[i][j] = {
                    type: ''mine'',
                    x: j,
                    y: i
                };

            } else {
                this.squares[i][j] = {
                    type: ''number'',
                    x: j,
                    y: i,
                    value: 0
                };
            }
        }
    }

    this.updateNum();
    this.createDom();

    this.parent.oncontextmenu = function () {
        return false;
        // 阻止右键出菜单事件
    }

    // 剩余雷数
    this.mineNumDom = document.querySelector(''.mineNum'');
    this.mineNumDom.innerHTML = this.surplusMine;
};

getAround() 函数

// 找某个方格周围的八个格子
Mine.prototype.getAround = function (square) {
    var x = square.x,
        y = square.y;
    var result = []; // 把找到的格子的坐标返回出去(二维数组)
    for (var i = x - 1; i <= x + 1; i++) {
        for (var j = y - 1; j <= y + 1; j++) {
            if (i < 0 ||
                j < 0 ||
                i > this.td - 1 ||
                j > this.tr - 1 ||
                // 上述表示出边界
                (i == x && j == y) ||
                // 表示循环到自己
                this.squares[j][i].type == ''mine''
                // 表示循环到(周围的格子)雷 (注意i和j表示的是坐标,而squares存储的是行和列)
            ) {
                continue;
            }
            // 要以行与列的形式返回出去,因为到时候需要它去取数组里的数据
            result.push([j, i]);
        }
    }
    return result;
}

updateNum() 函数

// 更新所有的数字
Mine.prototype.updateNum = function () {
    for (var i = 0; i < this.tr; i++) {
        for (var j = 0; j < this.td; j++) {
            // 要更新的是雷周围的数字
            if (this.squares[i][j].type == ''number'') {
                continue;
            }
            var num = this.getAround(this.squares[i][j]); // 获取到每一个雷周围的数字
            for (var k = 0; k < num.length; k++) {
                this.squares[num[k][0]][num[k][1]].value += 1;
            }
        }
    }
};

play函数

Mine.prototype.play = function (ev, obj) {
    var This = this;
    if (ev.which == 1 && obj.className != ''flag'') { // 后面的条件是为了用户右键之后不能点击
        // 点击的是左键
        var curSquare = this.squares[obj.pos[0]][obj.pos[1]];
        var cl = [''zero'', ''one'', ''two'', ''three'', ''four'', ''five'', ''six'', ''seven'', ''eight''];
        // cl 存储className

        if (curSquare.type == ''number'') {
            // 用户点击的是数字
            obj.innerHTML = curSquare.value;
            obj.className = cl[curSquare.value];

            // 点到了数字零
            if (curSquare.value == 0) {
                /* 
                    递归思路: 
                    1.显示自己
                    2.查找四周
                        1) 显示四周(如果四周的值不为零,那就显示到这,不需要再找了)
                        2)如果值为零了
                            a.显示自己
                            b.找四周(如果四周的值不为零,那就显示到这,不需要再找了)
                                I.显示自己
                                II.找四周(如果四周的值不为零,那就显示到这,不需要再找了)
                                    。。。。。。
                 */

                obj.innerHTML = '''';                             // 显示为空
                function getAllZero(square) {
                    var around = This.getAround(square);        // 找到了周围N个格子
                    for (var i = 0; i < around.length; i++) {
                        var x = around[i][0];                   // 行
                        var y = around[i][1];                   // 列
                        This.tds[x][y].className = cl[This.squares[x][y].value];

                        if (This.squares[x][y].value == 0) {
                            // 如果以某个格子为中心,找到的某个格子为零,那就接着调用(递归)
                            if (!This.tds[x][y].check) {
                                // 给对应的td 添加一条属性,如果找过的话,这个值就为true,下一次就不会再找了,防止函数调用栈出问题
                                This.tds[x][y].check = true;
                                getAllZero(This.squares[x][y]);
                            }

                        } else {
                            // 如果以某个格子为中心找到的四周的值不为零,就把数字显示出来
                            This.tds[x][y].innerHTML = This.squares[x][y].value;
                        }
                    }

                }
                getAllZero(curSquare);
            }

        } else {
            // 用户点击的是雷
            this.gameOver(obj);
        }
    }
    if (ev.which == 3) {
        // 用户点击的是右键
        // 如果右击的是一个数字,就不能点击
        if (obj.className && obj.className != ''flag'') {
            return;
        }
        obj.className = obj.className == ''flag'' ? '''' : ''flag''; // 切换calss 有无

        if (this.squares[obj.pos[0]][obj.pos[1]].type == ''mine'') {
            this.allRight = true;
        } else {
            this.allRight = false;
        }

        if (obj.className == ''flag'') {
            this.mineNumDom.innerHTML = --this.surplusMine;
        } else {
            this.mineNumDom.innerHTML = ++this.surplusMine;
        }

        if (this.surplusMine == 0) {
            // 剩余的雷的数量为0,表示用户已经标完小红旗了,这时候要判断游戏是成功还是结束
            if (this.allRight == true) {
                // 这个条件成立,说明用户全部标对了
                alert(''恭喜你,游戏通过'');
                for (i = 0; i < this.tr; i++) {
                    for (j = 0; j < this.td; j++) {
                        this.tds[i][j].onmousedown = null;
                    }
                }

            } else {
                alert(''游戏失败'');
                this.gameOver();
            }
        }
    }
}

gameOver函数

Mine.prototype.gameOver = function (clickTd) {
    /* 
        1.显示所有的雷
        2.取消所有格子的点击事件
        3.给点中的格子标红
    */
    for (i = 0; i < this.tr; i++) {
        for (j = 0; j < this.td; j++) {
            if (this.squares[i][j].type == ''mine'') {
                this.tds[i][j].className = ''mine'';
            }
            this.tds[i][j].onmousedown = null;
        }
    }
    if (clickTd) {
        clickTd.style.backgroundColor = ''#f00'';
    }
}

最后,补充些其他的功能

其他

// 添加 button 的功能
var btns = document.getElementsByTagName(''button'');
var mine = null; // 用来存储生成的实例
var ln = 0; // 用来处理当前选中的状态
var arr = [
    [9, 9, 10],
    [16, 16, 40],
    [28, 28, 99]
]; //不同级别的行数,列数,雷数
for (let i = 0; i < btns.length - 1; i++) {
    btns[i].onclick = function () {
        btns[ln].className = '''';
        this.className = ''active'';
        mine = new Mine(arr[i][0], arr[i][1], arr[i][2]);
        mine.init();
        ln = i;
    }
}
btns[0].onclick(); // 初始化
btns[3].onclick = function () {
    for (var i = 0; i < btns.length - 1; i++) {
        if (btns[i].className == ''active'') {
            btns[i].onclick();
        }
    }
}

js 整合代码

function Mine(tr, td, mineNum) {
    this.tr = tr;                                       // tr表示行数  
    this.td = td;                                       // td表示列数  
    this.mineNum = mineNum;                             // mineNum表示雷的数量

    this.squares = [];                                  // 存储所有方块的信息,是一个二维数组,按照行与列的顺序排放,存取都按照行列方式
    this.tds = [];                                      // 存储所有单元格的DOM
    this.surplusMine = mineNum;                         // 剩余雷的数量
    this.allRight = false;                              // 右击标注的小红旗是否全部是雷,用来判断用户是否游戏成功
    this.parent = document.querySelector(''.gameBox'');
}

// 生成n个不重复的数字
Mine.prototype.randomNum = function () {
    var square = new Array(this.tr * this.td); // 生成一个空数组  长度为格子总数
    for (var i = 0; i < square.length; i++) {
        square[i] = i;
    }
    // 数组乱序
    square.sort(function () {
        return 0.5 - Math.random()
    });
    return square.slice(0, this.mineNum);
};

// 创建表格
Mine.prototype.createDom = function () {
    var This = this;
    var table = document.createElement(''table'');
    for (var i = 0; i < this.tr; i++) {                 // 行
        var domTr = document.createElement(''tr'');
        this.tds[i] = [];
        for (var j = 0; j < this.td; j++) {             // 列
            var domTd = document.createElement(''td'');
            this.tds[i][j] = domTd;                     // 把所有创建的td添加到数组当中
            domTd.pos = [i, j];                         // 把格子对应的行和列村到格子身上,为了下面通过这个值去数组里面取到对应的数据
            domTd.onmousedown = function () {
                This.play(event, this);                 // 大的This 指的是实例对象   小的this指的是点击的domTd 
            };

            // if (this.squares[i][j].type == ''mine'') {
            //     domTd.className = ''mine'';
            // }
            // if (this.squares[i][j].type == ''number'') {
            //     domTd.innerHTML = this.squares[i][j].value;
            // }

            domTr.appendChild(domTd);
        }
        table.appendChild(domTr);
    }
    this.parent.innerHTML = '''';                         // 避免多次点击创建多个

    this.parent.appendChild(table);
};

Mine.prototype.init = function () {
    // this.randomNum();
    var rn = this.randomNum();                  // 雷在格子里的位置
    var n = -1;                                 // 用来找到对应的索引格子
    for (var i = 0; i < this.tr; i++) {
        this.squares[i] = [];
        for (var j = 0; j < this.td; j++) {
            // 取一个方块在数组里的数据,要使用行与列的形式存取
            // 找方块周围的方块的时候,要用坐标的形式去取
            // 行与列的形式,和坐标的形式,x,y是刚好相反的
            n++;
            if (rn.indexOf(n) != -1) {
                // 如果这个条件成立,说明现在循环到的索引在雷的数组里面找到了,那就表明这个索引对应的是个雷
                this.squares[i][j] = {
                    type: ''mine'',
                    x: j,
                    y: i
                };

            } else {
                this.squares[i][j] = {
                    type: ''number'',
                    x: j,
                    y: i,
                    value: 0
                };
            }
        }
    }

    this.updateNum();
    this.createDom();

    this.parent.oncontextmenu = function () {
        return false;
        // 阻止右键出菜单事件
    }

    // 剩余雷数
    this.mineNumDom = document.querySelector(''.mineNum'');
    this.mineNumDom.innerHTML = this.surplusMine;
};


// 找某个方格周围的八个格子
Mine.prototype.getAround = function (square) {
    var x = square.x,
        y = square.y;
    var result = []; // 把找到的格子的坐标返回出去(二维数组)
    for (var i = x - 1; i <= x + 1; i++) {
        for (var j = y - 1; j <= y + 1; j++) {
            if (i < 0 ||
                j < 0 ||
                i > this.td - 1 ||
                j > this.tr - 1 ||
                // 上述表示出边界
                (i == x && j == y) ||
                // 表示循环到自己
                this.squares[j][i].type == ''mine''
                // 表示循环到(周围的格子)雷 (注意i和j表示的是坐标,而squares存储的是行和列)
            ) {
                continue;
            }
            // 要以行与列的形式返回出去,因为到时候需要它去取数组里的数据
            result.push([j, i]);
        }
    }
    return result;
}

// 更新所有的数字
Mine.prototype.updateNum = function () {
    for (var i = 0; i < this.tr; i++) {
        for (var j = 0; j < this.td; j++) {
            // 要更新的是雷周围的数字
            if (this.squares[i][j].type == ''number'') {
                continue;
            }
            var num = this.getAround(this.squares[i][j]); // 获取到每一个雷周围的数字
            for (var k = 0; k < num.length; k++) {
                this.squares[num[k][0]][num[k][1]].value += 1;
            }
        }
    }
};

Mine.prototype.play = function (ev, obj) {
    var This = this;
    if (ev.which == 1 && obj.className != ''flag'') { // 后面的条件是为了用户右键之后不能点击
        // 点击的是左键
        var curSquare = this.squares[obj.pos[0]][obj.pos[1]];
        var cl = [''zero'', ''one'', ''two'', ''three'', ''four'', ''five'', ''six'', ''seven'', ''eight''];
        // cl 存储className

        if (curSquare.type == ''number'') {
            // 用户点击的是数字
            obj.innerHTML = curSquare.value;
            obj.className = cl[curSquare.value];

            // 点到了数字零
            if (curSquare.value == 0) {
                /* 
                    递归思路: 
                    1.显示自己
                    2.查找四周
                        1) 显示四周(如果四周的值不为零,那就显示到这,不需要再找了)
                        2)如果值为零了
                            a.显示自己
                            b.找四周(如果四周的值不为零,那就显示到这,不需要再找了)
                                I.显示自己
                                II.找四周(如果四周的值不为零,那就显示到这,不需要再找了)
                                    。。。。。。
                 */

                obj.innerHTML = '''';                             // 显示为空
                function getAllZero(square) {
                    var around = This.getAround(square);        // 找到了周围N个格子
                    for (var i = 0; i < around.length; i++) {
                        var x = around[i][0];                   // 行
                        var y = around[i][1];                   // 列
                        This.tds[x][y].className = cl[This.squares[x][y].value];

                        if (This.squares[x][y].value == 0) {
                            // 如果以某个格子为中心,找到的某个格子为零,那就接着调用(递归)
                            if (!This.tds[x][y].check) {
                                // 给对应的td 添加一条属性,如果找过的话,这个值就为true,下一次就不会再找了,防止函数调用栈出问题
                                This.tds[x][y].check = true;
                                getAllZero(This.squares[x][y]);
                            }

                        } else {
                            // 如果以某个格子为中心找到的四周的值不为零,就把数字显示出来
                            This.tds[x][y].innerHTML = This.squares[x][y].value;
                        }
                    }

                }
                getAllZero(curSquare);
            }

        } else {
            // 用户点击的是雷
            this.gameOver(obj);
        }
    }
    if (ev.which == 3) {
        // 用户点击的是右键
        // 如果右击的是一个数字,就不能点击
        if (obj.className && obj.className != ''flag'') {
            return;
        }
        obj.className = obj.className == ''flag'' ? '''' : ''flag''; // 切换calss 有无

        if (this.squares[obj.pos[0]][obj.pos[1]].type == ''mine'') {
            this.allRight = true;
        } else {
            this.allRight = false;
        }

        if (obj.className == ''flag'') {
            this.mineNumDom.innerHTML = --this.surplusMine;
        } else {
            this.mineNumDom.innerHTML = ++this.surplusMine;
        }

        if (this.surplusMine == 0) {
            // 剩余的雷的数量为0,表示用户已经标完小红旗了,这时候要判断游戏是成功还是结束
            if (this.allRight == true) {
                // 这个条件成立,说明用户全部标对了
                alert(''恭喜你,游戏通过'');
                for (i = 0; i < this.tr; i++) {
                    for (j = 0; j < this.td; j++) {
                        this.tds[i][j].onmousedown = null;
                    }
                }

            } else {
                alert(''游戏失败'');
                this.gameOver();
            }
        }
    }
}

// 游戏失败函数
Mine.prototype.gameOver = function (clickTd) {
    /* 
        1.显示所有的雷
        2.取消所有格子的点击事件
        3.给点中的格子标红
    */
    for (i = 0; i < this.tr; i++) {
        for (j = 0; j < this.td; j++) {
            if (this.squares[i][j].type == ''mine'') {
                this.tds[i][j].className = ''mine'';
            }
            this.tds[i][j].onmousedown = null;
        }
    }
    if (clickTd) {
        clickTd.style.backgroundColor = ''#f00'';
    }
}

// var mine = new Mine(28, 28, 99);
// mine.init();

// 添加 button 的功能
var btns = document.getElementsByTagName(''button'');
var mine = null; // 用来存储生成的实例
var ln = 0; // 用来处理当前选中的状态
var arr = [
    [9, 9, 10],
    [16, 16, 40],
    [28, 28, 99]
]; //不同级别的行数,列数,雷数
for (let i = 0; i < btns.length - 1; i++) {
    btns[i].onclick = function () {
        btns[ln].className = '''';
        this.className = ''active'';
        mine = new Mine(arr[i][0], arr[i][1], arr[i][2]);
        mine.init();
        ln = i;
    }
}
btns[0].onclick(); // 初始化
btns[3].onclick = function () {
    for (var i = 0; i < btns.length - 1; i++) {
        if (btns[i].className == ''active'') {
            btns[i].onclick();
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • 分享自己用JS做的扫雷小游戏
  • js版扫雷实现代码 原理不错
  • js实现扫雷小程序的示例代码
  • 使用纯javascript实现经典扫雷游戏
  • javascript 扫雷游戏
  • HTML+JavaScript实现扫雷小游戏
  • JavaScript制作windows经典扫雷小游戏
  • JavaScript版经典游戏之扫雷游戏完整示例【附demo源码下载】
  • js+canvas实现简单扫雷小游戏
  • js实现简单扫雷

Android实现扫雷小游戏

Android实现扫雷小游戏

本文实例为大家分享了Android实现扫雷小游戏的具体代码,供大家参考,具体内容如下

先看效果图:

初始游戏界面:

翻开块和标记块界面:

游戏结束界面:

菜单界面:

更换难度界面:

查看游戏记录界面:

代码分析

Block.java

这部分代码实现的是游戏界面的板块

设置四个变量来记录当前块是否被翻开,当前块是否是地雷,是否把当前快标记为地雷(也就是插旗子),当前块周围的地雷数量。

关键部分代码:

//设置翻开状态
public void setNumberOfSurroundingMines(int number) {
 this.setBackgroundResource(R.drawable.selected);//设置翻开背景图
 updateNumber(number);//设置周围雷数
}

//添加雷块标识
public void setMineIcon() {
 this.setBackgroundResource(R.drawable.dl);
}

//添加标记标识
public void setFlagIcon(boolean enabled) {

 if (!enabled) {
 this.setBackgroundResource(R.drawable.hq);
 } else {
 this.setTextColor(Color.BLACK);
 }
}

//清除所有标记
public void clearAllIcons() {
 this.setText("");
 this.setTextColor(R.drawable.unselected);
}

private void setBoldFont() {
 this.setTypeface(null, Typeface.BOLD);
}

//翻开方块
public void OpenBlock() {
 if (!isCovered) {
 return;
 }
 isCovered = false;
 //如果为雷设置地雷标识
 if (hasMine()) {
 setMineIcon();
 } else {
 setNumberOfSurroundingMines(numberOfMinesInSurrounding);//根据周围雷数设置翻开状态及显示数字
 }
}

LevelActivity

这部分把玩家选择的难度easy或hard传递给MenuActivity

package com.example.saolei;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class LevelActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_level);
 }

 /*根据不同难度返回相应参数*/
 public void Easy(View view) {
 Intent data = new Intent();
 data.putExtra("result", "easy");
 setResult(2, data);
 finish();
 }

 public void Hard(View view) {
 Intent data = new Intent();
 data.putExtra("result", "hard");
 setResult(2, data);
 finish();
 }

 public void Return(View view) {
 Intent data = new Intent();
 data.putExtra("result", "");
 setResult(2, data);
 finish();
 }
}

MenuActivity

这部分是菜单页面,包括难度选择和游戏记录的查看

这里接收难度选择界面传回的参数并将其传回主界面处理

package com.example.saolei;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class MenuActivity extends AppCompatActivity {
 String t = "easy";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_menu);
 }

 @Override
 //接收难度界面传回的参数并将其传回主界面处理
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == 2) {
  Intent level = new Intent();
  t = data.getStringExtra("result");
  level.putExtra("result", t);
  setResult(1, data);
  finish();
 }
 }

 //点击新游戏按钮,将结果传回主界面
 public void NewGame(View view) {
 Intent data = new Intent();
 data.putExtra("result", "newgame");
 setResult(1, data);
 finish();
 }

 //点击改变难度按钮,启动难度选择界面
 public void ChangeLevel(View view) {
 Intent level = new Intent(MenuActivity.this, LevelActivity.class);
 startActivityForResult(level, 2);
 }

 //点击游戏记录按钮,显示记录
 public void Record(View view) {
 String filename = getExternalCacheDir().getAbsolutePath() + "/gamerecord.txt";//文件路径
 File file = new File(filename);
 Scanner inputStream = null;
 FileInputStream fis = null;
 BufferedReader br = null;
 String str;
 String message = " " + "级别" + " " + "胜负" + "  " + "时间\n";
 //若文件不存在,显示暂无记录
 if (!file.exists()) {
  new AlertDialog.Builder(this)
   .setMessage("暂无记录!")
   .setNegativeButton("确定", null)
   .create().show();
 }
 else {
  try {
  //读取文件记录并生成对话框显示
  inputStream = new Scanner(new FileInputStream(filename));
  int i = 1;
  fis = new FileInputStream(filename);
  br = new BufferedReader(new InputStreamReader(fis));
  while ((str = br.readLine()) != null) {
   message = message + (i + ". " + str + "\n");
   i++;
  }
  new AlertDialog.Builder(this)
   .setMessage(message)
   .setNegativeButton("确定", null)
   .create().show();
  fis.close();
  br.close();
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  } finally {
  inputStream.close();
  }
 }
 }

 //点击返回按钮,回传结果到主界面
 public void Return1(View view) {
 Intent data = new Intent();
 data.putExtra("result", "start");
 setResult(1, data);
 finish();
 }
}

MainActivity

这部分是主体内容,包括游戏初始化,雷区的创建,游戏胜负的判定,游戏结果的存档等

这部分比较长,就不贴代码了

最后附上完整项目链接

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • Android 实现扫雷小游戏实例代码

C#实现扫雷游戏

C#实现扫雷游戏

本文实例为大家分享了C#实现扫雷游戏的具体代码,供大家参考,具体内容如下

一、实验目的:

1、掌握c#窗体和控件的常用属性和功能
2、完成扫雷游戏的基本功能

二、实验要求:

1、游戏基本功能必须实现。鼠标左键点非雷点,否则游戏结束;鼠标右键一次标记雷点,邮件两次标记上问号,右键三次取消标记。
2、可以对游戏选择难度,分为初级、中级和高级,按笑脸按钮重新开始游戏
3、符合游戏逻辑。每个点周围的雷的个数必须正确
4、点开雷点,显示游戏结束,并且显示各个点的情况
5、点开所有非雷点或者标记完所有雷点时,能够显示游戏胜利
6、不接受键盘操作,只接受鼠标操作

三、实验内容:

1、构建菜单栏,添加开始栏、帮助栏,开始栏用于游戏难度的选择,帮助栏用于游戏规则的介绍
2、创建雷区,使用buttonarray模拟雷区,start按钮用于重新开始游戏
3、鼠标左键时,分三种情况:
    (1)鼠标点击雷点时,直接显示游戏结束
    (2)鼠标点击空白点时,周围没雷,则显示周围点的情况,周围有雷,只显示此点的雷数
    (3)鼠标左键点了一个大于0的数字,显示周围雷点的情况,若周围雷点标错,直接显示游戏结束
4、鼠标右键时,第一次标记为雷点,第二次标记为疑问,第三次取消标记。
5、显示周围点的情况时,因为周围点的雷点数也可能为0,还需要显示此点周围的情况,需用递归,完成此项功能。
6、点击笑脸按钮时,如果不是第一次开始,就删除原有按钮,否则直接初始化长度、宽度和雷数,重新构建button按钮
7、点击菜单栏的难度选择按钮时,如果不是第一次开始,就删除原有按钮,否则直接初始化长度、宽度和雷数,重新构建button按钮

四、实验源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Size s = new Size(250,300);
            this.MaximumSize = this.MinimumSize = s;
            this.Size = s;
        }


        Button start = new Button();
        Button[,] buttonarray;
        int[,] MileState;
        int Miles = 10;
        int widths = 9, heights = 9;
        int remain;//剩余雷数
        int notMiles;//剩余非雷数
        int isfirst = 1;//是否是第一次
        int[,] sign;//表示各点是否输出
        private void Form1_Load(object sender, EventArgs e)
        {
            
            MenuStrip ms = new MenuStrip();
            ToolStripMenuItem tsmione = new ToolStripMenuItem("开始");
            ToolStripMenuItem tsmi1 = new ToolStripMenuItem("初级");
            ToolStripMenuItem tsmi2 = new ToolStripMenuItem("中级");
            ToolStripMenuItem tsmi3 = new ToolStripMenuItem("高级");
            ToolStripMenuItem tsmi4 = new ToolStripMenuItem("退出");
            tsmione.DropDownItems.Add(tsmi1);
            tsmione.DropDownItems.Add(tsmi2);
            tsmione.DropDownItems.Add(tsmi3);
            tsmione.DropDownItems.Add(tsmi4);
            ms.Items.Add(tsmione);
            tsmi1.Click += new EventHandler(tsmi1_Click);
            tsmi2.Click += new EventHandler(tsmi2_Click);
            tsmi3.Click += new EventHandler(tsmi3_Click);
            tsmi4.Click += new EventHandler(tsmi4_Click);
            ToolStripMenuItem tsmitwo = new ToolStripMenuItem("帮助");
            ToolStripMenuItem tsmi5 = new ToolStripMenuItem("游戏规则");
            tsmi5.Click += new EventHandler(tsmi5_Click);
            tsmitwo.DropDownItems.Add(tsmi5);
            ms.Items.Add(tsmitwo);
            this.Controls.Add(ms);
            //笑脸按钮
            start.Text = "";
            start.Location = new Point(75, 25);
            start.Click += new EventHandler(start_Click);
            this.Controls.Add(start);

        }
        private void tsmi1_Click(object sender, EventArgs e)
        {
            Size s = new Size(250, 300);
            this.MaximumSize = this.MinimumSize = s;
            this.Size = s;
            if (isfirst == 1)
            {
                widths = 9; heights = 9; Miles = 10;
                Initialize_button(sender, e);
                start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
                isfirst = 0;
                return;
            }
            delete();
            widths = 9; heights = 9; Miles = 10;
            Initialize_button(sender, e);
            start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
        }
        private void tsmi2_Click(object sender, EventArgs e)
        {
            Size s = new Size(400, 450);
            this.MaximumSize = this.MinimumSize = s;
            this.Size = s;
            if (isfirst == 1)
            {
                widths = 16; heights = 16; Miles = 40;
                Initialize_button(sender, e);
                start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
                isfirst = 0;
                return;
            }
            delete();
            widths = 16; heights = 16; Miles = 40;
            Initialize_button(sender, e);
            start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
        }
        private void tsmi3_Click(object sender, EventArgs e)
        {
            Size s = new Size(650, 450);
            this.MaximumSize = this.MinimumSize = s;
            this.Size = s;
            if (isfirst == 1)
            {
                widths = 30; heights = 16; Miles = 99;
                Initialize_button(sender, e);
                start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
                isfirst = 0;
                return;
            }
            delete();
            widths = 30; heights = 16; Miles = 99;
            Initialize_button(sender, e);
            start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
        }
        //删除控件
        public void delete()
        {
            int i, j;
            for (i = 0; i < heights; i++)
                for (j = 0; j < widths; j++)
                    this.Controls.Remove(buttonarray[i, j]);
        }
        private void tsmi4_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void tsmi5_Click(object sender, EventArgs e)
        {
            string str = "鼠标左键点开非雷点继续游戏,点开雷点游戏结束\r\n";
            str += "鼠标右键一次标记雷点,右键两次标记问号,右键三次取消标记";
            MessageBox.Show(str);
        }
        //设计扫雷界面,布雷
        private void Initialize_button(object sender, EventArgs e)
        {
            //初始化游戏界面
            //创建扫雷按钮,设计游戏界面
            buttonarray = new Button[heights, widths];
            MileState = new int[heights, widths];
            notMiles = widths * heights - Miles;
            remain = Miles;
            int i, j;
            for (i = 0; i < heights; i++)
            {
                for (j = 0; j < widths; j++)
                {
                    buttonarray[i, j] = new System.Windows.Forms.Button();
                    buttonarray[i, j].Location = new System.Drawing.Point(20 + 20 * j, 60 + 20 * i);
                    buttonarray[i, j].Size = new System.Drawing.Size(20, 20);
                    buttonarray[i, j].UseVisualStyleBackColor = true;
                    buttonarray[i, j].Text = "";
                    buttonarray[i, j].MouseDown += new MouseEventHandler(but_MouseDown);
                    this.Controls.Add(buttonarray[i, j]);
                }
            }
            int count = 0;
            //雷区初始化,鼠标右键次数初始化
            for (i = 0; i < heights; i++)
                for (j = 0; j < widths; j++)
                    MileState[i, j] = 0;
            //设置雷的位置
            while (count < Miles)
            {
                Random r = new Random();
                i = r.Next(heights);
                j = r.Next(widths);
                if (MileState[i, j] != -1)
                {
                    MileState[i, j] = -1;
                    count++;
                    //雷点周围非雷的点各加1
                    if (i - 1 >= 0 && j - 1 >= 0 && MileState[i - 1, j - 1] != -1) MileState[i - 1, j - 1] += 1;
                    if (i - 1 >= 0 && MileState[i - 1, j] != -1) MileState[i - 1, j] += 1;
                    if (i - 1 >= 0 && j + 1 < widths && MileState[i - 1, j + 1] != -1) MileState[i - 1, j + 1] += 1;
                    if (j - 1 >= 0 && MileState[i, j - 1] != -1) MileState[i, j - 1] += 1;
                    if (j + 1 < widths && MileState[i, j + 1] != -1) MileState[i, j + 1] += 1;
                    if (i + 1 < heights && j - 1 >= 0 && MileState[i + 1, j - 1] != -1) MileState[i + 1, j - 1] += 1;
                    if (i + 1 < heights && MileState[i + 1, j] != -1) MileState[i + 1, j] += 1;
                    if (i + 1 < heights && j + 1 < widths && MileState[i + 1, j + 1] != -1) MileState[i + 1, j + 1] += 1;
                }
            }
        }
        //点击笑脸按钮
        private void start_Click(object sender, EventArgs e)
        {
            if (isfirst == 1)
            {
                Initialize_button(sender, e);
                start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
                isfirst = 0;
                return;
            }
            delete();
            remain = Miles;
            notMiles = widths * heights - Miles;
            start.Text = "";
            Initialize_button(sender, e);
            start.Location = new Point((buttonarray[0, 0].Location.X
                + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25);
        }
        //按下鼠标键时
        private void but_MouseDown(object sender, MouseEventArgs e)
        {
            //获取按钮坐标
            int x = (this.PointToClient(MousePosition).Y - 60) / 20;
            int y = (this.PointToClient(MousePosition).X - 20) / 20;
            sign = new int[heights, widths];
            //递归时表示是否访问
            for (int i = 0; i < heights; i++)
                for (int j = 0; j < widths; j++)
                    sign[i, j] = 0;
            //鼠标左键点了一个大于0的数字
            if (e.Button == MouseButtons.Left && buttonarray[x, y].Text != "" && buttonarray[x, y].Text != "×"
                && buttonarray[x, y].Text != "?" && buttonarray[x, y].Text != "-1")
            {
                int num = 0;
                bool issigned = false;
                //周围标记的地雷个数
                if (x - 1 >= 0 && y - 1 >= 0)
                {
                    if (buttonarray[x - 1, y - 1].Text == "×") num++;
                    else if (buttonarray[x - 1, y - 1].Text == "-1") issigned = true;
                }
                if (x - 1 >= 0)
                {
                    if (buttonarray[x - 1, y].Text == "×") num++;
                    else if (buttonarray[x - 1, y].Text == "-1") issigned = true;
                }
                if (x - 1 >= 0 && y + 1 < widths)
                {
                    if (buttonarray[x - 1, y + 1].Text == "×") num++;
                    else if (buttonarray[x - 1, y + 1].Text == "-1") issigned = true;
                }
                if (y - 1 >= 0)
                {
                    if (buttonarray[x, y - 1].Text == "×") num++;
                    else if (buttonarray[x, y - 1].Text == "-1") issigned = true;
                }
                if (y + 1 < widths)
                {
                    if (buttonarray[x, y + 1].Text == "×") num++;
                    else if (buttonarray[x, y + 1].Text == "-1") issigned = true;
                }
                if (x + 1 < heights && y - 1 >= 0)
                {
                    if (buttonarray[x + 1, y - 1].Text == "×") num++;
                    else if (buttonarray[x + 1, y - 1].Text == "-1") issigned = true;
                }
                if (x + 1 < heights)
                {
                    if (buttonarray[x + 1, y].Text == "×") num++;
                    else if (buttonarray[x + 1, y].Text == "-1") issigned = true;
                }
                if (x + 1 < heights && y + 1 < widths)
                {
                    if (buttonarray[x + 1, y + 1].Text == "×") num++;
                    else if (buttonarray[x + 1, y + 1].Text == "-1") issigned = true;
                }
                if (buttonarray[x, y].Text == Convert.ToString(num))
                {
                    if (issigned == false) { print(x, y, sign); notMiles++; }
                    else MessageBox.Show("哎呀,点错了,重新开始吧");
                }
            }
            //鼠标左键,周围没雷
            if (e.Button == MouseButtons.Left && MileState[x, y] == 0)
            {
                if (sign[x, y] == 0) print(x, y, sign);
                if (notMiles == 0)
                    MessageBox.Show("恭喜你,扫雷成功,回去领赏吧", "成功");
            }
            //鼠标左键,周围有雷
            if (e.Button == MouseButtons.Left && MileState[x, y] > 0)
            {
                if (sign[x, y] == 0 && --notMiles == 0)
                    MessageBox.Show("恭喜你,扫雷成功,回去领赏吧", "成功");
                sign[x, y] = 1;
                buttonarray[x, y].FlatStyle = FlatStyle.Popup;
                buttonarray[x, y].Text = Convert.ToString(MileState[x, y]);
            }
            //鼠标左键,错误
            if (e.Button == MouseButtons.Left && MileState[x, y] == -1)
            {
                for (x = 0; x < heights; x++)
                    for (y = 0; y < widths; y++)
                    {
                        if (MileState[x, y] != 0)
                            buttonarray[x, y].Text = Convert.ToString(MileState[x, y]);
                        else
                            buttonarray[x, y].FlatStyle = FlatStyle.Popup;
                    }
                start.Text = "";
                MessageBox.Show("哎呀,点错了,重新开始吧!");
            }
            //鼠标右键
            if (e.Button == MouseButtons.Right)
            {
                //第一次鼠标右键
                if (buttonarray[x, y].Text == "" && sign[x, y] == 0)
                {
                    //用X表示雷
                    buttonarray[x, y].Text = "×";
                    sign[x, y] = 1;
                    if (MileState[x, y] == -1)
                    {
                        if (--remain == 0) MessageBox.Show("恭喜你,扫雷成功,回去领赏吧", "成功");
                    }
                }
                //第二次鼠标右键
                else if (buttonarray[x, y].Text == "×")
                {
                    buttonarray[x, y].Text = "?";
                    remain++;
                }
                //第三次鼠标右键
                else if (buttonarray[x, y].Text == "?")
                {
                    buttonarray[x, y].Text = "";
                    sign[x, y] = 0;
                }
            }
        }

        //显示周围雷区的情况,递归
        private void print(int x, int y, int[,] sign)
        {
            buttonarray[x, y].FlatStyle = FlatStyle.Popup;
            sign[x, y] = 1;
            notMiles--;
            if (x - 1 >= 0 && y - 1 >= 0 && sign[x - 1, y - 1] == 0)
                if (MileState[x - 1, y - 1] > 0)
                {
                    buttonarray[x - 1, y - 1].Text = Convert.ToString(MileState[x - 1, y - 1]);
                    buttonarray[x - 1, y - 1].FlatStyle = FlatStyle.Popup;
                    sign[x - 1, y - 1] = 1;
                }
                else if (MileState[x - 1, y - 1] == 0) print(x - 1, y - 1, sign);
            if (x - 1 >= 0 && sign[x - 1, y] == 0)
                if (MileState[x - 1, y] > 0)
                {
                    buttonarray[x - 1, y].Text = Convert.ToString(MileState[x - 1, y]);
                    buttonarray[x - 1, y].FlatStyle = FlatStyle.Popup;
                    sign[x - 1, y] = 1;
                }
                else if (MileState[x - 1, y] == 0) print(x - 1, y, sign);
            if (x - 1 >= 0 && y + 1 < widths && sign[x - 1, y + 1] == 0)
                if (MileState[x - 1, y + 1] > 0)
                {
                    buttonarray[x - 1, y + 1].Text = Convert.ToString(MileState[x - 1, y + 1]);
                    buttonarray[x - 1, y + 1].FlatStyle = FlatStyle.Popup;
                    sign[x - 1, y + 1] = 1;
                }
                else if (MileState[x - 1, y + 1] == 0) print(x - 1, y + 1, sign);
            if (y - 1 >= 0 && sign[x, y - 1] == 0)
                if (MileState[x, y - 1] > 0)
                {
                    buttonarray[x, y - 1].Text = Convert.ToString(MileState[x, y - 1]);
                    buttonarray[x, y - 1].FlatStyle = FlatStyle.Popup;
                    sign[x, y - 1] = 1;
                }
                else if (MileState[x, y - 1] == 0) print(x, y - 1, sign);
            if (y + 1 < widths && sign[x, y + 1] == 0)
                if (MileState[x, y + 1] > 0)
                {
                    buttonarray[x, y + 1].Text = Convert.ToString(MileState[x, y + 1]);
                    buttonarray[x, y + 1].FlatStyle = FlatStyle.Popup;
                    sign[x, y + 1] = 1;
                }
                else if (MileState[x, y + 1] == 0) print(x, y + 1, sign);
            if (x + 1 < heights && y - 1 >= 0 && sign[x + 1, y - 1] == 0)
                if (MileState[x + 1, y - 1] > 0)
                {
                    buttonarray[x + 1, y - 1].Text = Convert.ToString(MileState[x + 1, y - 1]);
                    buttonarray[x + 1, y - 1].FlatStyle = FlatStyle.Popup;
                    sign[x + 1, y - 1] = 1;
                }
                else if (MileState[x + 1, y - 1] == 0) print(x + 1, y - 1, sign);
            if (x + 1 < heights && sign[x + 1, y] == 0)
                if (MileState[x + 1, y] > 0)
                {
                    buttonarray[x + 1, y].Text = Convert.ToString(MileState[x + 1, y]);
                    buttonarray[x + 1, y].FlatStyle = FlatStyle.Popup;
                    sign[x + 1, y] = 1;
                }
                else if (MileState[x + 1, y] == 0) print(x + 1, y, sign);
            if (x + 1 < heights && y + 1 < widths && sign[x + 1, y + 1] == 0)
                if (MileState[x + 1, y + 1] > 0)
                {
                    buttonarray[x + 1, y + 1].Text = Convert.ToString(MileState[x + 1, y + 1]);
                    buttonarray[x + 1, y + 1].FlatStyle = FlatStyle.Popup;
                    sign[x + 1, y + 1] = 1;
                }
                else if (MileState[x + 1, y + 1] == 0) print(x + 1, y + 1, sign);
        }

    }
}

五、实验结果:

1、用”×”标记雷点,用”?”标记疑问点,空白点表示周围无雷点或者该点还未点开,根据颜色区别二者。

2、游戏胜利和游戏结束显示messagebox。

3、第一次开始点击笑脸按钮,重新开始点击哭脸按钮。

4、点击开始菜单栏的子菜单栏选择游戏难度,初级、中级、高级的雷点分别为10、40、99个
(初级)
(中级)
(高级)
点击雷点,显示游戏错误,游戏结束

六、总结

通过本次实验,对c#控件和窗体有了更深入的了解,懂得如何根据各个控件的特点实现扫雷对应的功能,希望在以后能更加熟练地使用各个控件。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • C#仿Windows XP自带的扫雷游戏
  • c# 实现自动扫雷
  • 利用C#编写扫雷游戏(附源码)
  • C#带你玩扫雷(附源码)
  • 详解从零开始---用C#制作扫雷游戏

C++实现扫雷小游戏(控制台)

C++实现扫雷小游戏(控制台)

本文实例为大家分享了C++实现扫雷小游戏的具体代码,供大家参考,具体内容如下

1.问题描述

用c++写一个扫雷小游戏,扫雷大家都玩过吧,先任意点一个方格,没有爆炸时,会出现一个数字,这个数字是以它为中心的9个格子内所有雷的个数。一般围在一堆数字中间的有可能是雷,你在你认为是雷的那里右击,就可以把它设定为雷,然后在数字区用鼠标左右键双击,可以打开非雷区,所有雷被标记后,就赢了。
今天我们写的程序需要能实现以下几个功能

(1).输入坐标打开一个格子,此格子若是雷则游戏结束,若不是则显示周围雷的个数。
(2).输入坐标为格子插上棋子和取消插旗子。

2.设计思路

(1)创建两个数组,一个是开发者数组,一个是玩家数组。生成两个界面,开发者界面显示雷和数字,玩家界面显示问号和数字。
(2)初始化两个雷阵,然后用随机数布雷。
(3)开始游戏,点到不是雷的地方将周围无雷的地方展开,如果点到雷游戏结束。

其他详细内容见代码

3.上代码

#include "pch.h"
#include <iostream>
#include <stdlib.h> 
#include<cstdlib>
#include<ctime>
using namespace std;

int shuzu1[12][12];
char show[12][12];


void wjm()
{
    cout << "  1     2     3     4     5     6     7     8     9    10   " << endl << endl;

    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[1][j] << "  |";

    }
    cout << "   1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[2][j] << "  |";

    }
    cout << "   2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[3][j] << "  |";
 
    }
    cout << "   3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[4][j] << "  |";

    }
    cout << "   4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[5][j] << "  |";

    }
    cout << "   5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[6][j] << "  |";

    }
    cout << "   6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[7][j] << "  |";

    }
    cout << "   7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[8][j] << "  |";

    }
    cout << "   8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[9][j] << "  |";

    }
    cout << "   9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << shuzu1[10][j] << "  |";

    }
    cout << "   10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;

}
//开发者界面
void first()//初始化
{
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            shuzu1[i][j] = 0;//开发者数组
            
        }
    }
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j <12; j++) 
        {
            show[i][j] = ''?'';//玩家数组
        }
    }
}
//初始化两个雷阵
void jm()//界面
{
    cout << "  1     2     3     4     5     6     7     8     9    10   " << endl << endl;
    
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[1][j] << "  |";

    }
    cout << "   1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[2][j] << "  |";

    }
    cout << "   2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[3][j] << "  |";

    }
    cout << "   3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[4][j] << "  |";

    }
    cout << "   4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[5][j] << "  |";

    }
    cout << "   5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[6][j] << "  |";

    }
    cout << "   6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[7][j] << "  |";

    }
    cout << "   7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[8][j] << "  |";

    }
    cout << "   8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[9][j] << "  |";

    }
    cout << "   9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;
    for (int j = 1; j < 11; j++)
    {
        cout << "  " << show[10][j] << "  |";

    }
    cout << "   10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl;

    cout << ''\n'' << "选项" << ''\n'' << "提示:输入横坐标后回车再输入纵坐标\n" << "1-点击(x,y)" << ''\n'' << "2-在(x,y)插旗子" << ''\n'' << "3-取消插旗子(x,y)" << ''\n'' << "4-老子不玩了" << endl;
}
//玩家界面
void bulei()
{
    srand(time(NULL));
    for (int a=0; a <10; a++)//生成10个雷
    {
        int m = rand() % 10 + 1;
        int n = rand() % 10 + 1;
        if (shuzu1[m][n] != 9)
        {
            shuzu1[m][n] = 9;
        }
    }
    
    


}
//布雷
void number()
{
    int count = 0;
    for (int x = 1; x < 11; x++)
    {
        for (int y = 1; y < 11; y++)
        {
            if (shuzu1[x][y] == 9)
            {
                if(shuzu1[x - 1][y - 1]!=9)
                shuzu1[x - 1][y-1]++;
                if(shuzu1[x - 1][y]!=9)
                shuzu1[x - 1][y]++;
                if(shuzu1[x - 1][y + 1]!=9)
                shuzu1[x - 1][y + 1]++;
                if(shuzu1[x][y - 1]!=9)
                shuzu1[x][y - 1]++;
                if (shuzu1[x][y + 1] != 9)
                shuzu1[x][y + 1]++;
                if (shuzu1[x + 1][y - 1] != 9)
                shuzu1[x + 1][y - 1]++;
                if (shuzu1[x + 1][y] != 9)
                shuzu1[x + 1][y]++;
                if (shuzu1[x + 1][y + 1] != 9)
                shuzu1[x + 1][y + 1]++;
            }
        }
    }
        
}
//生成数字
void unfold(int x,int y)
{
    if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
    {
        if (shuzu1[x][y] == 0)
        {
            show[x][y] = '' '';
            if (show[x][y + 1] == ''?'')
                unfold(x, y + 1);
            if (show[x][y - 1] == ''?'')
                unfold(x, y - 1);
            if (show[x + 1][y] == ''?'')
                unfold(x + 1, y);
            if (show[x - 1][y] == ''?'')
                unfold(x - 1, y);
            
        }
        if (shuzu1[x][y] != 0 && shuzu1[x][y] != 9)
        {
            show[x][y] = shuzu1[x][y] + ''0'';
        }
    }
        
}    
//无雷展开
void flag(int x, int y)
{
    show[x][y] = ''F'';
    jm();
}
//插旗子
void unflag(int x, int y)
{
    if (show[x][y] == ''F'')
    {
        show[x][y] = ''?'';
        jm();
    }
    else 
    {
        cout << "错误";
    }
}
//取消插旗子
void start(int x,int y)
{
    if (shuzu1[x][y] == 9)
    {
        cout << "你输了";
        exit(0);
    }
    if (shuzu1[x][y] != 9 && shuzu1[x][y] != 0)
    {
        show[x][y] = shuzu1[x][y]+''0'';
        jm();
    }
    if (shuzu1[x][y] == 0)
    {
        unfold(x, y);
        jm();
    }

}
//展开格子
void end()
{
    int count = 0;
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= 10; j++)
        {
            if (show[i][j] == ''?''||show[i][j]==''F'')
            {
                count++;
            }
        }

    }
    if (count == 10)
    {
        cout << "你赢了";
        exit(0);
    }
    
    
}
//结束游戏

int main()
{
    int x = 5;
    int y = 8;
    int z;
    first();
    bulei();
    number();
    jm();
    for (;;)
    {
        cin >> z;
        switch (z)
        {
            case 1:
            {
            cin >> x >> y;
                if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
                {
                    start(x, y);
                }
                else
                {
                    cout << "错误"; break;
                    
                }
        
            }break;
            case 2:
            {
                cin >> x >> y;
                if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
                {
                    flag(x, y);
                }
                else
                {
                    cout << "错误";
                }
            }break;
            case 3:
            {
                cin >> x >> y;
                if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
                {
                    unflag(x, y);
                }
                else
                {
                    cout << "错误";
                }
            }break;
            case 4:
            {
                exit(0);

            }break;
        }
        end();
    }

}

4.运行结果部分截图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • C/C++ 中实现让控制台暂停的方法
  • C++控制台实现简单人机对弈井字棋
  • C++实现控制台版扫雷程序
  • C++控制台版扫雷游戏
  • C++控制台实现扫雷游戏
  • C/C++ 控制台等待指令解析

c++实现扫雷小游戏代码分享

c++实现扫雷小游戏代码分享

分成两个源文件和一个头文件

注意:这串代码并不完整,不能够实现当所查坐标周围雷的数量为0时,直接展开周围坐标;

头文件:game.h

#include <stdio.h>


#define count 10 //雷的数量

//定义 行-ROW,列-COL
#define ROW 9
#define COL 9
#define ROWS ROW+2 //多加一些,方便代码
#define COLS COL+2

//初始化棋盘,声明的函数均在game.c中实现
void InitBoard(char board[ROWS][COLS],int rows,int cols);

//打印棋盘
void DisplayBoard(char board[ROW][COL],int row.int col);

//布置雷
void SetMine(char board[ROW][COLS],int row,int col);

//找雷
void FindMine(char mine[][COLS],char show[][COLS],int row,int col);

第一个源文件:saolei.c

#include "game.h"  //需要包含和声明的东西在game.h中完成

void menu()
{ 
  printf("                  \n");
  printf("       1.play     \n");
  printf("       0.exit     \n");
  printf("                  \n");
}

void game()
{ 
  // ROW and COL 在game.h中定义
  char mine[ROWS][COLS];  // 地雷埋藏的棋盘
  char show[ROWS][COLS];  // 展示出的棋盘
  //初始化棋盘
  //game()中的函数在game.h中声明,在game.c中实现
  InitBoard(mine,ROWS,COLS,''0'');   //0代表无雷,1代表有雷
  InitBOard(show,ROWS,COLS,''*'');
  //打印棋盘
  DisplayBoard(show,ROW,COL);
  //布置雷
  SetMine(mine,ROW,COL);
  //找雷
  FindMine(mine,show,ROW,COL);
  
}


int main()
{
  srand((unsigned int) time(NULL));//生成随机数
  int input = 0;
  do              //do-while循环
  {
    menu();    //(1--play   0--exit)也是do-while循环的条件
    printf("请选择:");
    scanf("%d",&input);
    switch(input)
    { 
    case 1:
        printf("开始游戏\n");
        game();
        break;
     case 0:
        printf("退出游戏");
        break;
     default:   
        printf("选择错误,请重新输入");
        break;
    }
  }while(input)
  
  return 0;
}

第二个源文件:game.c

#include "game.h"

void InitBoard(char board[ROWS][COLS],int rows,int cols,char set)
{
  int i = 0;
  int j = 0;
  for(i = 0;i < rows;i++)
  {
    for(j = 0;j < cols;j++)
    {
      board[i][j] = set;
    }
  }
  
}

void DisplayBoard(char board[ROWS][COLS],int row,int col)
{
  int i = 0;
  int j = 0;
  for(i = 0;i <= row;i++)
  {
    printf("%d",i);
  }
  printf("\n");
  for(i = 1;i <= row;i++)
  {
    printf("%d"i)
    for(j = 1;j <= col;j++)
    {
      printf(" %c ",board[i][j]);
    }
    printf("\n");
  }
  
}

void SetMine(char board[ROW][COL],int row,int col)
{
  int x = 0;
  int y = 0;
  while(count) //count-雷的数量
  {
    x = rand()%row + 1;//因为在1-row中布置雷,边缘一排不用,最后为了记录
    y = rand()%col + 1;//周围雷的数量方便,同时代码方便
    if( board[x][y] == ''0'')
    {
      board[x][y] = ''1'';
      count--;
    }
  }
}

//算出周围雷的个数
int get_mine_count(mine[ROWS][COLS],int row,int col)
{
  int i = 0;
  int j = 0;
  int mine_count = 0;
  for( i = -1;i <= 1; i++ )
  {
    for( j = -1;j <= 1; j++ )
    {
      if( mine[i][j] == ''1'')
      {
        mine_count++;
      }
    }
  }
  return mine_count;
}

void FindMine(char mine[][COLS],char show[][COLS],int row,int col)
{
  int x = 0;
  int y = 0;
  int ret = 0;//已经查找过的位置的数量
  while(ret < row * col - count)//当还剩下count的数量时,赢得游戏
  { 
    printf("请输入查找位置下标:");
    scanf("%d%d",&x,&y);
    if(x >= 1 && x <= row && y >= 1 && y <= col)
    {
      if(mine[x][y] == ''0'')
      {
        //当所查坐标的位置不是雷时,将该坐标展示成周围雷的个数
        int c = get_mine_count(mine,ROW,COL);
        mine[x][y] = c + ''0'';//返回值为int型,数组为char型,因此+''0''
        ret++;
        DisplayBoard(show,ROW,COL);
      }
      else
      {
        printf("YOU LOSE\n");
        break;
      }
    }
    else
    {
      printf("输入非法,请重新输入\n");
    }
  }
  printf("VICTORY\n");  //获得胜利
}

到此这篇关于c++实现扫雷小游戏代码分享的文章就介绍到这了,更多相关c++实现扫雷小游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • 380行C++代码实现扫雷小游戏
  • C++实现简单扫雷小游戏
  • C++实现扫雷、排雷小游戏
  • C++实现扫雷小游戏(控制台版)
  • C++实现一个扫雷小游戏
  • C++实现扫雷经典小游戏
  • C++实现扫雷小游戏

今天关于JS实现扫雷项目总结js实现扫雷项目总结报告的讲解已经结束,谢谢您的阅读,如果想了解更多关于Android实现扫雷小游戏、C#实现扫雷游戏、C++实现扫雷小游戏(控制台)、c++实现扫雷小游戏代码分享的相关知识,请在本站搜索。

本文标签: