注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

浩瀚星云

上善若水 厚德载物<学习版>

 
 
 

日志

 
 

扫雷  

2008-05-03 10:24:23|  分类: ASP(过时) |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

<html>
<head>
<title>网页特效|Linkweb.cn/Js|---扫雷</title>
</head><BODY onLoad="startNewGame()">
<!-- 用<BODY onLoad="startNewGame()">替换原有的<body> -->
<!--将以下代码加入HTML的<Body></Body>之间-->

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

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

  评论这张
 
阅读(29)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017