当前位置: 主页->网页特效专栏->游戏类->扫雷
栏目导航
网页特效
  典型特效 状态栏
  游戏类 页面背景
  页面特效 页面导航
  文本操作 文本特效
  图形特效 鼠标特效
  时间日期 密码类
  浏览相关 警告对话
  技巧类 计数转换
  测试搜索 代码生成
  播放音乐 按钮特效
  系统相关 链接特效
  黑客性质 相关特效
  窗口特效 其它特效
最新更新
·一个正在跳舞的BABY
·速度与准确性的训练
·剪子、包袱、锤游戏
·速算训练
·简单的走迷宫游戏
·鼠标点击速度测试游戏
·试试运气找好东东!
·射击游戏 小蜜蜂
·填空游戏 (不简单哟)
·21点(黑杰克)
热点文章
 
 
 
 
 
 
 
 
 
 

扫雷

http://www.yy0736.com 加入日期:2005-03-07 17:50:00 点击数:

查看:[大字体 中字体 小字体]

说明: 扫雷

效果: 点这里看效果!

代码:


<html>
<head>
<title>扫雷</title>
<script language="JavaScript">
<!-- hide code from old browsers
var BOMB = " * ";
var NOBOMB = "";
var MARKED = " X ";
var constModul = 0.0;
var constFactor = 0.0;
var constMove = 0.0;
var pseudoRandomNumber = 0.0;
var constXSize = 5;
var constYSize = 5;
var constBombAmnt = 5;
var squaresCleared = 0;
var firstTime=false;
var topValue=-1;
var rightValue=-1;
var bottomValue=-1;
var leftValue=-1;
var array;
var startTime;
var timerID = null;
var timerRunning = false;
//-------------------------------------------
function stopClock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
//-------------------------------------------
function startClock (){
// Make sure the clock is stopped
stopClock();
startTime = new Date();
showTime();
}
//--------------------------------------------
function diffSeconds (t1, t2){
// returns the difference between t1 (hh:mm:ss)
// and t2 (hh:mm:ss) in seconds!
return (t2.getHours()*3600 + t2.getMinutes()*60 +
t2.getSeconds()) - (t1.getHours()*3600 + t1.getMinutes()*60
+ t1.getSeconds());
}
//---------------------------------------------
function showTime (){
document.info.time.value = diffSeconds(startTime, new Date());
timerID = setTimeout("showTime()",1000);
timerRunning = true;
}

//---------------------------------------------
function initRandom(){
// initialize random number generator
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
constModul = Math.pow(2,48);
constFactor = 513.0
constMove = 29741096258473.0;
pseudoRandomNumber = (hours+minutes*60+seconds*3600);
}
//--------------------------------------------
function random(){
// returns the next pseudo-random number [0,1]
pseudoRandomNumber = (constFactor*pseudoRandomNumber
+constMove)%constModul;
return (pseudoRandomNumber / constModul);
}
//--------------------------------------------
function getArrayAt(x, y, value){
// Access-method of object-type ObjArray
// x,y are zero-based
return this[y*this.xSize+x];
}
//---------------------------------------------
function putArrayAt(x, y, value){
// Access-method of object-type ObjArray
// x,y are zero-based
this[y*this.xSize+x] = value;
}
//----------------------------------------------
function ObjArray(xSize, ySize){
for (i=0; i<xSize*ySize; i++)
this[i] = "";
this.xSize = xSize;
this.ySize = ySize;
this.getArrayAt = getArrayAt;
//Access-methods
this.putArrayAt = putArrayAt;
}
//----------------------------------------------
function putAreaAt(x, y, value)
// sets the value the displayed "text"-field array of pos (x,y)
// x,y are zero-based
{
var posInArea = (constXSize)+y*(constXSize+2)+(x+1);
document.area.elements[posInArea].value = value;
}
//----------------------------------------------
function getAreaAt(x, y)
// sets the value the displayed "text"-field array of pos (x,y)
// x,y are zero-based
{
var posInArea = (constXSize)+y*(constXSize+2)+(x+1);
return document.area.elements[posInArea].value;
}
//----------------------------------------------
function placeTheBombs(x, y)
// places the bombs in the array, except on position x,y
{
var i, randomXYPos;
array.putArrayAt(x, y, BOMB);
for (i=0; i<constBombAmnt; i++) {
//find a (random) position where there is still no bomb
//calculate bombs x,y position
randomXYPos = random();
randomXYPos = Math.floor(randomXYPos*constXSize*constYSize);
while (array[randomXYPos] == BOMB) {
//calculate bombs x,y position
randomXYPos = random();
randomXYPos = Math.floor(randomXYPos*constXSize*constYSize);
}
array[randomXYPos] = BOMB;
}
array.putArrayAt(x, y, NOBOMB);
}
//----------------------------------------------
function calculateBombsAround()
// calculates the amount of bombs that surround each
// square and stores it into the array
{
var x, y, x1, y1, numberOfBombs;
for (x=0; x<constXSize; x++)
for (y=0; y<constYSize; y++){
if (array.getArrayAt(x, y) == BOMB) continue;
numberOfBombs=0;
for(x1=x-1; x1<x+2; x1++)
for(y1=y-1; y1<y+2; y1++)
if((x1>=0) && (y1>=0) && (x1<constXSize) && (y1<constYSize))
if (array.getArrayAt(x1, y1) == BOMB)
numberOfBombs++;
array.putArrayAt(x, y, " "+numberOfBombs+" ");
//putAreaAt(x, y, " "+numberOfBombs+" ");
}
}
//----------------------------------------------
function initArea(){
var randomXYPos;
var numberOfBombs;
var i, x, y;
squaresCleared = 0;
document.info.bombsLeft.value = constBombAmnt;
// init. display
for (x=0; x<constXSize; x++)
for (y=0; y<constYSize; y++)
putAreaAt (x, y, "");
// create area in memory, too
array = new ObjArray(constXSize, constYSize);
firstTime = true;
}
//----------------------------------------------
function markSquare(x, y){
bottomValue=-1;
leftValue=-1;
if (getAreaAt(x,y)==MARKED){
putAreaAt(x,y,"");
document.info.bombsLeft.value++;
}
else{
// if the user clicks on a square that has already been opened
if (getAreaAt(x,y)!=NOBOMB){
// warn user and abort
alert("You've already done this square!")
return;
}
putAreaAt(x,y, MARKED);
document.info.bombsLeft.value--;
}
return;
}
//----------------------------------------------
function openSquare(x, y){
topValue=-1;
rightValue=-1;
squaresCleared++;
// if the user clicks on a square that has
// already been opened or been marked
if (getAreaAt(x,y)!=NOBOMB){
// warn user and abort
alert("You've already done this square!")
return;
}
// prevent user from beeing bombed the first time!
if (firstTime) {
placeTheBombs(x, y);
calculateBombsAround();
firstTime = false;
}
putAreaAt(x,y, squareContents = array.getArrayAt(x,y));
if (squareContents==BOMB){
alert("GAME-OVER, you lost!");
showAll();
}
else{
if (squaresCleared==constXSize*constYSize-constBombAmnt)
{
alert("Congratulations, you made it in "
+ document.info.time.value+" seconds!");
showAll();
}
}
}
//--------------------------------------------
function onButtonClicked(button){
var pos = parseInt (button.value);
if (button.name=="top")
topValue=pos;
if (button.name=="bottom")
bottomValue=pos;
if (button.name=="left")
leftValue=pos;
if (button.name=="right")
rightValue=pos;
if ((bottomValue>=0) && (leftValue>=0))
markSquare (bottomValue, leftValue)
if ((topValue>=0) && (rightValue>=0))
openSquare(topValue, rightValue)
}
//--------------------------------------------
function showAll()
// displays the area like it is represented in the array
{
var x,y;
stopClock();
// init. display
for (x=0; x<constXSize; x++)
for (y=0; y<constYSize; y++)
putAreaAt (x, y, array.getArrayAt(x,y));
}

