Dice Module

The dice module provides an OOP solution for creating various sided die simulators and a conveniant way of rolling multiple (varying) Dice objects

 1from ewccommons.dice import Dice, DiceShaker
 2
 3# Set a number of sides for the dice
 4number_of_die_sides: int = 4
 5# Create a n sided, named Dice object
 6dice: Dice = Dice(number_of_die_sides, name="My New Die", val=None)
 7# Have a look at what is created
 8print(
 9    f"Dice({number_of_die_sides})",
10    dice,
11    dice < number_of_die_sides,
12    d > 0,
13    sep="\n",
14)
15# Roll the dice and get the rolled value
16dice_roll: int = dice.roll()
17# Get the last rolled value
18dice_rolled: int = dice.rolled
19# The dice object can be compared like an int, using the rolled value
20if dice == number_of_die_sides:
21    # The dice object can be converted to string
22    # This gives a readable version of the last rolled value
23    print("Highest Roll", dice, sep="\n")
24# The dice object also supports new object copy
25# The copy will have the same number of sides/faces and dice name
26# The copy will also have the starting value of the last rolled
27dice2: Dice = dice.copy()
28# Roll both dice to hopefully get 2 different numbers
29# It is possible for both objects to independantly choose the same random value
30dice.roll()
31dice2.roll()
32# Verify the 2 dice could be different in value
33print(dice, dice2, dice.rolled, dice2.rolled, sep="\n")
34
35# TODO Add the DiceShaker example code