472,345 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Defining forms?

Hi,
I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other browser
(Opera, Netscape, Mozilla). It has no form defined yet it uses form buttons.
Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
--
To contact me, please use andrewbroomhead {AT} yahoo {DOT} co {DOT} uk

I have deleted some pointless HTML code from the beginning

<script type="text/JavaScript">
<!--
var GamePics = new Array(9);
var i;
var j;
var ind;
var AccClick=0;
var PlayingGame=0;
GamePics[0] = new Image();
GamePics[0].src="images/1.gif";
GamePics[1] = new Image();
GamePics[1].src="images/2.gif";
GamePics[2] = new Image();
GamePics[2].src="images/3.gif";
GamePics[3] = new Image();
GamePics[3].src="images/4.gif";
GamePics[4] = new Image();
GamePics[4].src="images/5.gif";
GamePics[5] = new Image();
GamePics[5].src="images/6.gif";
GamePics[6] = new Image();
GamePics[6].src="images/7.gif";
GamePics[7] = new Image();
GamePics[7].src="images/8.gif";
GamePics[8] = new Image();
GamePics[8].src="images/Blank.gif";
function Clicked(NumClicked)
{
if (AccClick == 0)
alert ("Please click on Start Game! to begin");
else
{
if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);
if (NumClicked == 1 && image[3].src == GamePics[8].src)
Swap(0,3);
if (NumClicked == 2 && image[0].src == GamePics[8].src)
Swap(1,0);
if (NumClicked == 2 && image[2].src == GamePics[8].src)
Swap(1,2);
if (NumClicked == 2 && image[4].src == GamePics[8].src)
Swap(1,4);
if (NumClicked == 3 && image[1].src == GamePics[8].src)
Swap(2,1);
if (NumClicked == 3 && image[5].src == GamePics[8].src)
Swap(2,5);
if (NumClicked == 4 && image[0].src == GamePics[8].src)
Swap(3,0);
if (NumClicked == 4 && image[4].src == GamePics[8].src)
Swap(3,4);
if (NumClicked == 4 && image[6].src == GamePics[8].src)
Swap(3,6);
if (NumClicked == 5 && image[3].src == GamePics[8].src)
Swap(4,3);
if (NumClicked == 5 && image[1].src == GamePics[8].src)
Swap(4,1);
if (NumClicked == 5 && image[5].src == GamePics[8].src)
Swap(4,5);
if (NumClicked == 5 && image[7].src == GamePics[8].src)
Swap(4,7);
if (NumClicked == 6 && image[2].src == GamePics[8].src)
Swap(5,2);
if (NumClicked == 6 && image[4].src == GamePics[8].src)
Swap(5,4);
if (NumClicked == 6 && image[8].src == GamePics[8].src)
Swap(5,8);
if (NumClicked == 7 && image[7].src == GamePics[8].src)
Swap(6,7);
if (NumClicked == 7 && image[3].src == GamePics[8].src)
Swap(6,3);
if (NumClicked == 8 && image[6].src == GamePics[8].src)
Swap(7,6);
if (NumClicked == 8 && image[4].src == GamePics[8].src)
Swap(7,4);
if (NumClicked == 8 && image[8].src == GamePics[8].src)
Swap(7,8);
if (NumClicked == 9 && image[5].src == GamePics[8].src)
Swap(8,5);
if (NumClicked == 9 && image[7].src == GamePics[8].src)
Swap(8,7);
WinCheck();
}
}
function Swap(Pic1, Pic2)
{
spare=new Image();
spare.src=document.image[Pic1].src;
document.image[Pic1].src=document.image[Pic2].src;
document.image[Pic2].src=spare.src;
}
function Timer()
{
if(PlayingGame==1)
{
timeTaken.value++;
setTimeout('Timer()', 1000);
}
}
function Shuffle()
{
if(AccClick==1)
{
AccClick=0;
PlayingGame=0;
startButton.value="Start Game!";
timeTaken.value=0;
return;
}
else
startButton.value="Stop Game!";
for (j=0;j<8;j++)
{
ind=Math.round(8*Math.random());
Swap(j,ind);
}
AccClick = 1;
timeTaken.value=0;
PlayingGame=1;
Timer();
}
function WinCheck()
{
var NoCorrect=0;
for (i=0;i<8;i++)
{
if (document.image[i].src == GamePics[i].src)
NoCorrect++;
}
if (NoCorrect == 8)
{
startButton.value="Start Game!";
PlayingGame=0;
AccClick=0;
alert("Congratulations, You have completed the game! It took
"+timeTaken.value+" seconds!");
timeTaken.value=0;
}
}
//-->
</script>
<center><table class="content">
<tr>
<td class="innerContent"><table width="300" border="0" cellspacing="1"
cellpadding="1"
height="300">
<tr>
<td><A border='0' href="javascript:Clicked('1')"><img border='0'
name="image" src="images/1.gif"></A></td>
<td><A border='0' href="javascript:Clicked('2')"><img border='0'
name="image" src="images/2.gif"></A></td>
<td><A border='0' href="javascript:Clicked('3')"><img border='0'
name="image" src="images/3.gif"></A></td>
</tr>
<tr>
<td><A border='0' href="javascript:Clicked('4')"><img border='0'
name="image" src="images/4.gif"></A></td>
<td><A border='0' href="javascript:Clicked('5')"><img border='0'
name="image" src="images/5.gif"></A></td>
<td><A border='0' href="javascript:Clicked('6')"><img border='0'
name="image" src="images/6.gif"></A></td>
</tr>
<tr>
<td><A border='0' href="javascript:Clicked('7')"><img border='0'
name="image" src="images/7.gif"></A></td>
<td><A border='0' href="javascript:Clicked('8')"><img border='0'
name="image" src="images/8.gif"></A></td>
<td><A border='0' href="javascript:Clicked('9')"><img border='0'
name="image" src="images/Blank.gif"></A></td>
</tr>
</table>
<p>
<input type='button' type='button' name='startButton' value='Start
Game!' OnMouseDown='javascript:Shuffle()'>
<font color="#FFFFFF" size="3" face="Geneva, Arial, Helvetica,
sans-serif">
You have taken&nbsp;
<input class='box' type='text' name='timeTaken' readonly size='2'
value='0'>
seconds</font><br>
</p>
<p>&nbsp; </p>
<table width="100%" border="0">
<tr>
<td width="72%"><font color="#FFFFFF" size="3" face="Verdana, Arial,
Helvetica,
sans-serif">The
completed picture looks like this: </font></td>
<td width="28%"><font face="Verdana, Arial, Helvetica,
sans-serif"><img
src="Images/Complete.gif" width="200" height="200" border="2"></font></td>
</tr>
</table>
<p>&nbsp;</p></body>
</html>
Jul 20 '05 #1
10 1447
On Tue, 10 Feb 2004 18:04:34 -0000, Andy B <No Spam Thanks> wrote:
I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other
browser (Opera, Netscape, Mozilla). It has no form defined yet it uses
form buttons.
Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
I'll quote sections of your code, then comment on any issues I see. Some
of the comments will be general guidelines only, and won't actually solve
your problem. However, they should still be given some consideration.
<A border='0' href="javascript:Clicked('1')"><img border='0'
name="image" src="images/1.gif"></A>
1) Don't use the JavaScript pseudo-protocol (javascript:) without a good
reason. You can do the above with either of:

