Dice Roll Function for RPGers! :D

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
coma8coma1
Veteran
Posts: 100
Joined: Sat Dec 08, 2007 5:29 pm
Location: Maryland, USA

Dice Roll Function for RPGers! :D

Post by coma8coma1 »

Note: This code has been tested and works in QuickBASIC and qb64. The author cannot speak for how it will behave under any other language. If you use this code please consider mentioning me in the credits :lol:

I came up with this wicked original dice roll function for my RPG awhile back. It returns the total for any number of rolls for any number sided die and even lets you do - or + simply by passing familiar strings to it such as "2d6" or "1d100+25"! The syntax is simple: You pass a string to the function that has the number of rolls and the number of sides to the die separated by a lowercase "d" and optionally you can add or subtract from the total with a "-" or "+" followed by another number.

First you need to declare the function near the beginning of your program

Code: Select all

DECLARE FUNCTION Roll% (dice$)
don't forget to seed the random number!

Code: Select all

RANDOMIZE TIMER
then add this new function

Code: Select all

FUNCTION Roll% (dice$)
 totalroll% = 0
 FOR rolln = 1 TO VAL(MID$(dice$, 1, INSTR(dice$, "d") - 1))
  totalroll% = totalroll% + INT(RND * VAL(MID$(dice$, INSTR(dice$, "d") + 1, LEN(dice$)))) + 1
 NEXT rolln
 Roll% = totalroll%
 IF INSTR(dice$, "+") > 0 THEN Roll% = totalroll% + VAL(MID$(dice$, INSTR(dice$, "+") + 1))
 IF INSTR(dice$, "-") > 0 THEN Roll% = totalroll% - VAL(MID$(dice$, INSTR(dice$, "-") + 1))
END FUNCTION
and now you can roll dice and deal damage simply by saying something like

Code: Select all

damage% = Roll%("4d8+5")
monsters.hp = monsters.hp - damage%
PRINT "You dealt "; damage%; " worth of whoopascii!"
now that's a dead monster!

anytime you call the Roll% function with a valid value for string "dice$" it returns a total value rolled. For instance to generate a value between 2 and 12 you might roll "2d6". To generate a value between 14 and 50 you might roll "4d10+10". To generate a 1 or a 0 you could simply ask for "1d2-1". The point is that RPG fans already understand the concept of "one die two minus one" and my function makes this easy and fun to work with.

Always remember that the more dice involved the more statistical the probability that you'll roll a value close to the middle of the range. For instance with only one die being cast you have an equal probability across the entire range, but with 2 dice that probability becomes triangle-shaped. With a third die you have have a fat bell curve and with each successive die added the curve gets more and more narrow. So while "1d12", "2d6" and "3d4" may all seem at first glance to generate similar values, it's important to understand that their statistical probabilities are all very different.

All comments and criticisms welcome. Be sure to write back and tell me what you used my code in. Good luck, and enjoy! -coma8
TWIZTER
Newbie
Posts: 1
Joined: Sat May 29, 2010 10:20 pm

Ummmm...not ot be picky

Post by TWIZTER »

Wrote a code for this on C64 long ago, and that was a compilation from several sources. Please dont presume this is your code. Not trying to be a problem..just saying...no one is reinventing the wheel here.


Truthfully Yours,

Twiz
Post Reply