-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.py
39 lines (29 loc) · 795 Bytes
/
dice.py
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
38
39
from dataclasses import dataclass
from enum import IntEnum
class diceType(IntEnum):
d4 = 4
d6 = 6
d8 = 8
d10 = 10
d12 = 12
d20 = 20
d100 = 100
@dataclass
class Roll:
amount: int
dice_type: diceType
bonus: int
def getMinRoll(self) -> int:
return self.amount + self.bonus
def getMaxRoll(self) -> int:
return self.amount * self.dice_type + self.bonus
def __repr__(self) -> str:
return f"{self.amount}d{self.dice_type}+{self.bonus}"
class RollFactory:
def __call__(self, dice: str) -> Roll:
bonus = '0'
if '+' in dice:
dice, bonus = dice.split('+')
amount, dice = dice.split('d')
return Roll(int(amount), diceType(int(dice)), int(bonus))
rollFactory = RollFactory()