...

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



zanetar
02-08-2004, 02:37 AM
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>

zanetar
02-08-2004, 02:40 AM
Or if you can't figure it out, recommend a better way to do it... This is the best way I could think of...

Nightfire
02-08-2004, 03:01 AM
Maybe it's the missing semi-colon on this line?

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

zanetar
02-08-2004, 03:12 AM
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.

beetle
02-08-2004, 04:35 AM
There were alot of things wrong with your code. Try this


<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>

zanetar
02-08-2004, 06:43 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum