专业IT网络知识平台,分享IT百科知识、生活百科知识解答!

易企推科技
易企推科技

使用js开发网页版 扫雷(附代码详解)

来源:小易整编  作者:小易  发布时间:2024-03-20 08:26
摘要://HTMEL//css.bottom,overtext-align:centerline-height:0px.bot...

//HTMEL    

使用js开发网页版 扫雷(附代码详解)

//css.bottom,over { text-align: center; line-height: 0px;} .bottom p { display: inline-block; width: 30px; height: 30px; border: 1px solid #808080; background-color: #f4a; text-align: center; font: 20px/30px arial; }//JS// 保存 初始雷化记录 和 拆雷记录 100个按钮中 有25个雷var box = Array(10);//初始化底层function initBottom() { const content = document.getElementsByClassName('content')[0]; const bottom = document.createElement('p'); content.appendChild(bottom); bottom.className = "bottom"; for (let i = 0; i < 10; i++) { box[i] = new Array(10); for (let j = 0; j < 10; j++) { const p = document.createElement('p'); bottom.appendChild(p); p.id = "bottom[" + i + "][" + j + "]"; box[i][j] = 0; } bottom[xss_clean] = bottom[xss_clean] + "
"; } initBom();}//初始化 25个 bom 颜色 和 innerHTMLfunction initBom() { //改变颜色 for (let i = 0; i < 25; i++) { let x = Math.floor(Math.random() * 10); let y = Math.floor(Math.random() * 10); while (box[x][y] == 1) { x = Math.floor(Math.random() * 10); y = Math.floor(Math.random() * 10); if (box[x][y] == 0) { break; } } box[x][y] = 1; document.getElementById("bottom[" + x + "][" + y + "]").style.backgroundColor = "rgb(0, 0, 0)"; document.getElementById("bottom[" + x + "][" + y + "]")[xss_clean] = "x"; // 否则 对不起 ? } // 生成底层数字 for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (!box[i][j] == 1) { document.getElementById("bottom[" + i + "][" + j + "]")[xss_clean] = getNumber(i, j); } } }}// 取得 周围雷的个数function getNumber(i, j) { let num = 0, x, y; // 四周 雷的 个数统计 for (let x = i - 1; x <= i + 1; x++) { for (y = j - 1; y <= j + 1; y++) { if (x == i && y == j) { continue; } if (document.getElementById("bottom[" + x + "][" + y + "]")) { if (box[x][y] == 1) { num++; } } } } return num;}//初始化 上层 overfunction initOver() { const content = document.getElementsByClassName('content')[0]; const over = document.createElement('p'); content.appendChild(over); over.className = "over"; over.id = "over"; for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { const p = document.createElement('p'); over.appendChild(p); p.id = "over[" + i + "][" + j + "]"; } over[xss_clean] = over[xss_clean] + "
"; } //取消 右击 菜单 over.oncontextmenu = function (e) { e.returnValue = false; } // 点击 做标记 over.onclick = function (e) { const target = e.srcElement; if (target.style.backgroundColor == "rgb(255, 255, 255)") { target.style.backgroundColor = "#808080"; } else { target.style.backgroundColor = "rgb(255, 255, 255)"; } } // 双击拆雷 over.ondblclick = function (e) { const target = e.srcElement; const strId = target.id.substring(4, target.id.length); if (document.getElementById("bottom" + strId).style.backgroundColor == "rgb(0, 0, 0)") { document.getElementById('over').style.display = "none"; } else { target.style.opacity = 0; let i = parseInt(strId.substring(1, 2)); let j = parseInt(strId.substring(4, 5)); box[i][j] = 1; // 等于0 扩散 if (document.getElementById("bottom" + strId)[xss_clean] == 0) { zooming(i, j); } else { if (isGameOver()) { alert("真厉害"); } } } }}//扩散: 当周围有 0时,自动显示function zooming(i, j) { for (let x = i - 1; x <= i + 1; x++) { for (y = j - 1; y <= j + 1; y++) { if (document.getElementById("bottom[" + x + "][" + y + "]") && document.getElementById("bottom[" + x + "][" + y + "]")[xss_clean] == 0) { document.getElementById("over[" + x + "][" + y + "]").style.opacity = 0; if (box[x][y] == 0) { box[x][y] = 1; zooming(x, y); } } } }}//是否清除完 box[][] 初始化 雷时赋值 1,拆雷是赋值 1,全部为1时,完成扫雷。function isGameOver() { for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (box[i][j] == 0) { return false; } } } return true;}
登录后复制

