Peilonrayz / dice_stats

Dice statistics made simple.
MIT License
3 stars 1 forks source link

Change `Dice.apply_functions` to allow an changes in total chance #2

Closed Peilonrayz closed 4 years ago

Peilonrayz commented 4 years ago

Currently the function is instructing us to use it in the following way. Whilst this is convenient it doesn't allow changes to the total chance of the dice.

print(
    Dice.from_dice(6)
        .apply_functions(
            {(1,): lambda _: Dice.from_dice(6)},
            lambda d: d,
        )
)

This should require a call to Dice.as_total_chance, or specifying the total chance at creation, as the chance for the result of 1 doesn't jump from 1/6 to 1 which is what the code is saying. This would change usage to:

print(
    Dice.from_dice(6)
        .apply_functions(
            {(1,): lambda d: Dice.from_dice(6, d)},
            lambda d: d,
        )
)

I do not think that Dice.apply_dice needs to change for the above reasons. This is because to be able to find the actual chance is the job of apply_dice, and would make it needlessly complex. However it should be checked that an increased chance is allowed to work.

print(
    Dice.from_dice(3)
        .apply_dice(
            {(1,): Dice.from_dice(4, 2)},
            Dice.from_dice(6),
        )
)

To:

Dice[4/3](
  1: 27.8%  5/18
  2: 27.8%  5/18
  3: 27.8%  5/18
  4: 27.8%  5/18
  5: 11.1%  1/9
  6: 11.1%  1/9
)
Peilonrayz commented 4 years ago

The above has been implemented. Please note that whilst the above form for apply_functions works so does the following. Note that @ changes the total chance the same way as_total_chance and creating a new dice with a specified total chance, like above, do.

d6 = Dice.from_dice(6)
print(
    d6.apply_functions(
        {(1,): lambda d: d6 @ d},
        lambda d: d,
    )
)