Dev Shed Lounge
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDev Shed Lounge

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old May 15th, 2002, 09:25 AM
binky's Avatar
binky binky is offline
Gerbil
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: In a Rotastak
Posts: 1,763 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 22 h 12 m 52 sec
Reputation Power: 18
Red face Boredom revisited...

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!

__________________
- Sorted!

www.ppfuk.com - Free Photo Sharing

Reply With Quote
  #2  
Old May 15th, 2002, 10:26 AM
NoXcuz's Avatar
NoXcuz NoXcuz is offline
Wiking
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Sep 2000
Location: Sweden
Posts: 3,608 NoXcuz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 27 sec
Reputation Power: 11
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

Reply With Quote
  #3  
Old May 15th, 2002, 10:36 AM
binky's Avatar
binky binky is offline
Gerbil
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: In a Rotastak
Posts: 1,763 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 22 h 12 m 52 sec
Reputation Power: 18
Does it??

Jeez, that's lucky. Was expecting errors in everything but IE6.

Reply With Quote
  #4  
Old May 16th, 2002, 02:29 AM
Datamike's Avatar
Datamike Datamike is offline
Web Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2001
Location: Finland
Posts: 719 Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 27 m 33 sec
Reputation Power: 9
Thumbs up Nice Work!

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDev Shed Lounge > Boredom revisited...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway