html
JavaScript版掃雷
剩余雷數:
0 個 持續時間:
0 秒 難度選擇: 初級(10*10)
中級(15*15)
高級(20*20)
提示:
- 1、點擊“開始游戲”游戲開始計時
- 2、游戲過程中點擊“開始游戲”將開始新游戲
<script src="../js/jms.js"></script>
<script src="../js/text.js"></script>
css
.main {
margin: 10px auto;
padding: 20px;
background: #eee;
width: 600px;
zoom: 1;
}
.main table {
background: #ccc;
float: left;
}
.main table td {
border: 2px outset #eee;
font-size: 20px;
width: 32px;
height: 32px;
text-align: center;
cursor: pointer;
}
.main table td:hover {
background-color: #aaa;
}
.main #operation {
width: 180px;
float: right;
text-align: center;
}
.landMine {
background-image: url(mine.png);
background-position: center;
background-repeat: no-repeat;
}
.main table td.normal {
border: 2px solid #eee;
background-color: #aaa;
}
.main table td.normal:hover {
background-color: #aaa;
}
.flag {
background-image: url(flag.png);
background-position: center;
background-repeat: no-repeat;
}
.main:after {
clear: both;
display: block;
content: ‘’;
line-height: 0;
height: 0;
visibility: hidden;
}
.main .tip {
font-size: 14px;
margin: 5px;
}
.main .tip ul {
}
.main .tip ul li {
margin: 5px 0;
line-height: 20px;
}
.main .light {
font-size: 30px;
}
.main .red {
color: red;
}
.main .f60 {
color: #f60;
}
.main input[type=‘button’] {
padding: 2px 10px;
margin: 5px;
font-size: 20px;
cursor: pointer;
}
.main .txtleft {
text-align: left;
}
.main input[type=‘radio’],
.main fieldset label {
cursor: pointer;
}
.main fieldset {
margin: 10px 0;
line-height: 25px;
}
JS-jms.js
(function () {
var JMS = function (
id,
rowCount,
colCount,
minLandMineCount,
maxLandMineCount
) {
if (!(this instanceof JMS))
return new JMS(
id,
rowCount,
colCount,
minLandMineCount,
maxLandMineCount
);
this.doc = document;
this.table = this.doc.getElementById(id); //畫格子的表格
this.cells = this.table.getElementsByTagName(‘td’); //小格子
this.rowCount = rowCount || 10; //格子行數
this.colCount = colCount || 10; //格子列數
this.landMineCount = 0; //地雷個數
this.markLandMineCount = 0; //標記的地雷個數
this.minLandMineCount = minLandMineCount || 10; //地雷最少個數
this.maxLandMineCount = maxLandMineCount || 20; //地雷最多個數
this.arrs = []; //格子對應的數組
this.beginTime = null; //游戲開始時間
this.endTime = null; //游戲結束時間
this.currentSetpCount = 0; //當前走的步數
this.endCallBack = null; //游戲結束時的回調函數
this.landMineCallBack = null; //標記為地雷時更新剩余地雷個數的回調函數
this.doc.oncontextmenu = function () {
//禁用右鍵菜單
return false;
};
this.drawMap();
};
JMS.prototype = {//畫格子drawMap: function () {var tds = [];if (window.ActiveXObject &&parseInt(navigator.userAgent.match(/msie ([\d.]+)/i)[1]) < 8) {var css = '#JMS_main table td{background-color:#888;}',head = this.doc.getElementsByTagName('head')[0],style = this.doc.createElement('style');style.type = 'text/css';if (style.styleSheet) {style.styleSheet.cssText = css;} else {style.appendChild(this.doc.createTextNode(css));}head.appendChild(style);}for (var i = 0; i < this.rowCount; i++) {tds.push('<tr>');for (var j = 0; j < this.colCount; j++) {tds.push("<td id='m_" + i + '_' + j + "'></td>");}tds.push('</td>');}this.setTableInnerHTML(this.table, tds.join(''));},//添加HTML到TablesetTableInnerHTML: function (table, html) {if (navigator && navigator.userAgent.match(/msie/i)) {var temp = table.ownerDocument.createElement('div');temp.innerHTML = '<table><tbody>' + html + '</tbody></table>';if (table.tBodies.length == 0) {var tbody = document.createElement('tbody');table.appendChild(tbody);}table.replaceChild(temp.firstChild.firstChild, table.tBodies[0]);} else {table.innerHTML = html;}},//初始化,一是設置數組默認值為0,二是確定地雷個數init: function () {for (var i = 0; i < this.rowCount; i++) {this.arrs[i] = [];for (var j = 0; j < this.colCount; j++) {this.arrs[i][j] = 0;}}this.landMineCount = this.selectFrom(this.minLandMineCount,this.maxLandMineCount);this.markLandMineCount = 0;this.beginTime = null;this.endTime = null;this.currentSetpCount = 0;},//把是地雷的數組項的值設置為9landMine: function () {var allCount = this.rowCount * this.colCount - 1,tempArr = {};for (var i = 0; i < this.landMineCount; i++) {var randomNum = this.selectFrom(0, allCount),rowCol = this.getRowCol(randomNum);if (randomNum in tempArr) {i--;continue;}this.arrs[rowCol.row][rowCol.col] = 9;tempArr[randomNum] = randomNum;}},//計算其他格子中的數字calculateNoLandMineCount: function () {for (var i = 0; i < this.rowCount; i++) {for (var j = 0; j < this.colCount; j++) {if (this.arrs[i][j] == 9) continue;if (i > 0 && j > 0) {if (this.arrs[i - 1][j - 1] == 9) this.arrs[i][j]++;}if (i > 0) {if (this.arrs[i - 1][j] == 9) this.arrs[i][j]++;}if (i > 0 && j < this.colCount - 1) {if (this.arrs[i - 1][j + 1] == 9) this.arrs[i][j]++;}if (j > 0) {if (this.arrs[i][j - 1] == 9) this.arrs[i][j]++;}if (j < this.colCount - 1) {if (this.arrs[i][j + 1] == 9) this.arrs[i][j]++;}if (i < this.rowCount - 1 && j > 0) {if (this.arrs[i + 1][j - 1] == 9) this.arrs[i][j]++;}if (i < this.rowCount - 1) {if (this.arrs[i + 1][j] == 9) this.arrs[i][j]++;}if (i < this.rowCount - 1 && j < this.colCount - 1) {if (this.arrs[i + 1][j + 1] == 9) this.arrs[i][j]++;}}}},//獲取一個隨機數selectFrom: function (iFirstValue, iLastValue) {var iChoices = iLastValue - iFirstValue + 1;return Math.floor(Math.random() * iChoices + iFirstValue);},//通過數值找到行數和列數getRowCol: function (val) {return {row: parseInt(val / this.colCount),col: val % this.colCount,};},//獲取元素$: function (id) {return this.doc.getElementById(id);},//給每個格子綁定點擊事件(左鍵和右鍵)bindCells: function () {var self = this;for (var i = 0; i < this.rowCount; i++) {for (var j = 0; j < this.colCount; j++) {(function (row, col) {self.$('m_' + i + '_' + j).onmousedown = function (e) {e = e || window.event;var mouseNum = e.button;var className = this.className;if (mouseNum == 2) {if (className == 'flag') {this.className = '';self.markLandMineCount--;} else {this.className = 'flag';self.markLandMineCount++;}if (self.landMineCallBack) {self.landMineCallBack(self.landMineCount - self.markLandMineCount);}} else if (className != 'flag') {self.openBlock.call(self, this, row, col);}};})(i, j);}}},//展開無雷區域showNoLandMine: function (x, y) {for (var i = x - 1; i < x + 2; i++)for (var j = y - 1; j < y + 2; j++) {if (!(i == x && j == y)) {var ele = this.$('m_' + i + '_' + j);if (ele && ele.className == '') {this.openBlock.call(this, ele, i, j);}}}},//顯示openBlock: function (obj, x, y) {if (this.arrs[x][y] != 9) {this.currentSetpCount++;if (this.arrs[x][y] != 0) {obj.innerHTML = this.arrs[x][y];}obj.className = 'normal';if (this.currentSetpCount + this.landMineCount ==this.rowCount * this.colCount) {this.success();}obj.onmousedown = null;if (this.arrs[x][y] == 0) {this.showNoLandMine.call(this, x, y);}} else {this.failed();}},//顯示地雷showLandMine: function () {for (var i = 0; i < this.rowCount; i++) {for (var j = 0; j < this.colCount; j++) {if (this.arrs[i][j] == 9) {this.$('m_' + i + '_' + j).className = 'landMine';}}}},//顯示所有格子信息showAll: function () {for (var i = 0; i < this.rowCount; i++) {for (var j = 0; j < this.colCount; j++) {if (this.arrs[i][j] == 9) {this.$('m_' + i + '_' + j).className = 'landMine';} else {var ele = this.$('m_' + i + '_' + j);if (this.arrs[i][j] != 0) ele.innerHTML = this.arrs[i][j];ele.className = 'normal';}}}},//清除顯示的格子信息hideAll: function () {for (var i = 0; i < this.rowCount; i++) {for (var j = 0; j < this.colCount; j++) {var tdCell = this.$('m_' + i + '_' + j);tdCell.className = '';tdCell.innerHTML = '';}}},//刪除格子綁定的事件disableAll: function () {for (var i = 0; i < this.rowCount; i++) {for (var j = 0; j < this.colCount; j++) {var tdCell = this.$('m_' + i + '_' + j);tdCell.onmousedown = null;}}},//游戲開始begin: function () {this.currentSetpCount = 0; //開始的步數清零this.markLandMineCount = 0;this.beginTime = new Date(); //游戲開始時間this.hideAll();this.bindCells();},//游戲結束end: function () {this.endTime = new Date(); //游戲結束時間if (this.endCallBack) {//如果有回調函數則調用this.endCallBack();}},//游戲成功success: function () {this.end();this.showAll();this.disableAll();alert('Congratulation!');},//游戲失敗failed: function () {this.end();this.showAll();this.disableAll();alert('GAME OVER!');},//入口play: function () {this.init();this.landMine();this.calculateNoLandMineCount();},
};window.JMS = JMS;
})();
JS-text.js
var jms = null,
timeHandle = null;
window.onload = function () {
var radios = document.getElementsByName(‘level’);
for (var i = 0, j = radios.length; i < j; i++) {
radios[i].onclick = function () {
if (jms != null)
if (jms.landMineCount > 0)
if (!confirm(‘確定結束當前游戲?’)) return false;
var value = this.value;
init(value, value, (value * value) / 5 - value, (value * value) / 5);
document.getElementById(‘JMS_main’).style.width =
value * 40 + 180 + 60 + ‘px’;
};
}
init(10, 10);
};
function init(rowCount, colCount, minLandMineCount, maxLandMineCount) {
var doc = document,
landMineCountElement = doc.getElementById(‘landMineCount’),
timeShow = doc.getElementById(‘costTime’),
beginButton = doc.getElementById(‘begin’);
if (jms != null) {
clearInterval(timeHandle);
timeShow.innerHTML = 0;
landMineCountElement.innerHTML = 0;
}
jms = JMS(‘landmine’, rowCount, colCount, minLandMineCount, maxLandMineCount);
jms.endCallBack = function () {
clearInterval(timeHandle);
};
jms.landMineCallBack = function (count) {
landMineCountElement.innerHTML = count;
};
//為“開始游戲”按鈕綁定事件
beginButton.onclick = function () {
jms.play(); //初始化
//顯示地雷個數
landMineCountElement.innerHTML = jms.landMineCount;//開始
jms.begin();//更新花費的時間
timeHandle = setInterval(function () {timeShow.innerHTML = parseInt((new Date() - jms.beginTime) / 1000);
}, 1000);
};
}
運行效果
總結
以上是生活随笔為你收集整理的网页版扫雷(HTML/CSS/JS实践)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。