`
fx05062219
  • 浏览: 19829 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

扫雷-打发时间

阅读更多
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
#container {
	width: 670px;
	height: 670px;
	background-color: #eeffee;
	margin: 100px auto;
}

.block {
	width: 20px;
	height: 20px;
	background-color: white;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}

.block2 {
	width: 10px;
	height: 10px;
	background-color: red;
	float: left;
}

.openedBlockNormal {
	width: 20px;
	height: 20px;
	background-color: green;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}

.openedBlockBomb {
	width: 20px;
	height: 20px;
	background-color: red;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}

.openedBlockEmpty {
	width: 20px;
	height: 20px;
	background-color: yellow;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}
</style>
<script type="text/javascript">
	var EventUtil = new Object;
	EventUtil.addEvent = function(oTarget, sEventType, funName) {
		if (oTarget.addEventListener) {//for DOM;
			oTarget.addEventListener(sEventType, funName, false);
		} else if (oTarget.attachEvent) {
			oTarget.attachEvent("on" + sEventType, funName);
		} else {
			oTarget["on" + sEventType] = funName;
		}
	};
	EventUtil.removeEvent = function(oTarget, sEventType, funName) {
		if (oTarget.removeEventListener) {//for DOM;
			oTarget.removeEventListener(sEventType, funName, false);
		} else if (oTarget.detachEvent) {
			oTarget.detachEvent("on" + sEventType, funName);
		} else {
			oTarget["on" + sEventType] = null;
		}
	};
</script>
<script type="text/javascript" src="bomb.js"></script>
</head>
<body>

<div id="containers" style="cursor: pointer"></div>
</body>
<script type="text/javascript">
	document.oncontextmenu = function() {
		return false;
	};
	var parent = document.getElementById("containers");
	var container = new Container(parent, "600px", "600px", 30, 30, 0);
	container.init();
	parent.appendChild(container.html);
</script>
</html>
// this class stands for eight direction of every block.
function Direction() {
	this.up = null;
	this.right = null;
	this.down = null;
	this.left = null;
	this.leftUp = null;
	this.rightUp = null;
	this.leftDown = null;
	this.rightDown = null;
}
// every block object stands for one block in the page.
function Block(className) {
	// blocks around.
	this.neighbors = new Direction();// [up,rightUp,right,rightDown,down,leftDown,left,leftUp]
	// html element. div used here.
	this.html = null;
	// position of this block.
	this.index = -1;
	// is this block a bomb?
	this.isBomb = false;
	// how many bombs around of this blocks.
	this.bombNumAround = -1;
	// html css style
	this.className = className;
	// is it already opened?
	this.isOpen = false;
	// do some preparation for this block;
	this.init();
}
// calculate how many bombs of around.
Block.prototype.calcBombAround = function() {
	if (!this.isBomb) {
		var bombNumber = 0;
		for ( var p in this.neighbors) {
			if (typeof (this.neighbors[p]) != "function") {
				if (null != this.neighbors[p] && this.neighbors[p].isBomb) {
					bombNumber++;
				}

			}
		}
		this.bombNumAround = bombNumber;
	}
};
// return html element.
Block.prototype.getHtml = function() {
	return this.html;
};
Block.prototype.init = function() {
	var that = this;
	// create a html element.
	this.html = document.createElement("div");
	// adding mouse over handler for this block. change background color for
	// this block.
	EventUtil.addEvent(this.html, "mouseover", function(evt) {
		var element = evt.target ? evt.target : evt.srcElement;
		element.style.backgroundColor = "#eeAB6F";
	});
	// remove the style which were added in mouseover handler.
	EventUtil.addEvent(this.html, "mouseout", function(evt) {
		var element = evt.target ? evt.target : evt.srcElement;
		element.style.backgroundColor = "";
		element.style.cursor = "";
	});
	// user click some block.
	EventUtil.addEvent(this.html, "mousedown", function(evt) {
		// right click button.
		if (evt.button == 2) {
			// if this block already indicate as a bomb, then change to normal
			// block.
			if (that.getStyle() == "openedBlockBomb") {
				that.changeStyle("block");
			} else if (that.getStyle() == "block") {
				// if this block is a normal block, indicate as a bomb.
				that.changeStyle("openedBlockBomb");
			}
		} else {// left click!
			// open it
			that.open();
		}

	});

	// setting defalut style name.
	this.changeStyle(this.className);
};
// change css style.
Block.prototype.changeStyle = function(styleName) {
	this.html.setAttribute("class", styleName);
	this.html.setAttribute("className", styleName);
};
// getting csss style
Block.prototype.getStyle = function() {
	var style = this.html.getAttribute("class");
	if (style == null || typeof (style) == "undefined") {
		style = this.html.getAttribute("className");
	}
	return style;
};
Block.prototype.open = function() {
	// if there is no bomb around, change style as an empty block.
	if (this.bombNumAround == 0) {
		this.changeStyle("openedBlockEmpty");
	} else if (this.bombNumAround > 0) {
		// setting bomb number to inner html.
		this.html.innerHTML = this.bombNumAround;
		this.changeStyle("openedBlockNormal");
	} else {
		// setting style as a bomb.
		this.changeStyle("openedBlockBomb");
		alert("bomb!!");
	}

	this.isOpen = true;
	// if 0 ==bomb number,them open other block around.
	if (this.bombNumAround == 0) {
		var directions = new Array();
		directions.push("up");
		directions.push("right");
		directions.push("down");
		directions.push("left");
		directions.push("leftUp");
		directions.push("rightUp");
		directions.push("leftDown");
		directions.push("rightDown");
		for ( var i = 0; i < directions.length; i++) {
			var blockInDirection = this.neighbors[directions[i]];
			if (null != blockInDirection
					&& typeof (blockInDirection) != "undefined"
					&& !blockInDirection.isBomb && !blockInDirection.isOpen) {
				blockInDirection.open();
			}
		}
	}
};

function Container(parent, width, heigth, rows, columns, bomb) {
	this.bombNumber = bomb;
	this.parent = parent;
	this.width = width;
	this.heigth = heigth;
	this.rows = rows;
	this.columns = columns;
	this.childObject = new Array();
	this.html = null;

}
Container.prototype.init = function() {
	this.html = document.createElement("div");
	this.html.id = "container";

	var bombArray = new Object();
	// how many bombs in all.
	var bombNumber = 100;
	var index = this.rows * this.columns + 1;
	var loopNumber = 0;
	// randomly generate the position of bomb.
	while (true) {
		if (loopNumber >= bombNumber) {
			break;
		}
		var randomBombPosition = Math.floor(Math.random() * index);
		if (bombArray[randomBombPosition] != true) {
			bombArray[randomBombPosition] = true;
			loopNumber++;
		}

	}
	// generate block objects.
	for ( var i = 0; i < this.rows * this.columns; i++) {
		var block = new Block("block");

		if (bombArray[i] == true) {
			block.isBomb = true;
		}

		this.childObject.push(block);
		this.html.appendChild(block.html);
	}
	// generating the relation of blocks. neighbors around.
	for ( var j = 0; j < this.rows * this.columns; j++) {
		block = this.childObject[j];
		block.neighbors.up = this.childObject[j - this.columns];
		block.neighbors.right = this.childObject[j + 1];
		block.neighbors.down = this.childObject[j + this.columns];
		block.neighbors.left = this.childObject[j - 1];
		block.neighbors.leftUp = this.childObject[j - this.columns - 1];
		block.neighbors.rightUp = this.childObject[j - this.columns + 1];
		block.neighbors.leftDown = this.childObject[j + this.columns - 1];
		block.neighbors.rightDown = this.childObject[j + this.columns + 1];
		if (j / this.columns == 0) {
			block.neighbors.up = null;
			block.neighbors.leftUp = null;
			block.neighbors.rightUp = null;
		} else if (j / this.columns == this.rows - 1) {
			block.neighbors.down = null;
			block.neighbors.leftDown = null;
			block.neighbors.rightDown = null;
		}
		if (j % this.columns == 0) {
			block.neighbors.left = null;
			block.neighbors.leftUp = null;
			block.neighbors.leftDown = null;
		} else if (j % this.columns == this.columns - 1) {
			block.neighbors.right = null;
			block.neighbors.rightUp = null;
			block.neighbors.rightDown = null;
		}
		block.calcBombAround();
	}

};


分享到:
评论

相关推荐

    Java游戏扫雷-简易复制品.zip

    Java游戏扫雷-简易复制品.zipJava游戏扫雷-简易复制品.zip Java游戏扫雷-简易复制品.zipJava游戏扫雷-简易复制品.zip Java游戏扫雷-简易复制品.zipJava游戏扫雷-简易复制品.zip Java游戏扫雷-简易复制品.zipJava游戏...

    扫雷-程序设计报告扫雷-程序设计报告.doc

    扫雷-程序设计报告扫雷-程序设计报告.doc

    易语言源码扫雷--.rar

    易语言源码扫雷--.rar

    计算机软件-编程源码-实例7-8--扫雷--2个部分.zip

    标题"计算机软件-编程源码-实例7-8--扫雷--2个部分.zip"表明这是一个关于计算机编程的实例,具体来说是实现扫雷游戏的源代码,分为两个部分。扫雷游戏是经典的逻辑游戏,通常在操作系统中内置,通过标记雷区来揭开...

    C# 制作的扫雷--源码

    C# 制作的扫雷--包含源码,内有详细注释,保证能无误运行! 应好友请求今天将资源分从 10分 减为 3分 。 2010.10.26. 今日起资源分再降 1 分,以后就要 2 分算了,感谢大家的支持!! 2010.10.29

    计算机软件-商业源码-实例7-8--扫雷--2个部分.zip

    计算机软件-商业源码-实例7-8--扫雷--2个部分.zip

    java毕设-扫雷-项目源代码

    java毕设-扫雷-项目源代码

    扫雷-html5.zip

    本项目“扫雷-html5.zip”就是一个很好的例子,它利用HTML5技术实现了一个无需安装任何客户端的扫雷游戏,用户只需拥有一个现代浏览器即可享受游戏的乐趣。接下来,我们将深入探讨这个项目中的关键知识点。 1. ...

    python小游戏-扫雷-MineSweeping.zip

    python小游戏-扫雷-MineSweeping.zip

    MATLAB小游戏扫雷-MineSweeper.zip

    MATLAB小游戏扫雷-MineSweeper.zip 本帖最后由 wuyou136 于 2016-2-19 13:15 编辑 这是用来练习GUI编写的小游戏,规则应该不用介绍了,大家应该都很熟悉,给需要学习GUI的人分享一下源代码。游戏第一次点击时...

    扫雷-Java完整仿制.

    扫雷-Java完整的仿制

    MATLAB小游戏扫雷-main.m

    MATLAB小游戏扫雷-main.m 本帖最后由 wuyou136 于 2016-2-19 13:15 编辑 这是用来练习GUI编写的小游戏,规则应该不用介绍了,大家应该都很熟悉,给需要学习GUI的人分享一下源代码。游戏第一次点击时绝对不会...

    Tetris-www.-扫雷-javaclimb.com.rar

    这里我们关注的是一个名为"Tetris-www.-扫雷-javaclimb.com.rar"的压缩包,它包含了扫雷游戏的源码,且据描述,这个源码已经经过了实际测试,能够正常运行。这为我们提供了一个绝佳的学习和研究扫雷游戏逻辑的机会,...

    扫雷-Qt项目

    【扫雷-Qt项目】是一个使用C++编程语言和Qt框架开发的小游戏,它通过Qt的图形用户界面库提供了一种交互式的扫雷体验。Qt是一个跨平台的应用程序开发框架,支持Windows、Linux、macOS等多种操作系统。在这个项目中,...

    扫雷--QT--数据结构课设

    本题目做一个N x M 的扫雷游戏,每个方格包含两种状态:关闭(closed)和打开(opened),初始化时每个方格都是关闭的,一个打开的方格也会包含两种状态:一个数字(clue)和一个雷(bomb)。你可以打开(open)一个...

    完美模拟win7下的扫雷-saolei.fig

    完美模拟win7下的扫雷-saolei.fig 本人心血之作,下载可以,不过最好不要清除里面的个人信息,也不要任意外传,谢谢。 先说一下这个程序的用法: (1)按照win7系统自带的扫雷程序为蓝图。玩法基本一样,...

    完美模拟win7下的扫雷-saolei.m

    完美模拟win7下的扫雷-saolei.m 本人心血之作,下载可以,不过最好不要清除里面的个人信息,也不要任意外传,谢谢。 先说一下这个程序的用法: (1)按照win7系统自带的扫雷程序为蓝图。玩法基本一样,...

    Java游戏扫雷-简易复制品.zip算法资源

    Java游戏扫雷-简易复制品.zip算法资源

    扫雷-小游戏-NAS-WebStation-HTML5

    标题中的“扫雷-小游戏-NAS-WebStation-HTML5”揭示了这是一个基于HTML5技术开发的扫雷小游戏,特别设计用于在NAS(Network Attached Storage,网络附加存储)设备上的WebStation服务器上运行。这样的设置使得用户...

    和大家分享一下别人的作品小游戏扫雷-saolei.m

    和大家分享一下别人的作品小游戏扫雷-saolei.m  这是我从别人那拷来的关于小游戏扫雷的m文件,大家可以下载下来学习一下,大家共同进步嘛~~~

Global site tag (gtag.js) - Google Analytics