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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Yahtzee game in Javascript

    I'am trying to get the dice to show as number such as 1,2,3,4,5,6 in the top 5 boxes instead of the dice and save them into a array so that i can evalated the array of dice later. what am i doing wrong.
    This is what i'am trying to do
    imageshack.us/photo/my-images/202/capturendp.png/
    Code:
    <html>
    
    <head></head>
    
    <body>
    
    <script>
    
    
    function roll()
    {
    var i;
    var j;
    
    for (j=1; j<6;j++) {
    i = Math.floor(Math.random()*6) + 1;  //1-6
    document.getElementById("d"+j).innerHTML = "<img src ='dice"+ i +".gif'/>";
    <img src="dice1.gif" name="1">
    <img src="dice2.gif" name="2">
    <img src="dice3.gif" name="3">
    <img src="dice4.gif" name="4">
    <img src="dice5.gif" name="5">
    <img src="dice6.gif" name="6">
    var faces = new Array(
     "dice1.gif",
     "dice2.gif",
     "dice3.gif",
     "dice4.gif",
     "dice5.gif",
     "dice6.gif",
    );
    
    }
    }
    
     </script>
    
    <b><CENTER>
    <FONT SIZE=7> Yahtzee</FONT>
    </b></CENTER>
    
    
    <table border="1" align="center">
    <tr>
    <td id="d1"> <img src="dice0.gif"/> </td>
    <td id="d2"> <img src="dice0.gif"/> </td>
    <td id="d3"> <img src="dice0.gif"/> </td>
    <td id="d4"> <img src="dice0.gif"/> </td>
    <td id="d5"> <img src="dice0.gif"/> </td>
    </tr>
    
    
    <table border="1" align="center">
    <tr> <td> 1's </td> <td> <input id="box1" type="text"/> </td>
    </tr><tr> <td> 2's </td> <td> <input id="box2" type="text"/> </td><td></td>
    <tr><td> 3's </td> <td> <input id="box3" type="text"/> </td>
    <tr><td> 4's </td> <td> <input id="box4" type="text"/> </td>
    <td><input type="button" value="Roll" onClick="roll()"/></td>
    <tr><td> 5's </td> <td> <input id="box5" type="text"/> </td>
    <tr><td> 6's </td> <td> <input id="box6" type="text"/> </td>
    <tr><td> 3 of a kind </td> <td> <input id="box7" type="text"/> </td>
    <tr><td> 4 of a kind </td> <td> <input id="box8" type="text"/> </td>
    <tr><td> Full house </td> <td> <input id="box9" type="text"/> </td>
    <tr><td> Sm straight </td> <td> <input id="box10" type="text"/> </td>
    <tr><td> Lg straight </td> <td> <input id="box11" type="text"/> </td>
    <td><input type="button" value="New game"/> </td></tr>
    <tr><td> Yahtzee </td> <td> <input id="box12" type="text"/> </td>
    <tr><td> Chance </td> <td> <input id="box13" type="text"/> </td>
    <tr><td> Total </td> <td> <input id="box14" type="text"/> </td>
    </table>
    
    
    
    
    <body>
    <html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You cannot mix Javascript and HTML code inside of a <script> tag. So first of all you'll have to remove the HTML <img> tags from the <script> tag.

    But there are several more issues which make me believe you should start taking some courses first.

    - Two opening <body> tags, no closing </body> tag
    - Two opening <html> tags, no closing </html> tag
    - You define an array "faces" that you never (need to) use
    - <center> and <font> have been deprecated long time ago


  •  

    Posting Permissions

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