def __init__(
            self, string_form: str,
            probability_distribution_factory: IProbabilityDistributionFactory):
        self._string_form = string_form
        self._number_of_dice = self._get_number_of_dice()
        self._single_dice_outcome_map = get_single_dice_outcome_map(
            re.split(r"[dk]", self._string_form)[1])
        self._number_to_keep_or_drop = self._get_number_to_keep_or_drop()
        self._simplified_form = None
        self._probability_distribution_factory = probability_distribution_factory

        if self._is_keep():
            if self._number_of_dice <= self._number_to_keep_or_drop:
                self._simplified_form = DiceExpression(
                    f"{self._number_of_dice}d{self._get_number_of_sides_string()}",
                    self._probability_distribution_factory,
                )
            elif self._number_to_keep_or_drop == 0:
                self._simplified_form = ConstantIntegerExpression(
                    "0", self._probability_distribution_factory)
        else:
            if self._number_of_dice <= self._number_to_keep_or_drop:
                self._simplified_form = ConstantIntegerExpression(
                    "0", self._probability_distribution_factory)
            elif self._number_to_keep_or_drop == 0:
                self._simplified_form = DiceExpression(
                    f"{self._number_of_dice}d{self._get_number_of_sides_string()}",
                    self._probability_distribution_factory,
                )
Exemplo n.º 2
0
 def test_dice_roll_fate_dice(self):
     self._test_dice = DiceExpression(
         "2dF", self._probability_distribution_factory)
     roll_set = set()
     for _ in range(10000):
         roll_set.add(self._test_dice.roll())
     self.assertSetEqual(roll_set, {-2, -1, 0, 1, 2})
Exemplo n.º 3
0
 def test_dice_roll_custom_dice_negative(self):
     self._test_dice = DiceExpression(
         "2d[-2,2,100]", self._probability_distribution_factory)
     roll_set = set()
     for _ in range(10000):
         roll_set.add(self._test_dice.roll())
     self.assertSetEqual(roll_set, {-4, 0, 4, 98, 102, 200})
Exemplo n.º 4
0
 def test_dice_roll_custom_dice_large_set(self):
     self._test_dice = DiceExpression(
         "2d[-2,0,2,4,6,31]", self._probability_distribution_factory)
     roll_set = set()
     for _ in range(10000):
         roll_set.add(self._test_dice.roll())
     self.assertSetEqual(
         roll_set, {-4, -2, 0, 2, 4, 6, 8, 10, 12, 29, 31, 33, 35, 37, 62})
