/    Sign up×
Community /Pin to ProfileBookmark

Java Dice Roller

Hello. This is the first time I’ve posted here, and I know little to nothing about Java, but am hoping someone can nevertheless answer this question. There is a pretty common script for a Java dice roller available, but when rolling multiple dice, it simply adds their results and answers with a single number. For purposes of roleplaying games it is important to view the separate values for each die, and I was wondering if anyone know how this could be done. Thank you very much.

(This is the original code for the dice roller. If someone could explain to me how, or better yet, alter this code to show the individual results, I would greatly appreciate it.):

<HEAD>

<SCRIPT LANGUAGE=”JavaScript”>
var die = 6;
var dice = 3;
function dice_roll(die, dice) {
var roll = 0;
for (loop=0; loop < dice; loop++) {
//roll = roll + Math.round(Math.random() * die) % die + 1;
}
document.form.text.value = roll;
}
// End –>
</script>

<BODY>

<center>
<form name=form>
<table border=2 cellpadding=5>
<tr>
<td colspan=2 align=middle>What dice type?</td>
<td colspan=2 align=middle>How many dice to roll?</td>
</tr>
<tr>
<td valign=top align=middle>
<p><input type=radio name=sides onclick=”die = 3″>3 Sided
<p><input type=radio name=sides onclick=”die = 4″>4 Sided
<p><input type=radio name=sides onclick=”die = 5″>5 Sided
<p><input type=radio checked name=sides onclick=”die = 6″>6 Sided
<p><input type=radio name=sides onclick=”die = 8″>8 Sided
</td>
<td valign=top align=middle>
<p><input type=radio name=sides onclick=”die = 10″>10 Sided
<p><input type=radio name=sides onclick=”die = 12″>12 Sided
<p><input type=radio name=sides onclick=”die = 20″>20 Sided
<p><input type=radio name=sides onclick=”die = 30″>30 Sided
<p><input type=radio name=sides onclick=”die = 100″>100 Sided
</td>
<td valign=top align=middle>
<p><input type=radio name=number onclick=”dice = 1″>1
<p><input type=radio name=number onclick=”dice = 2″>2
<p><input type=radio checked name=number onclick=”dice = 3″>3
<p><input type=radio name=number onclick=”dice = 4″>4
<p><input type=radio name=number onclick=”dice = 5″>5
</td>
<td valign=top align=middle>
<p><input type=radio name=number onclick=”dice = 6″>6
<p><input type=radio name=number onclick=”dice = 7″>7
<p><input type=radio name=number onclick=”dice = 8″>8
<p><input type=radio name=number onclick=”dice = 9″>9
<p><input type=radio name=number onclick=”dice = 10″>10
</td>
</tr>
<tr>
<td align=middle colspan=4>
<input type=button value=”Roll Dice” name=button onclick=”dice_roll(die, dice)”>
<input type=text size=10 name=text>
</td>
</tr>
</table>
</form>
</center>

<p><center>
<font face=”arial, helvetica” size=”-2″>Free JavaScripts provided<br>
by <a href=”http://javascriptsource.com”>The JavaScript Source</a></font>
</center><p>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJun 16.2004 — Hi!

Welcome to the forums.

Example:[code=php]<script language="JavaScript" type="text/javascript">
<!--
var die = 6;
var dice = 3;
function dice_roll(die, dice) {
var txtfld=document.form.text;
txtfld.value ='';
var roll = new Array();
for (loop=0; loop < dice; loop++) {
roll[loop] = 0 + Math.round(Math.random() * die) % die + 1;
txtfld.value += roll[loop]+' - ';
}
txtfld.value = txtfld.value.substring(0,txtfld.value.length-3);
}
//-->
</script>[/code]
Maybe you should make the text box for the result's display a bit larger (e.g.: <input type=text size=25 name=text>).

Cheers - Pit
Copy linkTweet thisAlerts:
@MeriauthorJun 16.2004 — Thanks. That makes room for the results, but the main problem is that I can't get them to display the results for each die separately.

If it rolls 5 10 sided dice, it gives an answer like '36'.

I need to have the result individuated, showing up as '3,8,5,6,5,9'

It seems like this should be something relatively easy to adjust, but I just can't figure it out.
Copy linkTweet thisAlerts:
@PittimannJun 16.2004 — Hi! ?

Seems you did not replace the script in your code with the one I posted. Sorry also, that I used ' - ' instead of commas to separate the values.

I put a working example with the code I previously posted here:

http://www.pit-r.de/scripts/rollDice.htm

Regards - Pit
Copy linkTweet thisAlerts:
@MeriauthorJun 18.2004 — Ah, excellent! Thank you very much, that worked perfectly.
Copy linkTweet thisAlerts:
@PittimannJun 18.2004 — Hi!

You're welcome!

Cheers - Pit
×

Success!

Help @Meri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...