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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Location
    Minnesota
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    D&D dice rolling script, not working. Probably something simple I've overlooked.

    In Dungeons and Dragons, dice rolls are set up like this #d#+#. X is the number of times you roll the die (or the number of dice rolled, either way, the same thing), Y is how many sides the die(dice) have, and Z is the optional enhancement. You first add up the total of the rolls of Y, then add Z to that... It may be easier to visualize what I'm trying to do if you run this in your browser. I put an <!-- HTML comment --> on the line I'm getting the error at.

    ... All this because I'm too cheap/lazy to buy real dice. Heh.

    <html>
    <head>
    <title>DnD Die Roller</title>
    <script>
    function roll()
    {
    var textfield1 = window.document.form1.textfield1.value;
    var textfield2 = window.document.form1.textfield2.value;
    var textfield3 = window.document.form1.textfield3.value;
    var result = 0;

    while(textfield1 > 0)
    {
    var roll = Math.round(Math.random())
    textfield1 - 1;
    var result1 = roll * textfield2;
    result = result + result1;
    }

    result = result + textfield3;

    window.document.form1.textfield4.value = result;
    }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <p><b><font face="Arial">Dungeons and Dragons Die Roller</font></b></p>
    <!-- ALWAYS GET ERROR ON THIS LINE. BROKE IT DOWN, ERROR COMING FROM |onSubmit="roll();|. JUST THAT BIT, I PUT IT ON ITS OWN LINE AND TESTED. THAT IS WHERE ERROR COMING FROM. PROBABLY AN ERROR IN THE BODY OF THE SCRIPT ABOVE. --><form name="form1" onSubmit="roll(); returnfalse;">
    <input type="text" name="textfield1" maxlength="6">
    <b><font face="Arial">d
    <input type="text" name="textfield2" maxlength="6">
    +
    <input type="text" name="textfield3" maxlength="6">
    <input name="roll" type="submit" value="Roll"></font></b>
    <b><font face="Arial">Result:</font></b>
    <input type="text" name="textfield4" maxlength="6">
    </form>

    <p></p>
    </body>
    </html>
    Last edited by zanetar; 02-08-2004 at 02:41 AM.

  • #2
    New to the CF scene
    Join Date
    Feb 2004
    Location
    Minnesota
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or if you can't figure it out, recommend a better way to do it... This is the best way I could think of...

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Maybe it's the missing semi-colon on this line?

    var roll = Math.round(Math.random());

  • #4
    New to the CF scene
    Join Date
    Feb 2004
    Location
    Minnesota
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah... I wonder why my browser didn't catch that. It catches other missing semicolons...

    But it shouldn't matter anyways, in JS, from my experience, a new line implies a semicolon.

    I'll try it though.

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There were alot of things wrong with your code. Try this
    Code:
    <html>
    <head>
    <title>DnD Die Roller</title>
    <script>
    function rollIt( f )
    {
    	var total			= 0;
    	var dieSides		= parseInt( f.sides.options[f.sides.selectedIndex].value, 10 );
    	var numberOfDice	= parseInt( f.quantity.value, 10 );
    	var bonus			= parseInt( f.bonus.value, 10 ) || 0;
    
    	for ( var i = 0; i < numberOfDice; i++ )
    	{
    		total += Math.ceil( Math.random() * dieSides );
    	}
    
    	f.total.value = total + bonus;
    }
    
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <p><b><font face="Arial">Dungeons and Dragons Die Roller</font></b></p>
    <form name="form1" onsubmit="rollIt(this);return false;">
    <input type="text" name="quantity" maxlength="6">
    <b><font face="Arial">d 
    <select name="sides">
    	<option value="4">4</option>
    	<option value="6">6</option>
    	<option value="10">10</option>
    	<option value="12">12</option>
    	<option value="20">20</option>
    </select>
    +
    <input type="text" name="bonus" maxlength="6">
    <input name="roll" type="submit" value="Roll"></font></b> 
    <b><font face="Arial">Result:</font></b>
    <input type="text" name="total" maxlength="6">
    </form>
    
    <p></p>
    </body>
    </html>
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    New to the CF scene
    Join Date
    Feb 2004
    Location
    Minnesota
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heh... Yar, I suppose there probably was. Its been over 2 years since I've made anything but a JS alert(). And when you've only been coding for 3 years, 2 years is quite some time. I originally had a script in PHP that worked just fine, but I don't like running apache all the time, so I tried to copy it over to JS, but it just didn't turn out.

    Thanks for the help.


  •  

    Posting Permissions

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