Multi-Sided Dice Roller DSL with modifiers. Turns REPL into easy die roller.
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 37 38 39 | #!/usr/bin/python
print """
pyRepl to DM Helper
Invocation:
python -i d20.py
Usage:
<N>*d(<S>)+<M>
N Number of dice
S Number of sides on die
M Modifier on die roll
Example:
>>> 3*d(6)+12
17
>>> 2*d(6,True)+12 #shows individual rolls
4
2
18
"""
from random import choice
class d(object):
def __init__(self,sides,show_rolls=False):
self.show=show_rolls;
self.sides=sides
def roll(self):
return choice(range(1,self.sides+1))
def accum(self,rolls):
accum=0;
for roll in range(rolls):
roll=self.roll()
if self.show: print roll
accum+=roll
return accum
def __rmul__(self,other):
try:
return self.accum(int(other))
except ValueError:
print "Only multiply by an INTEGER"
|
I wanted to use the python repl to roll dice easily for me. Typing is faster than mousing everything. I had an inspiration to use operator overloading to make this little DSL. Useful for me. Thought I might share.
Tags: oop