Last active
          May 6, 2019 16:14 
        
      - 
      
- 
        Save jsocol/f86f35accdcc3e66d88f657679c55fc4 to your computer and use it in GitHub Desktop. 
Revisions
- 
        jsocol revised this gist Jan 17, 2019 . 1 changed file with 25 additions and 19 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,36 +5,43 @@ class D(): """A die. Simulates an n-sided die, e.g. to create a d6, pass 6: >>> d4, d6, d8, d10, d12, d20 = D(4), D(6), D(8), D(10), D(12), D(20) To roll, you can call d6.roll() (or just d6()): >>> d6.roll() <<< 4 >>> d6() <<< 2 Or even just: >>> d6 <<< 3 (NB: I'm sure I owe someone an apology for that.) Need to roll with (dis) advantage? >>> d20, d20 <<< (20, 13) But the best way to use these is to do math with dice. For example, if you need to roll 3d8 + 4: >>> 3 * d8 + 4 <<< 22 Or add dice: >>> d8 + d4 <<< 9 Remember these are random rolls so the examples above are not guaranteed. """ def __init__(self, sides): @@ -43,7 +50,7 @@ def __init__(self, sides): def roll(self): """Roll the dice!""" return random.randint(1, self.sides) def __call__(self): return self.roll() @@ -58,9 +65,8 @@ def __add__(self, n): if isinstance(n, D): n = n.roll() return n + self.roll() __radd__ = __add__ def __repr__(self): return str(self.roll()) 
- 
        jsocol revised this gist Jan 16, 2019 . 1 changed file with 13 additions and 5 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,8 +8,7 @@ class D(): Simulates an n-sided die, e.g. to create a d6, pass 6: >>> d4, d6, d8, d10, d12, d20 = D(4), D(6), D(8), D(10), D(12), D(20) To roll, you can call d6.roll() (or just d6()): @@ -18,8 +17,13 @@ class D(): >>> d6() <<< 2 Or, well, just (in a repl): >>> d6 <<< 4 But the best way to use these is to do math with dice. For example, if you need to roll 3d8 + 4: >>> 3 * d8 + 4 <<< 22 @@ -55,4 +59,8 @@ def __add__(self, n): n = n.roll() return n + self.roll() __radd__ = __add__ def __repr__(self): """Oh I am so sorry.""" return str(self.roll()) 
- 
        jsocol revised this gist Jan 16, 2019 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,6 +39,9 @@ def __init__(self, sides): def roll(self): """Roll the dice!""" return random.randint(1, self.sides) def __call__(self): return self.roll() def __mul__(self, n): """Roll n (an integer) number of dice.""" 
- 
        jsocol created this gist Jan 16, 2019 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ import functools import operator import random class D(): """A die. Simulates an n-sided die, e.g. to create a d6, pass 6: >>> d6 = D(6) >>> d8 = D(8) To roll, you can call d6.roll() (or just d6()): >>> d6.roll() <<< 4 >>> d6() <<< 2 But the best way to use these is to do math with dice. For example, if you need to roll 3d8 + 4: >>> 3 * d8 + 4 <<< 22 Or add dice: >>> d8 + d4 <<< 9 The die uses random.randint so the examples above are not guaranteed. """ def __init__(self, sides): self.sides = sides def roll(self): """Roll the dice!""" return random.randint(1, self.sides) def __mul__(self, n): """Roll n (an integer) number of dice.""" return functools.reduce(operator.add, [self.roll() for _ in range(n)]) __rmul__ = __mul__ def __add__(self, n): """Add an integer or another die.""" if isinstance(n, D): n = n.roll() return n + self.roll() __radd__ = __add__