|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
So, the last time I was bored the Matrix was the target. Now it's Microsoft... or at least MS minesweeper.
So here you go: <html> <head> <style type="text/css"> input.display {font-family:arial; font-size:14pt; text-align:left; border:none; background-color:white; width:100;} input.box {font-family:wingdings; width:25; height:25; text-align:center;} td.box {width:25; height:25;} </style> <script type="text/javascript"> // set grid size var height = 7 var width = 7 var totalsquares = height * width // set mine number var mines = 4 var minearray = new Array() // set time limit in seconds var timelimit = 180 var timer = null var isgame = false function init() { document.getElementById("mines").value = mines for (x = 1; x <= mines; x++) { random = Math.ceil(Math.random() * totalsquares) distributemines(x - 1, random) } } function distributemines(minenumber, value) { isvalid = true for (y = 0; y < minearray.length; y++) { if (minearray[y] != value) { isvalid = true } else { isvalid = false break } } if (isvalid) { minearray[minenumber] = value } else { random = Math.ceil(Math.random() * totalsquares) distributemines(minenumber, random) } } function startstop() { if (!isgame) { isgame = true document.getElementById("startrow").style.display = "none" document.getElementById("flagrow").style.display = "block" sectimer = setTimeout("uptime()", 1000) } } function uptime() { if (isgame) { seconds = parseInt(document.getElementById("time").value) seconds++ document.getElementById("time").value = seconds if (seconds == timelimit) { clearTimeout(sectimer) reveal() alert("You've run out of time!") } else { sectimer = setTimeout("uptime()", 1000) } } } function checkme(value) { var issafe = true if (isgame) { if (hasflag) { if (document.getElementById("box" + value).value == "O") { document.getElementById("box" + value).value = "" } else { document.getElementById("box" + value).value = "O" } } else { for (x = 0; x < minearray.length; x++) { if (minearray[x] == value) { clearTimeout(sectimer) reveal() issafe = false break } } if (issafe) { removefields(value) } document.getElementById("box" + value).blur() checkval = 0 for (x = 1; x <= totalsquares; x++) { if (document.getElementById("box" + x).style.backgroundColor == "white") { checkval++ } } if (checkval == totalsquares - mines) { clearTimeout(sectimer) reveal() isgame = false; alert("Winner! Well done.") } } } } function reveal() { for (x = 0; x < minearray.length; x++) { document.getElementById("box" + minearray[x]).value = "M" } for (x = 1; x <= totalsquares; x++) { document.getElementById("box" + x).style.backgroundColor = "white" document.getElementById("box" + x).style.border = "dashed thin" document.getElementById("box" + x).blur() } } function removefields(value) { if (isgame) { posinrow = 0 posinrow = (value % width == 0) ? width : value % width ; posincol = 0 for (x = 1; x <= value; x++) { if (x % width == 1) { posincol++ } } minecount = 0 if (posinrow != 1) { minecount = minecount + calcmines(value - 1) } if (posinrow != width) { minecount = minecount + calcmines(value + 1) } if (posincol != 1) { above = value - width minecount = minecount + calcmines(above) if (posinrow != 1) { minecount = minecount + calcmines(above - 1) } if (posinrow != width) { minecount = minecount + calcmines(above + 1) } } if (posincol != height) { below = value + width minecount = minecount + calcmines(below) if (posinrow != 1) { minecount = minecount + calcmines(below - 1) } if (posinrow != width) { minecount = minecount + calcmines(below + 1) } } document.getElementById("box" + value).style.backgroundColor = "white" document.getElementById("box" + value).style.border = "dashed thin" if (minecount != 0) { document.getElementById("box" + value).style.fontFamily = "arial" document.getElementById("box" + value).value = minecount } } } function calcmines(value) { ismine = false for (x = 0; x < minearray.length; x++) { if (minearray[x] == value) { ismine = true break } } if (ismine) { return 1 } else { return 0 } } var hasflag = false function pickflag() { if (!hasflag) { document.getElementById("flag").style.color = "red" hasflag = true } else { document.getElementById("flag").style.color = "black" hasflag = false } document.getElementById("flag").blur() } </script> </head> <body onLoad="init()" bgcolor='#ffffff'> <table border='0'> <tr><td>Mines:</td><td><input class='display' type='text' size='3' id='mines'></td><td>Time:</td><td><input class='display' type='text' size='3' id='time' value='0'></td></tr> </table> <table border='0' cellpadding='0' cellspacing='0'> <script type="text/javascript"> for (x = 1; x <= totalsquares; x++) { if (x % width == 1) { document.write("<tr>") } document.write("<td class='box'><input type='button' id='box" + x + "' onClick='checkme(" + x + ")' class='box'></td>") if (x % width == 0) { document.write("</tr>") } } </script> </table> <table border='0'> <tr id='startrow'><td><input class='display' id='start' type='button' onClick='startstop()' value='start'></td></tr> <tr id='flagrow' style='display:none;'><td><input class='display' id='flag' type='button' onClick='pickflag()' value='flag'></td></tr> <tr><td><input class='display' id='reload' type='button' onClick='if(confirm("Start again?")) { window.location.reload() }' value='replay'></td></tr> </table> </body> </html> Enjoy! ![]() |
|
#2
|
||||
|
||||
|
Heh, it even works in Opera this time...
Cool! //NoXcuz
__________________
UN*X is sexy! who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep |
|
#3
|
||||
|
||||
|
Does it??
Jeez, that's lucky. Was expecting errors in everything but IE6. |
|
#4
|
||||
|
||||
|
It's pretty cool. Can't think of any use for it but still a nice piece of coding
![]()
__________________
-- Tomi Kaistila -- Developer's Journal The more you learn, the more you know. The more you know, the more you forget. The more you forget, the less you know. |
![]() |
| Viewing: Dev Shed Forums > Other > Dev Shed Lounge > Boredom revisited... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|