代码驿站移动版
频道导航
HTML/Xhtml
CSS
JavaScript
HTML5
PHP教程
ASP.NET
正则表达式
AJAX
ThinkPHP
Yii
MySQL
MariaDB
Oracle
MongoDB
Redis
DedeCMS
PHPCMS
帝国CMS
WordPress
Discuz
其它CMS
Zend Studio
Sublime
Notepad
Dreamweaver
Windows
Linux
Nginx
Apache
IIS
CentOS
Ubuntu
Debian
网站优化
工具资源
PHP源码
ASP.NET源码
其它源码
图标素材
按钮素材
字体素材
DedeCMS模板
帝国CMS模板
PHPCMS模板
WordPress模板
Discuz!模板
单页模板
开发软件下载
服务器软件下载
广告投放
联系我们
版权申明
软件编程
网页前端
移动开发
数据库
服务器
脚本语言
PHP代码
JAVA代码
Python代码
Android代码
当前位置:
主页
>
网页前端
>
JavaScript代码
>
javascript 扫雷游戏
时间:2021-07-11 08:42:29 | 栏目:
JavaScript代码
| 点击:次
<script type="text/javascript"><!-- function StringBuffer() { this._strings = new Array(); } StringBuffer.prototype.append = function(str) { this._strings.push(str); } StringBuffer.prototype.toString = function() { return this._strings.join(""); } var rownum; //row number var colnum; //column number var minenum; //mine number var totalCellNum; // total number of cell var elapseTime; //elapse time for mine sweeping var statedNum; // swept cell number var elapseSpan; // for display elapse time var tick; // timer for mine sweeping //generating mine area function init() { rownum = parseInt(document.mineform.rownum.value); colnum = parseInt(document.mineform.colnum.value); minenum = parseInt(document.mineform.minenum.value); totalCellNum = rownum * colnum; var buffer = new StringBuffer(); buffer.append("<table cellspacing=\"0\" cellpadding=\"0\" border='1'>"); for (var i = 0; i < rownum; i++) { buffer.append("<tr>"); for(var j = 0; j < colnum; j++) buffer.append("<td bgcolor='#AAA898' id=\"" + i + "_" + j+"\" name='cell' /> </td>"); buffer.append("</tr>"); } buffer.append("</table>"); var workarea = document.getElementById("workarea"); workarea.innerHTML = buffer.toString(); mine(); // lay mines at the work area startTick(); // starting time } // periodically update the elapsed time function displayelapsedTime() { elapseSpan.innerHTML = elapseTime++; if (elapseTime > 999) stopTick(); } // start up the timer for sweeping mine function startTick() { if (tick) stopTick(); elapseTime = 0; statedNum = 0; elapseSpan = document.getElementById("elapse"); document.getElementById("message").innerHTML = ""; tick = setInterval(displayelapsedTime, 1000); // start tick } // stop the timer function stopTick() { clearInterval(tick); tick = null; } // lay mines at the work area function mine() { var arr = new Array(totalCellNum); for(var i = 0; i < totalCellNum; i++) arr[i] = i; for(var i = 0; i < minenum; i++) { var suffix = Math.floor(arr.length * Math.random()); var rnd = arr[suffix]; var row = Math.floor(rnd / colnum); var col = rnd % colnum; var cell = document.getElementById(row + "_" + col); cell.mine = "●"; arr.splice(suffix, 1); } calcMineForAll(); } // calculating mine number for each cell function calcMineForAll() { for(var i = 0; i < rownum; i++) { for(var j = 0; j < colnum; j++) { calcMine(i, j); } } } // calculating mine number for given cell function calcMine(row, col) { var cell = document.getElementById(row + "_" + col); var nums = 0; if(row > 0) { if(col > 0) { if(document.getElementById((row - 1) + "_" + (col - 1)).mine) //top left nums++; } if(col < colnum - 1) { if(document.getElementById((row - 1) + "_" + (col + 1)).mine) //top right nums++; } if(document.getElementById((row - 1) + "_" + col).mine) // top nums++; } if(row < rownum - 1) { if(col > 0) { if(document.getElementById((row + 1) + "_" + (col - 1)).mine) //bottom left nums++; } if(col < colnum - 1) { if(document.getElementById((row + 1) + "_" + (col + 1)).mine) //bottom right nums++; } if(document.getElementById((row + 1) + "_" + col).mine) // bottom nums++; } if(col > 0) if(document.getElementById(row + "_" + (col - 1)).mine) //left nums++; if(col < colnum - 1) if(document.getElementById(row + "_" + (col + 1)).mine) //right nums++; //if(typeof(cell.mine) == "undefined") cell.nums = nums; } // count number of marked cell around given coordinate function countMarkNum(row, col) { var nums = 0; if(row > 0) { if(col > 0) { if(document.getElementById((row - 1) + "_" + (col - 1)).state == "mark") //top left nums++; } if(col < colnum - 1) { if(document.getElementById((row - 1) + "_" + (col + 1)).state == "mark") //top right nums++; } if(document.getElementById((row - 1) + "_" + col).state == "mark") // top nums++; } if(row < rownum -1) { if(col > 0) { if(document.getElementById((row + 1) + "_" + (col - 1)).state == "mark") //bottom left nums++; } if(col < colnum - 1) { if(document.getElementById((row + 1) + "_" + (col + 1)).state == "mark") //bottom right nums++; } if(document.getElementById((row + 1) + "_" + col).state == "mark") // bottom nums++; } if(col > 0) if(document.getElementById(row + "_" + (col - 1)).state == "mark") //left nums++; if(col < colnum - 1) if(document.getElementById(row + "_" + (col + 1)).state == "mark") //right nums++; return nums; } // click the cell function clickMe(cell) { if(typeof(cell.state) == "undefined") { if (cell.mine == "●") { // if incaution touch the mine, game immediate over gameover(cell); return; } if (cell.nums > 0) { cell.state = "up"; statedNum++; cell.className = "up"; cell.innerHTML = getColoredNum(cell.nums); } else { var a = cell.id.split("_"); var row = parseInt(a[0]); var col = parseInt(a[1]); disclose(row, col); } isWin(); } } // touch the mine, game over function gameover(element) { for(var i = 0; i < rownum; i++) { for(var j = 0; j < colnum; j++) { var cell = document.getElementById(i + "_" + j); if (cell.state == "up") continue; if (cell.mine) { if (typeof(cell.state) == "undefined") { cell.innerHTML = "●"; cell.style.cssText = "font-family:宋体"; } } } } element.state = "up"; element.innerHTML = "●"; element.style.cssText = "color:#AA0000;font-family:宋体;font-size:14px"; stopTick(); document.getElementById("message").innerHTML = "<font color='#DD0000'>Sorry, you lost the game!</font>"; } // get colored number, difference number will be colored difference color function getColoredNum(nums) { var color; switch(nums) { case 1: color = "#0000EE"; break; case 2: color = "#00AA00"; break; case 3: color = "#990000"; break; case 4: color = "#FF0000"; break; case 5: color = "#550000"; break; case 6: color = "#550055"; break; case 7: color = "#223366"; break; default: color = "#000000"; } return "<font color=" + color + ">" + nums + "</font>"; } function disclose(row, col) { var cell = document.getElementById(row + "_" + col); if(cell && typeof(cell.mine) == "undefined" && typeof(cell.state) == "undefined") { cell.state = "up"; cell.className = "up"; statedNum++; if (cell.nums == 0) { if(row > 0) { disclose(row - 1, col); if(col > 0) // top left disclose(row - 1, col - 1); if(col < colnum -1) // top right disclose(row - 1, col + 1); } if(row < rownum - 1) { disclose(row + 1, col); if(col > 0) // bottom left disclose(row + 1, col - 1); if(col < colnum -1) // bottom right disclose(row + 1, col + 1); } if(col > 0) disclose(row, col - 1); // left if(col < colnum -1) disclose(row, col + 1); // right } else { cell.innerHTML = getColoredNum(cell.nums); } } } // mark or unmark a cell function markMe(cell) { if (cell.state == "mark") { cell.innerHTML = " "; cell.state = undefined; statedNum--; } else if (typeof(cell.state) == "undefined") { cell.innerHTML = "F"; cell.state = "mark"; statedNum++; isWin(); } } // determine the game is win function isWin() { if (statedNum == totalCellNum) { document.getElementById("message").innerHTML = "<font color='#008800'>Congratulations, you won the game!</font>"; stopTick(); } } // when left and right key down function doubleClick(cell) { if(typeof(cell.state) == "undefined") { clickMe(cell); return; } var a = cell.id.split("_"); var row = parseInt(a[0]); var col = parseInt(a[1]); if (cell.innerHTML.indexOf(">" + countMarkNum(row, col) + "</") > -1) { if(row > 0) { clickMe(document.getElementById((row - 1) + "_" + col)); if(col > 0) // top left clickMe(document.getElementById((row - 1) + "_" + (col - 1))); if(col < colnum -1) // top right clickMe(document.getElementById((row - 1) + "_" + (col + 1))); } if(row < rownum - 1) { clickMe(document.getElementById((row + 1) + "_" + col)); if(col > 0) // bottom left clickMe(document.getElementById((row + 1) + "_" + (col - 1))); if(col < colnum -1) // bottom right clickMe(document.getElementById((row + 1) + "_" + (col + 1))); } if(col > 0) clickMe(document.getElementById(row + "_" + (col - 1))); // left if(col < colnum -1) clickMe(document.getElementById(row + "_" + (col + 1))); // right } } // process all mouse key down event document.onmousedown = function() { var e = event ? event : arguments[0] ; var cell = e.srcElement ? e.srcElement : e.target; if(cell.parentNode.name == "cell") cell = cell.parentNode ; if(cell.name == "cell") { if (e.button == 1) //left click clickMe(cell); else if (e.button == 2) //right click markMe(cell); else if (e.button == 3) //left and right click doubleClick(cell); } return true; } // --></script> <style type="text/css"><!-- body { font-size:14px; font-family: verdana; } input { font-size:12px; padding: 0px; margin: 0px; } table {border: 2px solid #6E6560;} td { height:19px; width:20px; font-family: Arial Black; text-align: center; padding-top:3px; font-size: 12px; cursor:default; border-left: 2px solid #D5D3D0; border-top: 2px solid #D5D3D0; border-right: 2px solid #6E6560; border-bottom: 2px solid #6E6560; } .up { border-right: 2px solid #F6F3F0; border-bottom: 2px solid #F6F3F0; background:#F6F3F0; } --></style><style type="text/css" bogus="1">body { font-size:14px; font-family: verdana; } input { font-size:12px; padding: 0px; margin: 0px; } table {border: 2px solid #6E6560;} td { height:19px; width:20px; font-family: Arial Black; text-align: center; padding-top:3px; font-size: 12px; cursor:default; border-left: 2px solid #D5D3D0; border-top: 2px solid #D5D3D0; border-right: 2px solid #6E6560; border-bottom: 2px solid #6E6560; } .up { border-right: 2px solid #F6F3F0; border-bottom: 2px solid #F6F3F0; background:#F6F3F0; }</style> <html> <body oncontextmenu="return false" onselectstart="return false"> <div align="center"> <form name="mineform"> Row: <input type="text" name="rownum" size="5" value="15"/> Column: <input type="text" name="colnum" size="5" value="20"/> Mine Number: <input type="text" name="minenum" size="5" value="30"/> <input type="button" value=" Start " name="start" onclick="init()"/> Elapsed Time: <span id="elapse" style="width:30px;font-family:Arial Black" align="right"></span> </form> <p></p> <div id="workarea"></div> <p></p> <p></p> <div id="message" style="font-family:Arial Black;font-size:16px" style="font-family:Arial Black;font-size:16px"></div> </div> </body> </html>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
您可能感兴趣的文章:
JavaScript常见事件处理程序实例总结
Javascript的匿名函数小结
url传递的参数值中包含&时,url自动截断问题的解决方法
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
兼容浏览器的js事件绑定函数(详解)
相关文章
10-19
javascript上下左右定时滚动插件
11-23
Webpack中loader打包各种文件的方法实例
11-27
Javascript实例教程(19) 使用HoTMetal(2)
10-05
javascript判断图片是否加载完成的方法推荐
11-22
在javascript中执行任意html代码的方法示例解读
JQuery
VUE
AngularJS
MSSql
MySQL
MongoDB
Redis
Linux
Tomcat
Nginx
网站首页
广告投放
联系我们
版权申明
联系站长