游戏类扫雷
把下列代码加到网页的<body></body>标签之间 从google下载我的照片管理软件<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
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;
}
//--------------------------------------------------------------------------
function putarrayat(x, y, value){
// access-method of object-type objarray
// x,y are zero-based
this = value;
}
//--------------------------------------------------------------------------
function objarray(xsize, ysize){
for (i=0; i<xsize*ysize; i++)
this = "";
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.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.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 == bomb) {
//calculate bombs x,y position
randomxypos = random();
randomxypos = math.floor(randomxypos*constxsize*constysize);
}
array = 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>
页:
[1]