root/hodgestar/PythonCode/DmTools/windowroll.py

Revision 206, 2.6 kB (checked in by simon, 4 years ago)

Die roller for The Window.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3import random
4import sys
5
6def roll_d10():
7    total, die = 0, 10
8    while die == 10:
9        die = random.randint(1,10)
10        total += die
11    return total
12
13def roll_thewindow(die,add=0):
14    assert(die >= 2)
15    dice = []
16
17    result = random.randint(1,die)
18    dice.append(result)
19       
20    if result == 1:
21        crit_die = random.randint(1,30)
22        dice.append("d30: %d" % crit_die)
23        if crit_die <= die:
24            dice.append("crit")
25    elif result == die:
26        botch_die = random.randint(1,30)
27        dice.append("d30: %d" % botch_die)
28        if botch_die <= die:
29            dice.append("botch")
30
31    return result + add, dice
32
33def to_int(s,errmsg):
34    try:
35        return int(s)
36    except Exception, e:
37        raise ValueError(errmsg)
38
39def parse_roll(s):
40    a = s.split(':')
41    if len(a) == 1:
42        rolls = 1
43        s = a[0]
44    elif len(a) == 2:
45        rolls = to_int(a[0],"Number of rolls to make should be an integer.")
46        s = a[1]
47    else:
48        raise ValueError("Unrecognised roll format.")
49
50    a = s.split('+')
51    sign = 1
52    if len(a) == 1:
53        a = s.split('-')
54        sign = -1
55   
56    if len(a) == 1:
57        add = 0
58    elif len(a) == 2:
59        add = sign * to_int(a[1],"Amount to add to or subtract from roll should be an integer.")
60    else:
61        raise ValueError("Unrecognised roll format.")
62
63    die = a[0]
64    if not die.startswith('d'):
65        raise ValueError("Unrecognised roll format.")
66    die = to_int(die[1:],"Die should be an integer.")
67
68    return die, add, rolls
69
70def ask_and_roll(fLog):
71    try:
72        sIn = raw_input(">").lower()
73    except BaseException, e:
74        print
75        return False
76    if sIn == 'q':
77        return False
78    else:
79        try:
80            if fLog:
81                fLog.write(">%s\n" % sIn)
82
83            die, add, rolls = parse_roll(sIn)
84
85            for i in range(rolls):
86                total, dice = roll_thewindow(die,add)
87                print "%i: %s" % (total, ", ".join([str(x) for x in dice]))
88
89            if fLog:
90                fLog.write("%i: %s\n" % (total, ", ".join([str(x) for x in dice])))
91        except Exception, e:
92            print e
93            print "Enter rolls as W:dX+Y."
94            if fLog:
95                fLog.write("%s\n" % e)
96        return True
97
98if __name__ == "__main__":
99    if len(sys.argv) == 2:
100        fLog = file(sys.argv[1],"a")
101    else:
102        fLog = None
103
104    print "Enter rolls as W:dX+Y (W rolls of dX+Y; W: and +Y optional; can also use -Y)"
105    print "Q or q to quit."
106    while ask_and_roll(fLog):
107        pass
108
109    if fLog:
110        fLog.close()
Note: See TracBrowser for help on using the browser.