| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import random |
|---|
| 4 | import sys |
|---|
| 5 | |
|---|
| 6 | try: |
|---|
| 7 | # use readline if we can for nice line navigation |
|---|
| 8 | # with raw_input |
|---|
| 9 | import readline |
|---|
| 10 | except ImportError: |
|---|
| 11 | pass |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | def roll_d10(): |
|---|
| 15 | total, die = 0, 10 |
|---|
| 16 | while die == 10: |
|---|
| 17 | die = random.randint(1,10) |
|---|
| 18 | total += die |
|---|
| 19 | return total |
|---|
| 20 | |
|---|
| 21 | def roll_l5r(roll,keep,add=0): |
|---|
| 22 | dice = [roll_d10() for i in range(roll)] |
|---|
| 23 | dice.sort() |
|---|
| 24 | dice.reverse() |
|---|
| 25 | kept = dice[:keep] |
|---|
| 26 | return sum(kept) + add, dice |
|---|
| 27 | |
|---|
| 28 | def to_int(s,errmsg): |
|---|
| 29 | try: |
|---|
| 30 | return int(s) |
|---|
| 31 | except Exception, e: |
|---|
| 32 | raise ValueError(errmsg) |
|---|
| 33 | |
|---|
| 34 | def parse_roll(s): |
|---|
| 35 | a = s.split(':') |
|---|
| 36 | if len(a) == 1: |
|---|
| 37 | rolls = 1 |
|---|
| 38 | s = a[0] |
|---|
| 39 | elif len(a) == 2: |
|---|
| 40 | rolls = to_int(a[0],"Number of rolls to make should be an integer.") |
|---|
| 41 | s = a[1] |
|---|
| 42 | else: |
|---|
| 43 | raise ValueError("Unrecognised roll format.") |
|---|
| 44 | |
|---|
| 45 | a = s.split('+') |
|---|
| 46 | if len(a) == 1: |
|---|
| 47 | add = 0 |
|---|
| 48 | elif len(a) == 2: |
|---|
| 49 | add = to_int(a[1],"Amount to add to roll should be an integer.") |
|---|
| 50 | else: |
|---|
| 51 | raise ValueError("Unrecognised roll format.") |
|---|
| 52 | s = a[0] |
|---|
| 53 | |
|---|
| 54 | a = s.split('k') |
|---|
| 55 | if len(a) != 2: |
|---|
| 56 | raise ValueError("Unrecognised roll format.") |
|---|
| 57 | |
|---|
| 58 | roll = to_int(a[0],"Number of dice to roll should be an integer.") |
|---|
| 59 | keep = to_int(a[1],"Number of dice to keep should be an integer.") |
|---|
| 60 | |
|---|
| 61 | return roll, keep, add, rolls |
|---|
| 62 | |
|---|
| 63 | def ask_and_roll(fLog): |
|---|
| 64 | try: |
|---|
| 65 | sIn = raw_input(">").lower() |
|---|
| 66 | except BaseException, e: |
|---|
| 67 | print |
|---|
| 68 | return False |
|---|
| 69 | if sIn == 'q': |
|---|
| 70 | return False |
|---|
| 71 | else: |
|---|
| 72 | try: |
|---|
| 73 | if fLog: |
|---|
| 74 | fLog.write(">%s\n" % sIn) |
|---|
| 75 | |
|---|
| 76 | roll, keep, add, rolls = parse_roll(sIn) |
|---|
| 77 | |
|---|
| 78 | for i in range(rolls): |
|---|
| 79 | total, dice = roll_l5r(roll,keep,add) |
|---|
| 80 | print "%i: %s" % (total, dice) |
|---|
| 81 | |
|---|
| 82 | if fLog: |
|---|
| 83 | fLog.write("%i: %s\n" % (total, dice)) |
|---|
| 84 | except Exception, e: |
|---|
| 85 | print e |
|---|
| 86 | print "Enter rolls as W:XkY+Z." |
|---|
| 87 | |
|---|
| 88 | if fLog: |
|---|
| 89 | fLog.write("%s\n" % e) |
|---|
| 90 | |
|---|
| 91 | return True |
|---|
| 92 | |
|---|
| 93 | if __name__ == "__main__": |
|---|
| 94 | if len(sys.argv) == 2: |
|---|
| 95 | fLog = file(sys.argv[1],"a") |
|---|
| 96 | else: |
|---|
| 97 | fLog = None |
|---|
| 98 | |
|---|
| 99 | print "Enter rolls as W:XkY+Z (W rolls of XkY+Z; W: and +Z optional)" |
|---|
| 100 | print "Q or q to quit." |
|---|
| 101 | while ask_and_roll(fLog): |
|---|
| 102 | pass |
|---|
| 103 | |
|---|
| 104 | if fLog: |
|---|
| 105 | fLog.close() |
|---|