Skip to content

tower

EmptyTowerError

Bases: Exception

Used when actions are taken on a tower where no dice remain.

RollDistribution dataclass

RollDistribution(
    dice_rolled: int, dice_results: dict[int, int]
)

An object that references the number of dice rolled and dict representing the counts for each possible result.

RollResult dataclass

RollResult(dice_rolled: int, dice_lost: int)

An object that contains the overall result of a tower roll: rolled vs lost.

Tower dataclass

Tower(dice_size: int = 6, dice_amount: int = 100)

A given instance of the dice tower.

Attributes:

Name Type Description
roll_distributions list[dict[int, int]]

A list of the previous roll results and their counts.

possible_values list[int]

A list of the possible die values based on size.

Source code in src/wretched_tower/tower.py
def __init__(self, dice_size: int = 6, dice_amount: int = 100) -> None:
    if dice_size < 2:  # noqa: PLR2004
        msg = "Dice must have more than one side!"
        raise ValueError(msg)
    self._dice_size = dice_size
    self.set_dice_left(dice_amount)
    self.possible_values = self._get_possible_die_values()
    self.roll_distributions = []

_get_possible_die_values

_get_possible_die_values() -> list[int]

Get a list of all the possible values based on the dice size.

Returns:

Type Description
list[int]

list[int]: The possible values based on the die size.

Source code in src/wretched_tower/tower.py
def _get_possible_die_values(self) -> list[int]:
    """
    Get a list of all the possible values based on the dice size.

    Returns:
        list[int]: The possible values based on the die size.
    """
    current_value = 1
    possible_values = []
    while current_value <= self._dice_size:
        possible_values.append(current_value)
        current_value += 1
    return possible_values

get_dice_left

get_dice_left() -> int

Get the dice remaining to be rolled.

Returns:

Name Type Description
int int

The dice remaining.

Source code in src/wretched_tower/tower.py
def get_dice_left(self) -> int:  # no cov
    """
    Get the dice remaining to be rolled.

    Returns:
        int: The dice remaining.
    """
    return self._dice_left

get_result_dict_template

get_result_dict_template() -> dict[int, int]

Get a dictionary of possible dice values and zeroed counts.

Source code in src/wretched_tower/tower.py
def get_result_dict_template(self) -> dict[int, int]:
    """Get a dictionary of possible dice values and zeroed counts."""
    results = {}
    for x in self.possible_values:
        results[x] = 0
    return results

roll_tower

roll_tower() -> RollResult

Using the dice remaining, roll them and then remove any that are ones, recording the results.

Returns:

Name Type Description
RollResult RollResult

The results of the roll as a RollResult object.

Source code in src/wretched_tower/tower.py
def roll_tower(self) -> RollResult:
    """
    Using the dice remaining, roll them and then remove any that are ones,
    recording the results.

    Returns:
        RollResult: The results of the roll as a RollResult object.
    """
    dice_to_roll = self.get_dice_left()
    if dice_to_roll == 0:
        msg = "Tower does not have any dice remaining."
        raise EmptyTowerError(msg)
    results = self.get_result_dict_template()
    for x in self.possible_values:
        results[x] = 0
    for _x in range(self._dice_left):
        die_result = random.randint(1, self._dice_size)  # nosec  # noqa: S311
        results[die_result] += 1
    self.set_dice_left(self._dice_left - results[1])
    self.roll_distributions.append(
        RollDistribution(dice_rolled=dice_to_roll, dice_results=results)
    )
    return RollResult(dice_rolled=dice_to_roll, dice_lost=results[1])

set_dice_left

set_dice_left(dice_left: int) -> None

Allows you to set the dice left value with some validations applied. Allowed values must be between 0 and 100.

Parameters:

Name Type Description Default
dice_left int

The dice remaining.

required

Raises:

Type Description
ValueError

If the dice amount exceeds 100 or is less than 0.

Source code in src/wretched_tower/tower.py
def set_dice_left(self, dice_left: int) -> None:
    """
    Allows you to set the dice left value with some validations applied.
    Allowed values must be between 0 and 100.

    Args:
        dice_left (int): The dice remaining.

    Raises:
        ValueError: If the dice amount exceeds 100 or is less than 0.
    """
    if dice_left > 100:  # noqa: PLR2004
        msg = "Tower cannot exceed 100 dice!"
        raise ValueError(msg)
    elif dice_left < 0:
        msg = "Tower dice amount cannot be a negative number!"
        raise ValueError(msg)
    self._dice_left = dice_left