forked from susanbrindle/ackstools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice.py
More file actions
executable file
·37 lines (31 loc) · 791 Bytes
/
dice.py
File metadata and controls
executable file
·37 lines (31 loc) · 791 Bytes
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
#!/usr/bin/python
import random
random.seed()
def die(n):
return random.randint(1,n)
def dice(n,m):
a = 0
for b in range(n):
a += random.randint(1,m)
return a
def roll(t):
if len(t) == 1:
return die(t[0])
elif len(t) == 2:
return dice(t[0],t[1])
elif len(t) > 2:
return dice(t[0],t[1]) + t[2]
else:
return -1 # consider doing better error condition design
def parse(s):
# takes strings of the form n, ndm, or ndm+k and converts to tuple for
# roll
addl = s.split('+')
dl = addl[0].split('d')
if len(addl) > 1 and len(dl) > 1:
return (int(dl[0]), int(dl[1]), int(addl[1]))
elif len(dl) > 1:
return (int(dl[0]), int(dl[1]))
else:
# given n, return a tuple which yields a constant when rolled
return (1, 1, int(dl[0])-1)