March 05, 2021, 07:36:37 AM

Author Topic: [bmx] D&D Dice Roller by Chroma [ 1+ years ago ]  (Read 568 times)

Offline BlitzBot

[bmx] D&D Dice Roller by Chroma [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : D&D Dice Roller
Author : Chroma
Posted : 1+ years ago

Description : My old D&D roots flared up while I was messing around with arrays today.  Maybe I'll use this in the future.

Accepts any sided die input.  Use is simple:

Print RollDice("3d6")
Print RollDice("1d6+2")
Print RollDice("1d20-3")

etc etc


Code :
Code: BlitzMax
  1. SeedRnd MilliSecs()
  2.  
  3. Local dice$ = "1d20+3"
  4.  
  5. For i = 1 To 20
  6.         Print "Rolling "+dice+": "++RollDice(dice)
  7. Next
  8.  
  9. End
  10.  
  11. Function RollDice:Int(die$)
  12.         If Left$(die,1) = "d" Then die = "1" + die
  13.         Local roll$[] = die.split("d")
  14.         Local ppos = Instr(roll[1],"+")
  15.         Local mpos = Instr(roll[1],"-")
  16.         Local bonus%, total%
  17.         If ppos > 0
  18.                 bonus = Int(Mid$(roll[1], ppos+1, roll[1].length-ppos))
  19.                 roll[1] = Left(roll[1], ppos-1)
  20.         ElseIf mpos > 0
  21.                 bonus = Int(Mid$(roll[1], mpos, roll[1].length-mpos+1))
  22.                 roll[1] = Left(roll[1], mpos-1)
  23.         EndIf
  24.         For Local i% = 1 To Int(roll[0])
  25.                 total:+ Rand(Int(roll[1]))
  26.         Next
  27.         Return total + bonus
  28. End Function


Comments :


Ryudin(Posted 1+ years ago)

 Very nice! I think all of us D&D guys have done this at some point.


slenkar(Posted 1+ years ago)

 thanks, im always doing games that need dice rolls


slenkar(Posted 1+ years ago)

 is there an easy way to see if the number is within certain values?


*(Posted 1+ years ago)

 If you require a Blitz .bb version go to<a href="codearcsa83f.html?code=2124" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=2124[/url]


Chroma(Posted 1+ years ago)

 Well I guess this would do just as well...
Code: [Select]

Local attackRoll% = RollDice(1,20,3)     '1d20+3

Function RollDice%(rolls%, die%, bonus%)
Local dieTotal%, i%
For i = 1 to rolls
dieTotal :+ Rand(die)
Next
Return dieTotal + bonus
End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal