Module mk2lib.dice
Implementation of single/dual dice.
Classes
class Dice (dice: int, dice2: int | None)-
Expand source code
@dataclass class Dice: """ This class is for representing and rolling dice. """ dice: int dice2: int | None @property def double(self) -> bool: """ Whether player has rolled a double (two of the same dice). :return: Whether player has rolled a double. """ return self.dual and (self.dice == self.dice2) @property def dual(self) -> bool: """ Whether player had rolled two dice. :return: Whether player has rolled two dice. """ return self.dice2 is not None @property def sum(self) -> int: """ Sum of the roll on dice. :return: Sum of dice (or value of dice, if rolled only one). """ return self.dice + (self.dice2 or 0) @staticmethod def roll(dual=False): """ Roll (one or two) dice. :param dual: Whether to roll two dice. """ return Dice( dice=randint(1, 6), dice2=randint(1, 6) if dual else None, ) def serialize(self) -> dict: """ Serialize dice object into dict. :return: Dict with serialized Dice state. """ return {"dice": self.dice, "dice2": self.dice2} @classmethod def deserialize(cls, data: dict) -> Dice: """ Deserialize dice object from saved data. :param data: Dict with serialized Dice object. :return: Restored Dice object. """ dice = cls( dice=data["dice"], dice2=data["dice2"], ) return diceThis class is for representing and rolling dice.
Static methods
def deserialize(data: dict) ‑> Dice-
Deserialize dice object from saved data.
:param data: Dict with serialized Dice object. :return: Restored Dice object.
def roll(dual=False)-
Expand source code
@staticmethod def roll(dual=False): """ Roll (one or two) dice. :param dual: Whether to roll two dice. """ return Dice( dice=randint(1, 6), dice2=randint(1, 6) if dual else None, )Roll (one or two) dice.
:param dual: Whether to roll two dice.
Instance variables
var dice : int-
The type of the None singleton.
var dice2 : int | None-
The type of the None singleton.
prop double : bool-
Expand source code
@property def double(self) -> bool: """ Whether player has rolled a double (two of the same dice). :return: Whether player has rolled a double. """ return self.dual and (self.dice == self.dice2)Whether player has rolled a double (two of the same dice).
:return: Whether player has rolled a double.
prop dual : bool-
Expand source code
@property def dual(self) -> bool: """ Whether player had rolled two dice. :return: Whether player has rolled two dice. """ return self.dice2 is not NoneWhether player had rolled two dice.
:return: Whether player has rolled two dice.
prop sum : int-
Expand source code
@property def sum(self) -> int: """ Sum of the roll on dice. :return: Sum of dice (or value of dice, if rolled only one). """ return self.dice + (self.dice2 or 0)Sum of the roll on dice.
:return: Sum of dice (or value of dice, if rolled only one).
Methods
def serialize(self) ‑> dict-
Expand source code
def serialize(self) -> dict: """ Serialize dice object into dict. :return: Dict with serialized Dice state. """ return {"dice": self.dice, "dice2": self.dice2}Serialize dice object into dict.
:return: Dict with serialized Dice state.