//--------------------------------------------
function startNewGame(){
initRandom();
initArea();
startClock();
}
// end hiding from old browsers -->
</script>
</head>


<BODY BGCOLOR="#FFFFFF" onLoad="startNewGame()">
<HR>
<center>
<h1><font color="red">扫 雷</font></h1>
<form name=info>
<input type="button" value="开始新游戏"
onClick="startNewGame()"> 用时:
<input type="text" name=time size=7>
剩余雷数:<input type="text" name=bombsLeft size=5>
</form>
<form name=area>
<table border=1><tr><td></td><td>
<input type="button" name=top value="0"
onClick="onButtonClicked(this)">
</td><td>
<input type="button" name=top value="1"
onClick="onButtonClicked(this)">
</td>
<td>
<input type="button" name=top value="2"
onClick="onButtonClicked(this)">
</td><td>
<input type="button" name=top value="3"
onClick="onButtonClicked(this)">
</td><td>
<input type="button" name=top value="4"
onClick="onButtonClicked(this)">
</td>
<td></td></tr><tr><td>
<input type="button" name=left value="0"
onClick="onButtonClicked(this)">
</td><td>
<input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td>
<input type="button" name=right value="0"
onClick="onButtonClicked(this)">
</td></tr><tr><td>
<input type="button" name=left value="1"
onClick="onButtonClicked(this)">
</td><td>
<input type="text" size=3>
</td><td>
<input type="text" size=3>
</td><td>
<input type="text" size=3>
</td><td>
<input type="text" size=3>
</td><td>
<input type="text" size=3></td><td>
<input type="button" name=right value="1"
onClick="onButtonClicked(this)">
</td></tr><tr><td>
<input type="button" name=left value="2"
onClick="onButtonClicked(this)">
</td><td>
<input type="text" size=3>
</td><td>
<input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td>
<input type="button" name=right value="2"
onClick="onButtonClicked(this)">
</td></tr><tr><td>
<input type="button" name=left value="3"
onClick="onButtonClicked(this)">
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="button" name=right value="3"
onClick="onButtonClicked(this)">
</td></tr><tr><td>
<input type="button" name=left value="4"
onClick="onButtonClicked(this)">
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="text" size=3>
</td><td><input type="button" name=right value="4"
onClick="onButtonClicked(this)">
</td></tr><tr>
<td></td><td>
<input type="button" name=bottom value="0"
onClick="onButtonClicked(this)">
</td><td><input type="button" name=bottom value="1"
onClick="onButtonClicked(this)">
</td><td>
<input type="button" name=bottom value="2"
onClick="onButtonClicked(this)">
</td><td>
<input type="button" name=bottom value="3"
onClick="onButtonClicked(this)">
</td><td>
<input type="button" name=bottom value="4"
onClick="onButtonClicked(this)">
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>


作者:佚名 来源: 网上转帖  

   

上篇:TicTacToe人工智能版   下篇:21点(黑杰克)


[夜鹰论坛] [我要留言] [关闭窗口] [ ][TOP]

此文章最新评论(不超过十条)
■评论此文章 ( 有问题请去夜鹰论坛发帖 )
共有评论: 查看全部评论 姓名:
【 声明 】 您所发表的言论将被众多网友阅读,因此,您所发表的言论应不违反中国法律,不违背一般的道德原则,否则,您必须对您的不当言论引发的一切不良后果负责;此外,我们的管理员有权删除您发表的不当言论,谢谢合作!