Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Dice game

  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Dice game

    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:

    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?
    Last edited by Bubbel; 11-11-2010 at 10:17 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You need to use square bracket notation.

    Code:
    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.
    Last edited by Philip M; 11-10-2010 at 12:53 PM.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,519
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    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;
      }
     }
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts
    I'll bet 100 bucks he will hand this assignment in with the swedish comments...

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by connollyc4 View Post
    I'll bet 100 bucks he will hand this assignment in with the swedish comments...
    Yes, because I guess he is Swedish!

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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Ummm...Vic:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    There are only 3 dice: dice1, dice2 and dice3
    And the 2nd parameter 'dice' tells which die should be set:1, 2 or 3.

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

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    New Coder
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Quote Originally Posted by glenngv View Post
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •