A dice parser
#3
I stripped this little tidbit out of my Runequest character generator. Change the R$ to whatever quantity, type of dice, and modifier that you need.

Code: (Select All)
RANDOMIZE TIMER
R$ = "2d6"
Build_Dice R$, a%
PRINT a%
END

FUNCTION DiceRoll% (quan AS INTEGER, dice AS INTEGER, plus AS INTEGER)
    'Rolls any number of dice of any number of sides and adds modifiers
    'syntax usage: DiceRoll% (number of dice rolled, number of sides, any modifier)
    DIM t%, x%
    t% = plus '                                                 add modifier
    FOR x% = 1 TO quan '                                        roll die <quan>tity of times
        t% = t% + INT(RND * dice) + 1 '                        total up results
    NEXT x%
    DiceRoll% = t%
END FUNCTION 'DiceRoll%

SUB Build_Dice (roll AS STRING, result AS INTEGER)
    'Parse a dice roll string and roll it, return in result
    roll = UCASE$(roll)
    dpos% = INSTR(roll, "D")
    qn% = -VAL(MID$(roll, 1, dpos% - 1)) * (dpos% > 1) - (dpos% = 1)
    p% = INSTR(roll, "+")
    n% = INSTR(roll, "-")
    IF p% <> 0 THEN md% = VAL(MID$(roll, p%)): dc% = VAL(MID$(roll, dpos% + 1, p% - dpos% + 1))
    IF n% <> 0 THEN md% = VAL(MID$(roll, n%)): dc% = VAL(MID$(roll, dpos% + 1, n% - dpos% + 1))
    IF p% = 0 AND n% = 0 THEN md% = 0: dc% = VAL(MID$(roll, dpos% + 1))
    result = DiceRoll%(qn%, dc%, md%)
END SUB 'Build_Dice
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply


Messages In This Thread
A dice parser - by James D Jarvis - 07-18-2023, 09:29 PM
RE: A dice parser - by SMcNeill - 07-18-2023, 10:16 PM
RE: A dice parser - by OldMoses - 07-18-2023, 11:25 PM



Users browsing this thread: 4 Guest(s)