<a href="" onclick="Clicked('1'); return false;"><img src="images/1.gif"
alt="short description"></a>

or:

<img src="images/1.gif" alt="short description" onclick="Clicked('1')">

Note that the former should be styled with CSS, and the latter won't work
with NN4.

2) You pass the string, '1', in the call above, but you check against a
number in the function:

function Clicked(NumClicked) {
...
if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);

Choose one or the other.

if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);
You use 'image' above as a global variable. However, it is not - it is the
name of several HTML elements. This is where the main issue lies. You can
access the images through the document.images collection. This can be done
either by index (first image in the entire document is 0), or by name. If
you use the latter, you'll have to give each image a unique name.
<input type='button' type='button' name='startButton' value='Start
Game!' OnMouseDown='javascript:Shuffle()'>
You probably want to use the onclick intrinsic event, not onmousedown.
Furthermore, prepending "javascript:" to an intrinsic event does not
specify the language in any browser other than IE. The proper way to
acheive this is to use a META element inside the HEAD section:

<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
<!--


You don't need to enclose script content with SGML comments. The archaic
reasons for doing so are no longer valid. Also, by convention, MIME types
are written in lowercase, but there shouldn't be any difference (the type
attribute is case insensitive).

Mike

In future, please don't place anything other than your signature, below
the signature marker ("-- ").

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Lee
Andy B said:

Hi,
I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other browser
(Opera, Netscape, Mozilla). It has no form defined yet it uses form buttons.
Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
--
To contact me, please use andrewbroomhead {AT} yahoo {DOT} co {DOT} uk


This is USENET.
To contact you, we respond to your post in the newsgroup.

If you're not seeing error messages in Netscape, then you're not looking
in the right place.

In Netscape 7, use the pulldown menu item:

Tools > Web Development > JavaScript Console

You should see a message complaining that startButton is not defined.
Most browsers don't let you get away with referring to elements
simply by their names, without any context information.

Also note that your method of choosing random numbers doesn't really
work the way you (and possibly your instructor) think it does:
ind=Math.round(8*Math.random());
chooses 0 and 8 half as often as any of the other values.

Jul 20 '05 #3

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Tue, 10 Feb 2004 18:04:34 -0000, Andy B <No Spam Thanks> wrote:
I am very new to JavaScript and my first assignment is to create a simple game. Below is my code which works fine in IE, but not in any other
browser (Opera, Netscape, Mozilla). It has no form defined yet it uses
form buttons.
Like I said, it works fine in IE but when loaded into another browser and the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
I'll quote sections of your code, then comment on any issues I see. Some
of the comments will be general guidelines only, and won't actually solve
your problem. However, they should still be given some consideration.
<A border='0' href="javascript:Clicked('1')"><img border='0'
name="image" src="images/1.gif"></A>


1) Don't use the JavaScript pseudo-protocol (javascript:) without a good
reason. You can do the above with either of:

<a href="" onclick="Clicked('1'); return false;"><img

src="images/1.gif" alt="short description"></a>

or:

<img src="images/1.gif" alt="short description" onclick="Clicked('1')">

Note that the former should be styled with CSS, and the latter won't work
with NN4.

2) You pass the string, '1', in the call above, but you check against a
number in the function:

function Clicked(NumClicked) {
...
if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);

Choose one or the other.

if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);


You use 'image' above as a global variable. However, it is not - it is the
name of several HTML elements. This is where the main issue lies. You can
access the images through the document.images collection. This can be done
either by index (first image in the entire document is 0), or by name. If
you use the latter, you'll have to give each image a unique name.
<input type='button' type='button' name='startButton' value='Start
Game!' OnMouseDown='javascript:Shuffle()'>


You probably want to use the onclick intrinsic event, not onmousedown.
Furthermore, prepending "javascript:" to an intrinsic event does not
specify the language in any browser other than IE. The proper way to
acheive this is to use a META element inside the HEAD section:

<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
<!--


You don't need to enclose script content with SGML comments. The archaic
reasons for doing so are no longer valid. Also, by convention, MIME types
are written in lowercase, but there shouldn't be any difference (the type
attribute is case insensitive).

Mike

In future, please don't place anything other than your signature, below
the signature marker ("-- ").

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)


Cheers guys, fixed it now and everything is ok.
Thanks very much for the help.
Andy
Jul 20 '05 #4
"Andy B" <No Spam Thanks> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
Hi,
I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other browser (Opera, Netscape, Mozilla). It has no form defined yet it uses form buttons. Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.


Try this:

