扫雷游戏,html和javascript写的

<!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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值