Important alert: (current site time 7/15/2013 10:48:19 PM EDT)
 

VB icon

BlackBox

Email
Submitted on: 4/2/2002 4:06:14 AM
By: Ray messinger 
Level: Intermediate
User Rating: By 1 Users
Compatibility: VbScript (browser/client side)
Views: 12934
 
     This is the BlackBox logical deduction game. 4 atoms are hidden in a box and you must shoot electrons to deduce their location. Try to use the least number of shots. I added a Trace option for my daughter to learn. Its a fun game and I am supprised that I haven't seen it listed before. Probable not the best code but it works.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: BlackBox
' Description:This is the BlackBox logical deduction game. 4 atoms are hidden in a box and you must shoot electrons to deduce their location. Try to use the least number of shots.
I added a Trace option for my daughter to learn.
Its a fun game and I am supprised that I haven't seen it listed before. Probable not the best code but it works.
' By: Ray messinger
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7393&lngWId=4'for details.'**************************************

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> Black Box </TITLE>
<META NAME="Author" CONTENT="Ray N. Messinger">
</HEAD>
<BODY BGCOLOR="floralwhite">
<div style="font-family:verdana,arial; font-size:18; color:green; font-weight:bold">Black Box - Logical deduction game</div>
<br>
<div style="font-family:verdana,arial; font-size:12; color:blue">
Try to discover the location of the 4 hidden atoms in the box by shooting electrons into it. The electrons interact with the atoms in specific ways. Shoot by clicking on the yellow edge cells. Guess the location by clicking on a green cell. After selecting the 4 locations click on the "Guess" button.
</div>
<hr>
<table cellspacing=10 cellpadding=10>
<tr>
<td>
<SCRIPT LANGUAGE="VBS">
Dim atom(5)
Dim BB(10,10)
dim gcnt
dim trace
trace=false
init
sub init
'---------- draw board ------------------
document.write ("<table border=1 bordercolor=black>")
	for r=0 to 9
		document.write ("<tr>")
		for c= 0 to 9
			if (r>0 and r<9) AND (c>0 and c<9) then
				col="green"
				cur="hand"
			else
				col="lightyellow"
				cur="crosshair"
			end if
			CellID= r & c
			if CellID= "00" OR CellID= "09" OR CellID= "90" OR CellID= "99" then
				document.write ("<TD>")
			else
				document.write ("<TD style=""text-align:center; background-color:" & Col & "; font-size:12; cursor:" & Cur & """ onClick='ShowMe' " & CellID & " width=16 height=16 id=" & CellID & " Name=" & CellID & ">")
				'document.write (CellID)
				document.write (" ")
			end if
		next
	next
document.write ("</table>")
'-------- hide atoms ------------
randomize
for a=1 to 4
	OK= false
	Do Until OK
		ac= int(rnd()*8 +1)
		ar= int(rnd()*8 +1)
		if BB(ar,ac)=1 then 
			OK=false
		else
			OK=true
		end if
	loop
	BB(ar,ac)=1
	CellID= ar & ac
	set aLoc = document.all(CellID)
	PrevColor= aLoc.style.backgroundcolor
	atom(a)= CellID
next
end sub
'======= Click =============
sub ShowMe()
	set obj= window.event.srcElement
	cc= obj.ID
	r= left(cc,1)
	c= right(cc,1)
	Currcol= obj.style.backgroundcolor
	if (r>0 and r<9) AND (c>0 and c<9) then 'guess loc
		if Currcol= "lightblue" then'
			ocol="green"
		else
			ocol="lightblue"
		end if
	else 'edge- shoot
		gcnt= gcnt+1
		ocol="yellow"
		if c=0 then dir=1 'go right
		if c=9 then dir=2 'go left
		if r=0 then dir=3 'go down
		if r=9 then dir=4 'go up
		
		currCell=cc
		currR= r
		currC= c
		finish= false
		do until finish
			if dir=1 then 'go right
				dr=0
				dc=1
				dr1= -1
				dc1= 1
				dr2= 1
				dc2= 1
				dd1=3
				dd2=4
			end if
			if dir=2 then 'go left
				dr=0
				dc=-1
				dr1= -1
				dc1= -1
				dr2= 1
				dc2= -1
				dd1=3
				dd2=4
			end if
			if dir=3 then 'go down
				dr=1
				dc=0
				dr1= 1
				dc1= -1
				dr2= 1
				dc2= 1
				dd1=1
				dd2=2
			end if
			if dir=4 then 'go up
				dr=-1
				dc=0
				dr1= -1
				dc1= -1
				dr2= -1
				dc2= 1
				dd1=1
				dd2=2
			end if
			nextR= currR + dr
			nextC= currC + dc
			nextCell= nextR & nextC
			nextR1= currR + dr1
			nextC1= currC + dc1
			nextCell1= nextR1 & nextC1
			nextR2= currR + dr2
			nextC2= currC + dc2
			nextCell2= nextR2 & nextC2
			'msgbox nextCell1 & nextcell2
			nn= bb(nextR,nextC)
			nn1= bb(nextR1,nextC1)
			nn2= bb(nextR2,nextC2)
			'------ check ----
			go=true
			if nn=1 then 'front collision
				ocol= "red"
				finish=true
				go=false
			else
			if nextR=0 OR nextR=9 OR nextC=0 OR nextC=9 then 'no collision
				ocol= "lightgreen"
				obj.innerhtml= gcnt
				set FinalLoc = document.all(nextCell)
				FinalLoc.style.backgroundcolor= "lightgreen"
				FinalLoc.innerhtml= gcnt
				finish=true
				go=false
			end if
			if nn1=1 and nn2=1 then
				go=false
				finish=true
				ocol= "orange"
			else
			if nn1=1 then 'bounce
				if currR=0 OR currR=9 OR currC=0 OR currC=9 then 'on start pos
					finish=true
					ocol= "orange"
				end if
				dir=dd1
				go=false
			end if
			if nn2=1 then 'bounce
				if currR=0 OR currR=9 OR currC=0 OR currC=9 then 'on start pos
					finish=true
					ocol= "orange"
				end if
				dir=dd2
				go=false
			end if
			end if
			end if
			
			if go then
				currR=nextR
				currC=nextC
				currCell= currR & currC
				set CurrLoc = document.all(currCell)
				if tracebox.checked=true then
					CurrLoc.style.backgroundcolor= "pink"
				end if
			end if
		loop
	end if
	obj.style.backgroundcolor= ocol
end sub
'--------------------------
sub showAtoms_onclick
	for a=1 to 4
		cc= atom(a)
		set aLoc = document.all(cc)
		acol= aLoc.style.backgroundcolor
		aLoc.style.backgroundcolor= "yellow"
	next
end sub
'-----------------------------
sub guess_onclick
	correct=0
	for a=1 to 4
		cc= atom(a)
		set aLoc = document.all(cc)
		acol= aLoc.style.backgroundcolor
		if acol= "lightblue" then'
			correct= correct+1
			aLoc.style.backgroundcolor= "blue"
		else
			aLoc.style.backgroundcolor= "yellow"
		end if
	next
	document.all("t1").value= correct & " correct in " & gcnt & " guesses!!"
end sub
'========
Sub Start_onclick()
	window.location.reload
end sub
</SCRIPT>
<td>
<table bgcolor=white width=300 style="font-family:verdana,arial; font-size:10" frame=border rules=none>
<tr bgcolor=black><td colspan=2 align=center><font color=white><B>Electron Reaction Rules</B></font></tr>
<tr><td bgcolor=lightgreen width=16 height=16>2</td><td rowspan=2 valign=baseline><B>Deflected</B>- hit an atom in an adjacent cell. It is deflected away at a right angle.</tr>
<tr><td> 
<tr><td bgcolor=red width=16 height=16>   </td><td rowspan=2 valign=baseline><B>Absorbed</B>- hit an atom head-on.</tr>
<tr><td> 
<tr><td bgcolor=orange width=16 height=16>   </td><td rowspan=2 valign=baseline> <B>Reflected</B>- hit atoms in both adjacent cells or on an adjacent atom on the perimeter of the blackbox .</tr>
<tr><td> 
</table>
</tr>
</table>
<hr>
<button id=Guess size=10>Guess</button><input type="text" size=30 id=t1 name=t1 value=""> <button id=Start size=10>Re-Start</button><hr>
Cheat:<button id=ShowAtoms size=10>Show</button> Trace <input type=checkbox id=tracebox><br>
</BODY>
</HTML>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/3/2002 4:52:06 PMRasputin

Wow! Very cool!

Thanks!

- Ras
(If this comment was disrespectful, please report it.)

 
12/24/2004 8:56:11 PM

loved it. very fun!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.