-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroll_dice.py
36 lines (33 loc) · 1.57 KB
/
roll_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
36
import re
import dice
from argparse import ArgumentParser
parser = ArgumentParser(description='Roll some dice.')
parser.add_argument('dice', help='The dice roll string, e.g. 2d6 or 1d20+2')
parser.add_argument('-H', '--high', action='store_true', help='Sets the dice to be weighted to roll bigger numbers')
parser.add_argument('-L', '--low', action='store_true', help='Sets the dice to be weighted to roll smaller numbers')
parser.add_argument('-F', '--factor', default=2.0, help='Sets the factor of the weighted dice, from 0.0 to 10.0')
args = parser.parse_args()
pattern = re.compile('^(\d+)d(\d+)([-+])?(\d+)?$', re.IGNORECASE)
matches = pattern.match(args.dice)
if matches:
groups = matches.groups()
if args.high:
total = dice.roll_high(int(groups[0]), int(groups[1]), float(args.factor))
fairness = '(The dice are weighted high)'
elif args.low:
total = dice.roll_low(int(groups[0]), int(groups[1]), float(args.factor))
fairness = '(The dice are weighted low)'
else:
total = dice.roll_dice(int(groups[0]), int(groups[1]))
fairness = '(The dice are fair)'
if groups[2] is None:
print('Rolled', total, 'for a total of', sum(total), fairness)
else:
if groups[2] == '-':
modifier = int(groups[3]) * -1
print('Rolled', total, 'minus', groups[3], 'for a total of', sum(total) + modifier, fairness)
else:
modifier = int(groups[3])
print('Rolled', total, 'plus', groups[3], 'for a total of', sum(total) + modifier, fairness)
else:
print('No dice string found.')