-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.py
35 lines (27 loc) · 1.04 KB
/
dice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import random
import re
def roll(times, sides, bonus, advantage=0):
if advantage == -1:
#print('disadvantage')
return min(roll(times, sides, bonus), roll(times, sides, bonus))
elif advantage == 1:
#print('advantage')
return max(roll(times, sides, bonus), roll(times, sides, bonus))
else:
return sum([random.randint(1, sides) for n in range(0, times)]) + bonus
#def roll_advantage(times, sides, bonus):
# return max(roll(times, sides, bonus), roll(times, sides, bonus))
#def roll_disadvantage(times, sides, bonus):
# return min(roll(times, sides, bonus), roll(times, sides, bonus))
def parse_damage(damage):
if not damage:
return [[0,0,0]]
def parse(damage):
if isinstance(damage,str):
damage = [damage]
for dmg in damage:
if '+' in dmg:
yield [int(i) for i in re.split("d|\+", dmg)]
else:
yield [int(i) for i in dmg.split('d') + [0]]
return list(parse(damage))