<html>
<head>
<title>Andy_B.htm</title>
<script type="text/JavaScript">
<!--
var path = location.href.substr(0,location.href.lastIndexOf("/")+1)
var i;
var j;
var ind;
var AccClick=0;
var PlayingGame=0;
var GamePics = new Array(9);
GamePics[0] = new Image();
GamePics[0].src = path + "images/1.gif";
GamePics[1] = new Image();
GamePics[1].src = path + "images/2.gif";
GamePics[2] = new Image();
GamePics[2].src = path + "images/3.gif";
GamePics[3] = new Image();
GamePics[3].src = path + "images/4.gif";
GamePics[4] = new Image();
GamePics[4].src = path + "images/5.gif";
GamePics[5] = new Image();
GamePics[5].src = path + "images/6.gif";
GamePics[6] = new Image();
GamePics[6].src = path + "images/7.gif";
GamePics[7] = new Image();
GamePics[7].src = path + "images/8.gif";
GamePics[8] = new Image();
GamePics[8].src = path + "images/Blank.gif";
function Clicked(NumClicked) {
if (AccClick == 0) {
alert ("Please click on Start Game! to begin");
} else {
if (NumClicked == 1 && document.image1.src == GamePics[8].src)
Swap(0,1);
if (NumClicked == 1 && document.image3.src == GamePics[8].src)
Swap(0,3);
if (NumClicked == 2 && document.image0.src == GamePics[8].src)
Swap(1,0);
if (NumClicked == 2 && document.image2.src == GamePics[8].src)
Swap(1,2);
if (NumClicked == 2 && document.image4.src == GamePics[8].src)
Swap(1,4);
if (NumClicked == 3 && document.image1.src == GamePics[8].src)
Swap(2,1);
if (NumClicked == 3 && document.image5.src == GamePics[8].src)
Swap(2,5);
if (NumClicked == 4 && document.image0.src == GamePics[8].src)
Swap(3,0);
if (NumClicked == 4 && document.image4.src == GamePics[8].src)
Swap(3,4);
if (NumClicked == 4 && document.image6.src == GamePics[8].src)
Swap(3,6);
if (NumClicked == 5 && document.image3.src == GamePics[8].src)
Swap(4,3);
if (NumClicked == 5 && document.image1.src == GamePics[8].src)
Swap(4,1);
if (NumClicked == 5 && document.image5.src == GamePics[8].src)
Swap(4,5);
if (NumClicked == 5 && document.image7.src == GamePics[8].src)
Swap(4,7);
if (NumClicked == 6 && document.image2.src == GamePics[8].src)
Swap(5,2);
if (NumClicked == 6 && document.image4.src == GamePics[8].src)
Swap(5,4);
if (NumClicked == 6 && document.image8.src == GamePics[8].src)
Swap(5,8);
if (NumClicked == 7 && document.image7.src == GamePics[8].src)
Swap(6,7);
if (NumClicked == 7 && document.image3.src == GamePics[8].src)
Swap(6,3);
if (NumClicked == 8 && document.image6.src == GamePics[8].src)
Swap(7,6);
if (NumClicked == 8 && document.image4.src == GamePics[8].src)
Swap(7,4);
if (NumClicked == 8 && document.image8.src == GamePics[8].src)
Swap(7,8);
if (NumClicked == 9 && document.image5.src == GamePics[8].src)
Swap(8,5);
if (NumClicked == 9 && document.image7.src == GamePics[8].src)
Swap(8,7);
WinCheck();
}
}
function Swap(Pic1, Pic2) {
var imag1 = eval("document.image" + Pic1);
var imag2 = eval("document.image" + Pic2);
var spare = new Image();
spare.src = imag1.src;
imag1.src = imag2.src;
imag2.src = spare.src;
}
function Timer() {
if (PlayingGame == 1) {
document.forms[0].timeTaken.value++;
setTimeout('Timer()',1000);
}
}
function Shuffle() {
if (AccClick == 1) {
AccClick = 0;
PlayingGame = 0;
document.forms[0].startButton.value = "Start Game!";
document.forms[0].timeTaken.value = 0;
return;
} else {
AccClick = 1;
PlayingGame = 1;
document.forms[0].startButton.value = "Stop Game!";
for (j=0; j<8; j++) {
ind = Math.round(8*Math.random());
Swap(j,ind);
}
document.forms[0].timeTaken.value = 0;
Timer();
}
}
function WinCheck() {
var NoCorrect = 0;
for (i=0; i<8; i++) {
var imag0 = eval("document.image" + i);
if (imag0.src == GamePics[i].src) NoCorrect++;
}
if (NoCorrect == 8) {
document.forms[0].startButton.value = "Start Game!";
PlayingGame = 0;
AccClick = 0;
alert("Congratulations, You have completed the game! It
took"+document.forms[0].timeTaken.value+" seconds!");
document.forms[0].timeTaken.value = 0;
}
}
//-->
</script>
</head>
<body bgcolor="#000000">
<center>
<table class="content">
<tr>
<td class="innerContent">
<table width="300" border="0" cellspacing="1" cellpadding="1"
height="300">
<tr>
<td><A border='0' href="javascript:Clicked(1)"><img border='0'
name="image0" src="images/1.gif"></A></td>
<td><A border='0' href="javascript:Clicked(2)"><img border='0'
name="image1" src="images/2.gif"></A></td>
<td><A border='0' href="javascript:Clicked(3)"><img border='0'
name="image2" src="images/3.gif"></A></td>
</tr>
<tr>
<td><A border='0' href="javascript:Clicked(4)"><img border='0'
name="image3" src="images/4.gif"></A></td>
<td><A border='0' href="javascript:Clicked(5)"><img border='0'
name="image4" src="images/5.gif"></A></td>
<td><A border='0' href="javascript:Clicked(6)"><img border='0'
name="image5" src="images/6.gif"></A></td>
</tr>
<tr>
<td><A border='0' href="javascript:Clicked(7)"><img border='0'
name="image6" src="images/7.gif"></A></td>
<td><A border='0' href="javascript:Clicked(8)"><img border='0'
name="image7" src="images/8.gif"></A></td>
<td><A border='0' href="javascript:Clicked(9)"><img border='0'
name="image8" src="images/Blank.gif"></A></td>
</tr>
</table>
</td>
</tr>
</table>
<form>
<p>
<input type='button' type='button' name='startButton' value='Start
Game!' onclick='Shuffle()'>
<font color="#FFFFFF" size="3" face="Geneva, Arial, Helvetica,
sans-serif">You have taken &nbsp;
<input class='box' type='text' name='timeTaken' readonly size='2'
value='0'> seconds</font><br>
</p>
<p>&nbsp; </p>
<table width="100%" border="0">
<tr>
<td width="72%"><font color="#FFFFFF" size="3" face="Verdana, Arial,
Helvetica, sans-serif">The completed picture looks like this: </font></td>
<td width="28%"><font face="Verdana, Arial, Helvetica,
sans-serif"><img src="Images/Complete.gif" width="200" height="200"
border="2"></font></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>
Jul 20 '05 #5

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:hzaWb.276356$na.437676@attbi_s04...
"Andy B" <No Spam Thanks> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
Hi,
I am very new to JavaScript and my first assignment is to create a simple game. Below is my code which works fine in IE, but not in any other browser
(Opera, Netscape, Mozilla). It has no form defined yet it uses form

buttons.
Like I said, it works fine in IE but when loaded into another browser and the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.


Try this:

<html>
<head>
<title>Andy_B.htm</title>
<script type="text/JavaScript">
<!--
var path = location.href.substr(0,location.href.lastIndexOf("/")+1)
var i;
var j;
var ind;
var AccClick=0;
var PlayingGame=0;
var GamePics = new Array(9);
GamePics[0] = new Image();
GamePics[0].src = path + "images/1.gif";
GamePics[1] = new Image();
GamePics[1].src = path + "images/2.gif";
GamePics[2] = new Image();
GamePics[2].src = path + "images/3.gif";
GamePics[3] = new Image();
GamePics[3].src = path + "images/4.gif";
GamePics[4] = new Image();
GamePics[4].src = path + "images/5.gif";
GamePics[5] = new Image();
GamePics[5].src = path + "images/6.gif";
GamePics[6] = new Image();
GamePics[6].src = path + "images/7.gif";
GamePics[7] = new Image();
GamePics[7].src = path + "images/8.gif";
GamePics[8] = new Image();
GamePics[8].src = path + "images/Blank.gif";
function Clicked(NumClicked) {
if (AccClick == 0) {
alert ("Please click on Start Game! to begin");
} else {
if (NumClicked == 1 && document.image1.src == GamePics[8].src)
Swap(0,1);
if (NumClicked == 1 && document.image3.src == GamePics[8].src)
Swap(0,3);
if (NumClicked == 2 && document.image0.src == GamePics[8].src)
Swap(1,0);
if (NumClicked == 2 && document.image2.src == GamePics[8].src)
Swap(1,2);
if (NumClicked == 2 && document.image4.src == GamePics[8].src)
Swap(1,4);
if (NumClicked == 3 && document.image1.src == GamePics[8].src)
Swap(2,1);
if (NumClicked == 3 && document.image5.src == GamePics[8].src)
Swap(2,5);
if (NumClicked == 4 && document.image0.src == GamePics[8].src)
Swap(3,0);
if (NumClicked == 4 && document.image4.src == GamePics[8].src)
Swap(3,4);
if (NumClicked == 4 && document.image6.src == GamePics[8].src)
Swap(3,6);
if (NumClicked == 5 && document.image3.src == GamePics[8].src)
Swap(4,3);
if (NumClicked == 5 && document.image1.src == GamePics[8].src)
Swap(4,1);
if (NumClicked == 5 && document.image5.src == GamePics[8].src)
Swap(4,5);
if (NumClicked == 5 && document.image7.src == GamePics[8].src)
Swap(4,7);
if (NumClicked == 6 && document.image2.src == GamePics[8].src)
Swap(5,2);
if (NumClicked == 6 && document.image4.src == GamePics[8].src)
Swap(5,4);
if (NumClicked == 6 && document.image8.src == GamePics[8].src)
Swap(5,8);
if (NumClicked == 7 && document.image7.src == GamePics[8].src)
Swap(6,7);
if (NumClicked == 7 && document.image3.src == GamePics[8].src)
Swap(6,3);
if (NumClicked == 8 && document.image6.src == GamePics[8].src)
Swap(7,6);
if (NumClicked == 8 && document.image4.src == GamePics[8].src)
Swap(7,4);
if (NumClicked == 8 && document.image8.src == GamePics[8].src)
Swap(7,8);
if (NumClicked == 9 && document.image5.src == GamePics[8].src)
Swap(8,5);
if (NumClicked == 9 && document.image7.src == GamePics[8].src)
Swap(8,7);
WinCheck();
}
}
function Swap(Pic1, Pic2) {
var imag1 = eval("document.image" + Pic1);
var imag2 = eval("document.image" + Pic2);
var spare = new Image();
spare.src = imag1.src;
imag1.src = imag2.src;
imag2.src = spare.src;
}
function Timer() {
if (PlayingGame == 1) {
document.forms[0].timeTaken.value++;
setTimeout('Timer()',1000);
}
}
function Shuffle() {
if (AccClick == 1) {
AccClick = 0;
PlayingGame = 0;
document.forms[0].startButton.value = "Start Game!";
document.forms[0].timeTaken.value = 0;
return;
} else {
AccClick = 1;
PlayingGame = 1;
document.forms[0].startButton.value = "Stop Game!";
for (j=0; j<8; j++) {
ind = Math.round(8*Math.random());
Swap(j,ind);
}
document.forms[0].timeTaken.value = 0;
Timer();
}
}
function WinCheck() {
var NoCorrect = 0;
for (i=0; i<8; i++) {
var imag0 = eval("document.image" + i);
if (imag0.src == GamePics[i].src) NoCorrect++;
}
if (NoCorrect == 8) {
document.forms[0].startButton.value = "Start Game!";
PlayingGame = 0;
AccClick = 0;
alert("Congratulations, You have completed the game! It
took"+document.forms[0].timeTaken.value+" seconds!");
document.forms[0].timeTaken.value = 0;
}
}
//-->
</script>
</head>
<body bgcolor="#000000">
<center>
<table class="content">
<tr>
<td class="innerContent">
<table width="300" border="0" cellspacing="1" cellpadding="1"
height="300">
<tr>
<td><A border='0' href="javascript:Clicked(1)"><img border='0'
name="image0" src="images/1.gif"></A></td>
<td><A border='0' href="javascript:Clicked(2)"><img border='0'
name="image1" src="images/2.gif"></A></td>
<td><A border='0' href="javascript:Clicked(3)"><img border='0'
name="image2" src="images/3.gif"></A></td>
</tr>
<tr>
<td><A border='0' href="javascript:Clicked(4)"><img border='0'
name="image3" src="images/4.gif"></A></td>
<td><A border='0' href="javascript:Clicked(5)"><img border='0'
name="image4" src="images/5.gif"></A></td>
<td><A border='0' href="javascript:Clicked(6)"><img border='0'
name="image5" src="images/6.gif"></A></td>
</tr>
<tr>
<td><A border='0' href="javascript:Clicked(7)"><img border='0'
name="image6" src="images/7.gif"></A></td>
<td><A border='0' href="javascript:Clicked(8)"><img border='0'
name="image7" src="images/8.gif"></A></td>
<td><A border='0' href="javascript:Clicked(9)"><img border='0'
name="image8" src="images/Blank.gif"></A></td>
</tr>
</table>
</td>
</tr>
</table>
<form>
<p>
<input type='button' type='button' name='startButton' value='Start
Game!' onclick='Shuffle()'>
<font color="#FFFFFF" size="3" face="Geneva, Arial, Helvetica,
sans-serif">You have taken &nbsp;
<input class='box' type='text' name='timeTaken' readonly size='2'
value='0'> seconds</font><br>
</p>
<p>&nbsp; </p>
<table width="100%" border="0">
<tr>
<td width="72%"><font color="#FFFFFF" size="3" face="Verdana,

Arial, Helvetica, sans-serif">The completed picture looks like this: </font></td>
<td width="28%"><font face="Verdana, Arial, Helvetica,
sans-serif"><img src="Images/Complete.gif" width="200" height="200"
border="2"></font></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>


Yep, that's what I tried. It was the document.image and document.forms[0]
that I was missing. Thanks to everyone for taking the time to help, it is
really appreciated
Thanks again
Andy
Jul 20 '05 #6
Also note that your method of choosing random numbers doesn't really
work the way you (and possibly your instructor) think it does:
ind=Math.round(8*Math.random());
chooses 0 and 8 half as often as any of the other values.


Ah ha! When I first looked at it, I couldnt figure out what you
meant... Then I tried it, and sure enough, you are right.

I understand what the problem is now... only 0 -> 0.049 will evaluate to
0, where 0.05 -> 0.149 will evalutate to 1, and 7.5->7.99 will evalutate
to 8, giving 9 possible values (not 8), where 0 and 8 split the likelyess.

For the OP, here is a more even distribution for your random function:

Math.floor(Math.random() * 8)

This will give values between 0 and 7 (8 values). If you need between 0
and 8 (9 values), change the 8 to a 9.

Brian


Jul 20 '05 #7
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:hzaWb.276356$na.437676@attbi_s04...
<snip>
function Swap(Pic1, Pic2) {
var imag1 = eval("document.image" + Pic1);
var imag2 = eval("document.image" + Pic2);

<snip>

It is *never* necessary (or desirable) to construct a dot notation
property accessor as a string and then use - eval - to resolve it. It is
always possible to use a bracket notation property accessor instead, and
doing so is considerably more efficient and reliable in more contexts
(such as environments implementing the ECMAScript compact profile (ECMA
327)). In this case:-

var imag1 = document["image" + Pic1];
var imag2 = document["image" + Pic2];

- are directly equivalent property accessors to the constructed dot
notation versions and as they use a standard language syntax there is no
need to mess around with - eval - to use them.

<URL: http://jibbering.com/faq/#FAQ4_39 >

There are almost no circumstances under with the use of - eval - are
appropriate and generally its use is indicative of a failure to
recognise a much better alternative.

<URL: http://jibbering.com/faq/#FAQ4_40 >

Richard.
Jul 20 '05 #8
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:c0*******************@news.demon.co.uk...
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:hzaWb.276356$na.437676@attbi_s04...
<snip>
function Swap(Pic1, Pic2) {
var imag1 = eval("document.image" + Pic1);
var imag2 = eval("document.image" + Pic2);

<snip>

It is *never* necessary (or desirable) to construct a dot notation
property accessor as a string and then use - eval - to resolve it. It is
always possible to use a bracket notation property accessor instead, and
doing so is considerably more efficient and reliable in more contexts
(such as environments implementing the ECMAScript compact profile (ECMA
327)). In this case:-

var imag1 = document["image" + Pic1];
var imag2 = document["image" + Pic2];

- are directly equivalent property accessors to the constructed dot
notation versions and as they use a standard language syntax there is no
need to mess around with - eval - to use them.

<URL: http://jibbering.com/faq/#FAQ4_39 >

There are almost no circumstances under with the use of - eval - are
appropriate and generally its use is indicative of a failure to
recognise a much better alternative.

<URL: http://jibbering.com/faq/#FAQ4_40 >

Richard.

I knew I was going to catch flack for using eval().

Since you've shown how to use the preferred alternative, I'll sin no more.
Jul 20 '05 #9
JRS: In article <40***********************@lovejoy.zen.co.uk>, seen in
news:comp.lang.javascript, Andy B <No@Spam.Thanks> posted at Tue, 10 Feb
2004 18:04:34 :-

if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);


and lots more similar. Anything that repetitive should be done with a
function or array.
function Xx(a, b, c, d, e) { // done once
if (NumClicked == a && image[b].src == GamePics[c].src) Swap(d,e) }

Xx(1,1,8,0,1)
...

or

var D = [
[1,1,8,0,1],
...,
[...]]

for (j=0;j<d.length;j++) { T = D[j]
if (NumClicked == T[0] && image[T[1]].src == GamePics[T[2]].src)
Swap(T[3],T[4]) }
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #10
Andy B wrote:

[snip huge quote]

Welcome, but consider trimming your quotes. Especially when you say
"thank you" that is often the first thing people like to see ;-) don't
hide it below the quote.

--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Pjotr Wedersteers | last post by:
Hello, I tried to create an array with 1000 cells, keys 0 thru 999 using $myarr = array (1000); But this leads to an array of 1 cell with...
7
by: Harry Pehkonen | last post by:
I have been defining new class methods when I'm trying to simplify some code. But I'm thinking I should just define functions within that method...
0
by: Brian van den Broek | last post by:
Hi all, IDLE refuses to launch, and I believe it is because I attempted to define a custom key-binding that it doesn't like. I was recently...
0
by: Dotnetified | last post by:
Reposting after about 2 weeks of no response ... thanks if you can help... ...
3
by: Christofer Dutz | last post by:
Hi, I am working on a practical university project and its our job to define a xml-based language for describing web based user interfaces. One...
1
by: Brian | last post by:
In my query, one criteria field contains !!, which will isolate all assets in my database that contain PurchaseOrderID's identical to the currently...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident....
7
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. ...
4
by: SpreadTooThin | last post by:
I'm running Visual Basic Express Editon 2008. I have a project that references an OLE object called MyObject That object fires events. How do I...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.