-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
54 lines (43 loc) · 1.55 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from abc import ABC, abstractmethod
import random
def flatten_range(table):
"""
Converts a table with range strings as keys into a table with individual integers as keys.
Args:
- table (dict): A dictionary where keys are range strings (e.g., '1-16') and values are outcomes.
Returns:
- dict: A new dictionary with individual integers as keys and corresponding outcomes as values.
"""
flattened_table = {}
for range_str, outcome in table.items():
# Handle single value and range values differently
if '-' in range_str:
start, end = map(int, range_str.split('-'))
for i in range(start, end + 1):
flattened_table[i] = outcome
else:
flattened_table[int(range_str)] = outcome
return flattened_table
class Die:
def __init__(self, die_type):
self.die_type = die_type
def roll(self, n=1):
return [random.randint(1, self.die_type) for _ in range(n)]
class Table(ABC):
def __init__(self, table):
self.table = table
@abstractmethod
def roll(self, n=1):
pass
class DieTable(Table):
def __init__(self, die, flattened_table):
super().__init__(flattened_table)
self.die = Die(die) if isinstance(die, int) else die
def roll(self, n=1):
results = self.die.roll(n)
return [self.table.get(result, result) for result in results]
class ListTable(Table):
def __init__(self, table):
super().__init__(table)
def roll(self, n=1):
return random.choices(self.table, k=n)