javascript 扫雷游戏
");
for (var i = 0; i ");
for(var j = 0; j
");
buffer.append("
");
}
buffer.append("
");
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 0) {
if(col > 0) {
if(document.getElementById((row - 1) + "_" + (col - 1)).mine) //top left
nums++;
}
if(col 0) {
if(document.getElementById((row + 1) + "_" + (col - 1)).mine) //bottom left
nums++;
}
if(col 0)
if(document.getElementById(row + "_" + (col - 1)).mine) //left
nums++;
if(col 0) {
if(col > 0) {
if(document.getElementById((row - 1) + "_" + (col - 1)).state == "mark") //top left
nums++;
}
if(col 0) {
if(document.getElementById((row + 1) + "_" + (col - 1)).state == "mark") //bottom left
nums++;
}
if(col 0)
if(document.getElementById(row + "_" + (col - 1)).state == "mark") //left
nums++;
if(col 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 Sorry, you lost the game!";
}
// 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 "" + nums + "";
}
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 0) // bottom left
disclose(row + 1, col - 1);
if(col 0)
disclose(row, col - 1); // left
if(col Congratulations, you won the game!";
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 0) // bottom left
clickMe(document.getElementById((row + 1) + "_" + (col - 1)));
if(col 0)
clickMe(document.getElementById(row + "_" + (col - 1))); // left
if(col
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;
}
Row:
Column:
Mine Number:
Elapsed Time:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]