color fudge dice rolls

when rolling fudge dice, use + and - instead. + is green, - is red.
This commit is contained in:
Patrick Hurst 2010-08-17 13:54:47 -04:00
parent 32aaf97c34
commit c35f15b981
1 changed files with 20 additions and 8 deletions

View File

@ -50,26 +50,38 @@ def dice(inp):
return "Invalid diceroll" return "Invalid diceroll"
groups = sign_re.findall(spec) groups = sign_re.findall(spec)
total = 0
rolls = [] rolls = []
bias = 0
for roll in groups: for roll in groups:
count, side = split_re.match(roll).groups() count, side = split_re.match(roll).groups()
count = int(count) if count not in " +-" else 1 count = int(count) if count not in " +-" else 1
if side.lower() == "f": # fudge dice are basically 1d3-2 if side.upper() == "F": # fudge dice are basically 1d3-2
rolls += nrolls(count, "F") for fudge in nrolls(count, "F"):
if fudge == 1:
rolls.append("\x033+\x0F")
elif fudge == -1:
rolls.append("\x034-\x0F")
else:
rolls.append("0")
total += fudge
elif side == "": elif side == "":
bias += count total += count
else: else:
side = int(side) side = int(side)
try: try:
if count > 0: if count > 0:
rolls += nrolls(count, side) dice = nrolls(count, side)
rolls += map(str, dice)
total += sum(dice)
else: else:
rolls += [-x for x in nrolls(abs(count), side)] dice = nrolls(-count, side)
rolls += [str(-x) for x in dice]
total -= sum(dice)
except OverflowError: except OverflowError:
return "Thanks for overflowing a float, jerk >:[" return "Thanks for overflowing a float, jerk >:["
if desc: if desc:
return "%s: %d (%s=%s)" % (desc.strip(), sum(rolls)+bias, inp, str(rolls).strip("[]")) return "%s: %d (%s=%s)" % (desc.strip(), total, inp, ", ".join(rolls))
else: else:
return "%d (%s=%s)" % (sum(rolls)+bias, inp, str(rolls).strip("[]")) return "%d (%s=%s)" % (total, inp, ", ".join(rolls))