相关文章:

js版扫雷实现代码 原理不错

js实现扫雷的代码

以上就是使用js开发网页版 扫雷(附代码详解)的详细内容,更多请关注易企推科技其它相关文章!


本文地址:网络知识频道 https://www.hkm168.com/jiqiao/1151266.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!


网络知识
小编:小易整编
相关文章相关阅读
  • 总结CreateJS的详细介绍

    总结CreateJS的详细介绍

    CreateJS库是一款HTML5游戏开发的引擎,是一套可以构建丰富交互体验的HTML5游戏的开源工具包,旨在降低HTML5项目的开发难度和成本,让开发者以熟悉的方式打造更具现代感的网络交互体验。掌握了CreateJS可以更方便的完成HTM...

  • jsp文件怎么创建

    jsp文件怎么创建

    创建jsp文件的步骤:1、安装和配置java开发环境;2、安装和配置web服务器;3、创建jsp文件;4、编写jsp代码;5、部署和运行应用程序;6、调试和测试。详细介绍:1、安装和配置java开发环境,首先需要在计算机上安装jdk,可以从...

  • win7升级错误代码80072efe该怎么办win7升级错误代码80072efe解决...

    win7升级错误代码80072efe该怎么办win7升级错误代码80072efe解决方案

    win7客户在系统更新的过程中遇到了80072efe的报错,像这种状况要怎么办呢?你先清查网络问题,然后去微软官网下载代理,假如你用的是32位计算机就免费下载32位代理,安装下载完成后马上重启。假如再次出现升级不正确得话,你也就再去官方网站...

  • 修复:在 Xbox 应用上的 Halo Infinite(Campaign)安装错误...

    修复:在 Xbox 应用上的 Halo Infinite(Campaign)安装错误代码 0X80070032、0X80070424 或 0X80070005

    haloinfinite(campaign)是一款第一人称射击视频游戏,于2021年11月推出,可供单人和多用户使用。该游戏是halo系列的延续,适用于windows、xboxone和xbox系列的用户x|s。最近...

  • git怎么合并分支代码

    git怎么合并分支代码

    git合并分支代码的方法:1、使用“gitmerge”命令,该命令用来做分支合并,可以将其他分支中的内容合并到当前分支中。2、使用“gitrebase”命令,该命令用于改变当前的分支的基点,进而实现分支合并。本教程操作环境:Window...

  • 如何使用Vue技术进行移动端开发

    如何使用Vue技术进行移动端开发

    如何使用Vue技术进行移动端开发随着移动互联网的迅猛发展,移动端应用的开发变得越来越重要。Vue.js作为一款轻量级、高性能的前端框架,被广泛应用于移动端开发中。本文将介绍如何使用Vue技术进行移动端开发,并给出具体的代码示例。一、准备工作...

  • 前端开发需要哪些软件

    前端开发需要哪些软件

    编程一般用的软件有:1、hbuilder;2、sublimetext;3、webstorm;4、phpstudy;5、dreamweaver;6、visualstudio;7、phpstorm;8、notepad等等。孔子说,“工欲善其...

  • 什么是构造函数?详解JavaScript中的构造函数

    什么是构造函数?详解JavaScript中的构造函数

    作为原型和原型链的基础,先了解清楚构造函数以及它的执行过程才能更好地帮助我们学习原型和原型链的知识。本篇文章带大家详细了解一下javascript中的构造函数,介绍一下怎么利用构造函数创建一个js对象,希望对大家有所帮助!一个普通的函数被用...

  • 周排行
  • 月排行
  • 年排行

精彩推荐