<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="Shirman">
<meta name="Keywords" content="扫雷,javascript">
<meta name="Description" content="learn javascript">
<title>扫雷</title>
<script type="text/javascript">
var table;
var tableLen;
var bombNum;
function doOnload(){
table = document.getElementsByTagName('table')[0];
table.innerHTML='';
tableLen= Math.floor(Math.random()*(5)+4);
bombNum = Math.floor(Math.random()*(10)+4);//炸弹个数[4,8]
bombCount = 0;
for(var i=0;i<tableLen;i++){
var row = table.insertRow();
for(var j=0;j<tableLen;j++){
var cell = row.insertCell();
cell.width=60;
cell.height=60;
cell.innerHTML = '<img οnclick="doBomb()" src="img/gril.jpg" width="60" height="60" border="1" alt="嫌疑">';
cell.id=j+'a'+i;
if((cell.value=Math.floor(Math.random()*5))==1){//随机定义是否为炸弹
bombCount++;
if(bombCount>bombNum){//控制炸弹个数
cell.value=0;
}
}
}
}
}
function doBomb(){
var e = event.srcElement;
var isBomb = e.parentNode.value;
var neighborBombNum = 0;
var bombId = e.parentNode.id;
var index = bombId.indexOf("a");
var x = bombId.substring(0,index);
var y = bombId.substring(index+1);
var neighbor;
var neighborId;
//检测四周是否存在炸弹
if(x!=0){//左边
neighborId = (parseInt(x)-1)+'a'+y;
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
if(y!=0){//左上边
neighborId = (parseInt(x)-1)+'a'+(parseInt(y)-1);
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
}
if(y<(tableLen-1)){//左下边
neighborId = (parseInt(x)-1) +'a'+(parseInt(y)+1);
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
}
}
if(x<(tableLen-1)){//右边
neighborId = (parseInt(x)+1)+'a'+y;
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
if(y!=0){//右上边
neighborId = (parseInt(x)+1)+'a'+(parseInt(y)-1);
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
}
if(y<(tableLen-1)){//右下边
neighborId = (parseInt(x)+1) +'a'+(parseInt(y)+1);
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
}
}
if(y!=0){//上边
neighborId = parseInt(x)+'a'+(parseInt(y)-1);
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
}
if(y<(tableLen-1)){//下边
neighborId = x +'a'+(parseInt(y)+1);
neighbor = document.getElementById(neighborId);
if(neighbor.value==1){
neighborBombNum++;
}
}
if(isBomb==1){//爆炸
for(var i=0;i<tableLen;i++){
var row = table.insertRow();
for(var j=0;j<tableLen;j++){
var allId = j+'a'+i;
var all = document.getElementById(allId);
if(all.value==1){
all.childNodes[0].src='img/fengjie.jpg';
}
}
}
if(confirm("凤姐都笑了,逗比派来的猴子,再来一局吗?")){
doOnload();
}else{
doOnload();
}
}else{//安全
var parent = e.parentNode;
parent.innerHTML='<h3 align=center>'+neighborBombNum+'</h3>';
//parent.innerText=neighborBombNum;
}
}
</script>
</head>
<body οnlοad='doOnload();' bgcolor=ffff33>
<center>
<div id="" class="">
<span class="">author:shirman qq:806820008</span>
</div>
<table bgcolor=ccff66 border=1 cellspacing=0 cellpadding=0 align=center>
</table><br><br><br><br>
</center>
</body>
</html>