Exemplo n.º 5
0
 def test_dice_get_probability_distribution_percentile_dice(self):
     self._test_dice = DiceExpression(
         "2d%", self._probability_distribution_factory)
     possible_rolls = itertools.product(range(1, 101), repeat=2)
     results = [sum(t) for t in possible_rolls]
     self.assertEqual(
         dict(collections.Counter(results)),
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 6
0
 def test_dice_roll_missing_dice_amount(self):
     self._test_dice = DiceExpression(
         "d10", self._probability_distribution_factory)
     roll_set = set()
     for _ in range(1000):
         roll_set.add(self._test_dice.roll())
     self.assertEqual(10, len(roll_set))
     self.assertEqual(10, max(roll_set))
     self.assertEqual(1, min(roll_set))
Exemplo n.º 7
0
 def test_dice_roll_percentile_dice(self):
     self._test_dice = DiceExpression(
         "1d%", self._probability_distribution_factory)
     roll_set = set()
     for _ in range(10000):
         roll_set.add(self._test_dice.roll())
     self.assertEqual(100, len(roll_set))
     self.assertEqual(100, max(roll_set))
     self.assertEqual(1, min(roll_set))
Exemplo n.º 8
0
 def test_dice_get_probability_distribution_custom_dice_multiplier(self):
     self._test_dice = DiceExpression(
         "d[-2*3,0,2]", self._probability_distribution_factory)
     self.assertEqual(
         {
             -2: 3,
             0: 1,
             2: 1
         },
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 9
0
 def test_dice_get_probability_distribution_custom_dice_range(self):
     self._test_dice = DiceExpression(
         "d[1-2*3,0,2]", self._probability_distribution_factory)
     self.assertEqual(
         {
             0: 1,
             1: 3,
             2: 4
         },
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 10
0
 def test_dice_get_probability_distribution_custom_dice_negative(self):
     self._test_dice = DiceExpression(
         "2d[-2,2,100]", self._probability_distribution_factory)
     self.assertEqual(
         {
             -4: 1,
             0: 2,
             4: 1,
             98: 2,
             102: 2,
             200: 1
         },
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 11
0
 def test_dice_get_probability_distribution_custom_dice_large_set(self):
     self._test_dice = DiceExpression(
         "d[-2,0,2,4,6,31,-2,-24]", self._probability_distribution_factory)
     self.assertEqual(
         {
             -24: 1,
             -2: 2,
             0: 1,
             2: 1,
             4: 1,
             6: 1,
             31: 1
         },
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 12
0
 def test_dice_get_probability_distribution_custom_dice_multiplier_range(
         self):
     self._test_dice = DiceExpression(
         "d[1-2*3,-4--9*10]", self._probability_distribution_factory)
     self.assertEqual(
         {
             -9: 10,
             -8: 10,
             -7: 10,
             -6: 10,
             -5: 10,
             -4: 10,
             1: 3,
             2: 3
         },
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 13
0
 def test_dice_get_probability_distribution_fate_dice(self):
     self._test_dice = DiceExpression(
         "4dF", self._probability_distribution_factory)
     self.assertEqual(
         {
             -4: 1,
             -3: 4,
             -2: 10,
             -1: 16,
             0: 19,
             1: 16,
             2: 10,
             3: 4,
             4: 1
         },
         self._test_dice.get_probability_distribution().get_result_map(),
     )
Exemplo n.º 14
0
 def test_dice_max_custom_dice_large_set(self):
     self._test_dice = DiceExpression(
         "76d[-2,0,2,4,6,31]", self._probability_distribution_factory)
     self.assertEqual(2356, self._test_dice.max())
Exemplo n.º 15
0
 def test_dice_max_fate_dice(self):
     self._test_dice = DiceExpression(
         "2dF", self._probability_distribution_factory)
     self.assertEqual(2, self._test_dice.max())
Exemplo n.º 16
0
 def test_dice_max_percentile_dice(self):
     self._test_dice = DiceExpression(
         "1d%", self._probability_distribution_factory)
     self.assertEqual(100, self._test_dice.max())
Exemplo n.º 17
0
 def test_dice_max_missing_dice_amount(self):
     self._test_dice = DiceExpression(
         "d10", self._probability_distribution_factory)
     self.assertEqual(10, self._test_dice.max())
Exemplo n.º 18
0
 def test_dice_str_percentile_dice(self):
     self._test_dice = DiceExpression(
         "1d%", self._probability_distribution_factory)
     self.assertEqual("1d%", str(self._test_dice))
Exemplo n.º 19
0
 def test_dice_str_fate_dice(self):
     self._test_dice = DiceExpression(
         "4dF", self._probability_distribution_factory)
     self.assertEqual("4dF", str(self._test_dice))
Exemplo n.º 20
0
 def test_dice_str_custom_dice_negative(self):
     self._test_dice = DiceExpression(
         "21d[-2,2,100]", self._probability_distribution_factory)
     self.assertEqual("21d[-2,2,100]", str(self._test_dice))
Exemplo n.º 21
0
 def test_dice_str_custom_dice_large_set(self):
     self._test_dice = DiceExpression(
         "d[-2,0,2,4,6,31,-2,-24]", self._probability_distribution_factory)
     self.assertEqual("d[-2,0,2,4,6,31,-2,-24]", str(self._test_dice))
Exemplo n.º 22
0
 def test_dice_estimated_cost_fate_dice(self):
     self._test_dice = DiceExpression(
         "10dF", self._probability_distribution_factory)
     self.assertEqual(30, self._test_dice.estimated_cost())
Exemplo n.º 23
0
 def test_dice_estimated_custom_dice_negative(self):
     self._test_dice = DiceExpression(
         "21d[-2,2,100]", self._probability_distribution_factory)
     self.assertEqual(63, self._test_dice.estimated_cost())
Exemplo n.º 24
0
 def test_dice_estimated_custom_dice_large_set(self):
     self._test_dice = DiceExpression(
         "2d[-2,0,2,4,6*7,31,-2,-24]",
         self._probability_distribution_factory)
     self.assertEqual(14, self._test_dice.estimated_cost())
Exemplo n.º 25
0
 def setUp(self):
     self._probability_distribution_factory = ProbabilityDistributionFactory(
     )
     self._test_dice = DiceExpression(
         "4d6", self._probability_distribution_factory)
     self._mock_parser_gen = create_autospec(rply.ParserGenerator)