...

View Full Version : Dice game



Bubbel
11-10-2010, 01:01 PM
Hi there!

I need some help developing a dice game. I'm totally new at this so a bit confused with the javascript language... Anyways, heres my code:


/.../

<script type="text/javascript" language="javascript">

var diceValue1 = 1;
var diceValue2 = 1;
var diceValue3 = 1;

function showDice(imgNr, dice) {

for (var i=0; i<=5; i++)
{

if (imgNr == 1) var imgUrl = "1.gif";
else if (imgNr == 2) var imgUrl = "2.gif";
else if (imgNr == 3) var imgUrl = "3.gif";
else if (imgNr == 4) var imgUrl = "4.gif";
else if (imgNr == 5) var imgUrl = "5.gif";
else if (imgNr == 6) var imgUrl = "6.gif";

document.getElementById("dice" +i).src = imgUrl;
}
}

function randomInteger(high) {
var randNumber = Math.random();
var randNumber = Math.floor(high*randNumber)+1;
return randNumber;
}

function showRandomPict(dice) {
var imgNr = randomInteger(6);
showDice(imgNr, dice);
return imgNr;
}

function rollAll(pictNr) {
diceValue1 = showRandomPict(1);
diceValue2 = showRandomPict(2);
diceValue3 = showRandomPict(3);
}

function changePict(bigDice) {
if (bigDice) document.bigD.src = "tarning.jpg";
else document.bigD.src = "tarning1.jpg";
}

</script>
</head>

<body>

<h3>Tärningskastning</h3>

<div id="pics">
<p>
<img id="dice1" src="1.gif" alt="tarning1" width=60px onclick="showRandomPict(1)" />
<img id="dice2" src="2.gif" alt="tarning2" width=60px onclick="showRandomPict(2)" />
<img id="dice3" src="3.gif" alt="tarning3" width=60px onclick="showRandomPict(3)" />
</p>

</div>

<div id= "BigDice"
<p><img name="bigD" src="tarning.jpg" alt="stor tärning" width=80px onmouseover="changePict(false)
"onmouseout="changePict(true)" onclick="rollAll()" /><br /></p>
</div>
</body>
</html>


So what i need help with is my pictures. They wont show. Firebug says: "document.getElementById("dice" + i) is null", and I have tried to solve that but with no progress...

So what should i do??

Next step is to show the sums of the dices on the screen and also save this sums to be able to show them later on in an alert window... But that is for later, right now the dices needs to show the right (random) sides...

Help anyone?

Philip M
11-10-2010, 01:46 PM
You need to use square bracket notation.


document.getElementById["dice" + i].src = imgUrl;

You might guess that this student assignment has been raised before.
Did you try to use the forum search feature for "dice"? If so, you would have found
http://www.codingforums.com/showthread.php?t=194027

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar which will insert the tags. You can (and should) edit your previous post.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

vwphillips
11-10-2010, 02:37 PM
function showDice(imgNr, dice) {
var imgUrl;
if (imgNr == 1) imgUrl = "1.gif";
else if (imgNr == 2){
imgUrl = "2.gif";
}
else if (imgNr == 3){
imgUrl = "3.gif";
}
else if (imgNr == 4){
imgUrl = "4.gif";
}
else if (imgNr == 5){
imgUrl = "5.gif";
}
else if (imgNr == 6){
imgUrl = "6.gif";
}
for (var i=1; i<=5; i++){
if (document.getElementById("dice" +i)){
document.getElementById("dice" +i).src = imgUrl;
}
}
}

connollyc4
11-10-2010, 02:42 PM
I'll bet 100 bucks he will hand this assignment in with the swedish comments... :D

Philip M
11-10-2010, 04:25 PM
I'll bet 100 bucks he will hand this assignment in with the swedish comments... :D

Yes, because I guess he is Swedish!

src="tarning.jpg" alt="stor tärning" :)

Old Pedant
11-10-2010, 10:36 PM
Ummm...Vic:


function showDice(imgNr, dice) {
for (var i=1; i<=5; i++){
document.getElementById("dice" +i).src = imgNr + ".gif"
}
}

Though for the life of me, I can't understand why he'd want to give all 5 dice the same image.

glenngv
11-11-2010, 10:32 AM
There are only 3 dice: dice1, dice2 and dice3
And the 2nd parameter 'dice' tells which die should be set:1, 2 or 3.


function showDice(imgNr, dice) {
document.getElementById("dice" + dice).src = imgNr + ".gif"
}

Bubbel
11-11-2010, 11:11 AM
First of all, Thanks for all the help! glenngvs help did fix the problem =) Thanks!

Second, yes this is a Swedish assignment but im not a he :P

glenngv
11-11-2010, 07:29 PM
Glad to be of help Bubbel. :)

There's nothing wrong if this is an assignment as Bubbel was clearly not asking us to do all the work for her. She was asking for advice/help on a specific problem she's having trouble with. And this is within the bounds of the forum rules on posting assignments.

Old Pedant
11-11-2010, 08:16 PM
There are only 3 dice: dice1, dice2 and dice3
And the 2nd parameter 'dice' tells which die should be set:1, 2 or 3.


Sorry...I hadn't read the original code; was only commenting on Vic's way over-complicated answer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum