diceroller.py: Implemented argument parsing
This commit is contained in:
parent
364399eb52
commit
34c82f0e52
|
@ -8,9 +8,10 @@
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
# Implements a "dice" - that is, a series of dice plus modifiers
|
# Implements a "dice" - that is, a series of dice plus modifiers
|
||||||
# fixme: implement rerolls (currently they do nothing!)
|
|
||||||
class Dice():
|
class Dice():
|
||||||
def __init__(self, num, sides, mod, drop_low=0, drop_high=0, reroll=0, reroll_times=0):
|
def __init__(self, num, sides, mod, drop_low=0, drop_high=0, reroll=0, reroll_times=0):
|
||||||
self.num = num
|
self.num = num
|
||||||
|
@ -23,7 +24,7 @@ class Dice():
|
||||||
self.times_rolled = 0
|
self.times_rolled = 0
|
||||||
|
|
||||||
# Rolls the dice, prints the result
|
# Rolls the dice, prints the result
|
||||||
def roll(self, verbose=True): # fixme: default verbose to False and add -v flag
|
def roll(self, verbose=False):
|
||||||
results = []
|
results = []
|
||||||
rerolled = []
|
rerolled = []
|
||||||
|
|
||||||
|
@ -61,9 +62,10 @@ class Dice():
|
||||||
# Get the results
|
# Get the results
|
||||||
total = sum(results) + self.mod
|
total = sum(results) + self.mod
|
||||||
|
|
||||||
|
# fixme - verbose mode could use slightly better formatting re: drop_info and reroll_info...
|
||||||
if verbose:
|
if verbose:
|
||||||
drop_info = None
|
drop_info = ''
|
||||||
reroll_info = None
|
reroll_info = ''
|
||||||
|
|
||||||
if dropped:
|
if dropped:
|
||||||
drop_info = '[dropped {}]'.format(','.join(['{}'.format(x) for x in dropped]))
|
drop_info = '[dropped {}]'.format(','.join(['{}'.format(x) for x in dropped]))
|
||||||
|
@ -143,13 +145,44 @@ def parse_input(args):
|
||||||
return dice_list
|
return dice_list
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(description='Roll dice based on descriptions passed in on the command line', formatter_class=argparse.RawTextHelpFormatter)
|
||||||
|
parser.add_argument('--repeat', '-r', metavar='N', type=int, default=1, help='repeat the given rolls N times')
|
||||||
|
parser.add_argument('--verbose', '-v', action='store_true', help='print detailed information about each roll')
|
||||||
|
parser.add_argument('dice', nargs=argparse.REMAINDER, help="""
|
||||||
|
Dice are input in the following form:
|
||||||
|
|
||||||
|
XdY[modifiers]
|
||||||
|
|
||||||
|
This will roll X Y-sided dice and apply the specified modifiers.
|
||||||
|
|
||||||
|
Modifiers can be any of the following (where N and M are integers):
|
||||||
|
|
||||||
|
(+|-)N Add or subtract N from the total
|
||||||
|
lN Drop the lowest-rolling N dice from the total
|
||||||
|
hN Drop the highest-rolling N dice from the total
|
||||||
|
rN[xM] Any dice that roll <= N will be rerolled.
|
||||||
|
If the optional 'xM' option is specified, dice will be rerolled a maximum of M times.
|
||||||
|
Otherwise each die will be rerolled until the result is > N
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
1d20+5 roll 1 twenty-sided die, and add 5 to the result
|
||||||
|
6d6l1h1 roll 6 six-sided dice, and drop both the highest and the lowest roll
|
||||||
|
4d6l1r2x1 roll 4 six-sided dice. Any dice rolling a 1 or 2 will be rerolled once.
|
||||||
|
If the result is still 1 or 2 it is kept. The lowest die is dropped from the result
|
||||||
|
""")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# fixme: command line arguments! get some argparse over here
|
settings = parse_args()
|
||||||
|
dice_list = parse_input(settings.dice)
|
||||||
dice_list = parse_input(sys.argv)
|
|
||||||
|
|
||||||
|
for i in range(settings.repeat):
|
||||||
for dice in dice_list:
|
for dice in dice_list:
|
||||||
dice.roll()
|
dice.roll(verbose=settings.verbose)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user