-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.py
144 lines (112 loc) · 5.27 KB
/
evaluator.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from lark import Transformer
import numpy as np
from colorama import Fore
def truncate(string, length):
if len(string) > length:
return string[:length] + ' ...'
return string
class Evaluator(Transformer):
variable_declarations = {}
def variable_declaration(self, arguments):
name, value = arguments
if name in self.variable_declarations:
print(Fore.GREEN +
f"Redeclearing variable {name} from {self.variable_declarations[name]} to {value}")
else:
print(Fore.GREEN +
f"Declearing variable {name} with value {value}")
self.variable_declarations[name] = value
return value
def variable_search(self, arguments):
(name, ) = arguments
if name not in self.variable_declarations:
raise ValueError(f"Variable {name} is not defined")
return self.variable_declarations[name]
def dice_notation(self, arguments):
(result,) = arguments
return result
def addition(self, arguments):
left, right = arguments
return left + right
def subtraction(self, arguments):
left, right = arguments
return left - right
def multiplication(self, arguments):
left, right = arguments
return left * right
def divition(self, arguments):
left, right = arguments
return left / right
def keep_highest(self, arguments):
(value,) = arguments
return "keep highest", int(value)
def keep_lowest(self, arguments):
(value,) = arguments
return "keep lowest", int(value)
def keep_choice(self, arguments):
(value,) = arguments
return "keep choice", int(value)
def number(self, arguments):
(value,) = arguments
return float(value)
def fudge(self, arguments):
(quantity, ) = arguments
# TODO: Extrapolate to negative and floating point numbers
rolls = np.random.choice([-1, 0, 1], size=int(quantity))
mapping_dictionary = {-1: "-", 0: " ", 1: "+"}
print(Fore.GREEN + truncate(
f"Rolling {int(quantity)} fudge dices: {' '.join([f'[{mapping_dictionary[key]}]' for key in rolls])}", 100))
return rolls.sum()
def dice(self, arguments):
quantity, number_of_faces, *keep = arguments
if quantity < sum([value for (k, value) in keep]):
raise ValueError(
Fore.RED + "The maximum amount of dice you can keep is the quantity of the throw")
# TODO: Extrapolate to negative and floating point numbers
rolls = np.random.choice(int(number_of_faces), size=int(quantity)) + 1
print(Fore.GREEN + truncate(
f"Rolling {int(quantity)} {int(number_of_faces)}-sided dices: {[roll for roll in rolls]}", 100))
if keep:
rolls_kept = np.array([])
sorted_rolls_left = np.sort(rolls)
for keep_key_word, amount in keep:
if keep_key_word == "keep highest":
print(Fore.GREEN +
f"Keeping the {amount} highest dice: {sorted_rolls_left[-amount:]}")
rolls_kept = np.concatenate(
(rolls_kept, sorted_rolls_left[-amount:]))
sorted_rolls_left = sorted_rolls_left[:-amount]
if keep_key_word == "keep lowest":
print(Fore.GREEN +
f"Keeping the {amount} lowest dice: {sorted_rolls_left[:amount]}")
rolls_kept = np.concatenate(
(rolls_kept, sorted_rolls_left[:amount]))
sorted_rolls_left = sorted_rolls_left[amount:]
if keep_key_word == "keep choice":
choices_array = self._input_keep_choice_handler(
sorted_rolls_left, amount)
for choice in choices_array:
if choice not in sorted_rolls_left:
raise ValueError(Fore.RED +
f"The value '{choice}' is not avaiable")
index = np.where(sorted_rolls_left == choice)[0][0]
sorted_rolls_left = np.delete(sorted_rolls_left, index)
rolls_kept = np.concatenate((rolls_kept, choices_array))
return rolls_kept.sum()
return rolls.sum()
def _input_keep_choice_handler(self, sorted_rolls_left, amount):
choices = input(Fore.YELLOW +
f"Choose {amount} dices you would like to keep from {list(sorted_rolls_left)} (write them separated by spaces): "
+ Fore.RESET)
choices_array = np.array(
[int(choice) for choice in choices.split()])
while len(choices_array) != amount:
print(
Fore.RED + f"Wrong amount of dice: your input amount was {len(choices_array)} but the expected was {amount}")
choices = input(Fore.YELLOW +
f"Choose {amount} dices you would like to keep from {list(sorted_rolls_left)} (write them separated by spaces): "
+ Fore.RESET)
choices_array = np.array(
[int(choice) for choice in choices.split()])
return choices_array
evaluator = Evaluator()