-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDice.lua
355 lines (280 loc) · 8.66 KB
/
Dice.lua
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
-- The implementation of this module makes use of a class metaphor. All a class
-- really is is a table meant to be used as a metatable.
-- # Helper Functions
-- Instantiate the given class, passing the given arguments to its 'initialize'
-- method.
local function new(class, ...)
local result = setmetatable({}, {__index = class})
result:initialize(...)
return result
end
-- Derive the given class, adding a 'new' method to the child class.
local function derive(class)
return setmetatable({new = new}, {__index = class})
end
-- Return a function usable as a method that always gives an error.
local function unimplemented(method_name)
assert(type(method_name) == 'string')
return function ()
error('unimplemented: ' .. method_name)
end
end
-- Return a function usable as a method that calls the given method.
local function alias(method_name)
return function (self, ...)
return self[method_name](self, ...)
end
end
-- Is the given value an integer?
local function is_integer(value)
return type(value) == 'number' and value == math.floor(value)
end
-- Is the given value an integer at least a certain amount?
local function is_integer_at_least(lower_bound, value)
return is_integer(value) and value >= lower_bound
end
-- Is the given value a natural number, i.e., an integer at least 0?
local function is_natural(value)
return is_integer_at_least(0, value)
end
-- Does the given value represent an expression in the dice DSL?
-- NOTE: This function exists mostly to mark intent. It doesn't actually check
-- if the given table has all the methods expected of an expression.
local function is_expression(value)
return type(value) == 'table'
end
-- Every distribution we care about is symmetric, so its mean is the mean of
-- its minimum and maximum.
local function mean(expression)
return (expression:maximum() + expression:minimum()) / 2
end
-- The range of a distribution is the distance between its minimum and maximum.
local function range(expression)
return expression:maximum() - expression:minimum()
end
-- The standard deviation of a distribution is the square root of its variance.
local function standard_deviation(expression)
return math.sqrt(expression:variance())
end
-- Given two dice expressions, signal which is "better", as judged by the
-- following metrics:
-- - greater mean
-- - smaller range
-- - less variance
-- The first return value will be 1 if the first argument is better, -1 if the
-- second is, or 0 if they're indistinguishable. The second return value is a
-- string describing the metric by which the better one won.
function compare(dice_a, dice_b)
local mean_a, mean_b = dice_a:mean(), dice_b:mean()
if mean_a > mean_b then
return 1, 'greater mean'
elseif mean_b > mean_a then
return -1, 'greater mean'
else
local range_a, range_b = dice_a:range(), dice_b:range()
if range_a < range_b then
return 1, 'smaller range'
elseif range_b < range_a then
return -1, 'smaller range'
else
local variance_a, variance_b =
dice_a:variance(), dice_b:variance()
if variance_a < variance_b then
return 1, 'less variance'
elseif variance_b < variance_a then
return -1, 'less variance'
else
return 0, 'no difference'
end
end
end
end
-- # Dice Expression DSL
-- ## BaseExpression
local BaseExpression = {
-- These definitions don't need to be overridden by derived classes.
mean = mean,
range = range,
standard_deviation = standard_deviation,
compare = compare,
-- Derived classes must provide definitions for these.
initialize = unimplemented('initialize'),
minimum = unimplemented('minimum'),
maximum = unimplemented('maximum'),
variance = unimplemented('variance'),
roll = unimplemented('roll'),
-- Methods can be called by various names.
average = alias('mean'),
ev = alias('mean'),
expected_value = alias('mean'),
sd = alias('standard_deviation'),
min = alias('minimum'),
max = alias('maximum'),
sample = alias('roll'),
}
-- ## Constant
local Constant = derive(BaseExpression)
function Constant:initialize(constant)
assert(is_integer(constant))
self.constant = constant
end
function Constant:minimum()
return self.constant
end
function Constant:maximum()
return self.constant
end
function Constant:variance()
return 0
end
function Constant:roll()
return self.constant
end
-- ## DiceRoll
local DiceRoll = derive(BaseExpression)
function DiceRoll:initialize(quantity, sides)
assert(is_natural(quantity))
assert(is_natural(sides) and sides >= 1)
self.quantity = quantity
self.sides = sides
end
function DiceRoll:minimum()
return math.min(self.quantity * 1, self.quantity * self.sides)
end
function DiceRoll:maximum()
return math.max(self.quantity * 1, self.quantity * self.sides)
end
function DiceRoll:variance()
local single_die_mean = (1 + self.sides) / 2
local sum = 0
for n = 1, self.sides do
sum = sum + (n - single_die_mean) ^ 2
end
return math.abs(self.quantity) * sum / self.sides
end
function DiceRoll:roll()
local sum = 0
for n = 1, math.abs(self.quantity) do
sum = sum + math.random(1, self.sides)
end
if self.quantity >= 0 then
return sum
else
return -sum
end
end
-- ## Addition
local Addition = derive(BaseExpression)
function Addition:initialize(operand_a, operand_b)
assert(is_expression(operand_a))
assert(is_expression(operand_b))
self.operand_a = operand_a
self.operand_b = operand_b
end
function Addition:minimum()
return self.operand_a:minimum() + self.operand_b:minimum()
end
function Addition:maximum()
return self.operand_a:maximum() + self.operand_b:maximum()
end
function Addition:variance()
return self.operand_a:variance() + self.operand_b:variance()
end
function Addition:roll()
return self.operand_a:roll() + self.operand_b:roll()
end
-- ## Negation
local Negation = derive(BaseExpression)
function Negation:initialize(operand)
assert(is_expression(operand))
self.operand = operand
end
function Negation:minimum()
return -self.operand:maximum()
end
function Negation:maximum()
return -self.operand:minimum()
end
function Negation:variance()
return self.operand:variance()
end
function Negation:roll()
return -self.operand:roll()
end
-- ## Multiplication
local Multiplication = derive(BaseExpression)
function Multiplication:initialize(expression, constant)
assert(is_expression(expression))
assert(is_natural(constant))
self.expression = expression
self.constant = constant
end
function Multiplication:minimum()
if self.constant >= 0 then
return self.expression:minimum() * self.constant
else
return self.expression:maximum() * self.constant
end
end
function Multiplication:maximum()
if self.constant >= 0 then
return self.expression:maximum() * self.constant
else
return self.expression:minimum() * self.constant
end
end
function Multiplication:variance()
return self.expression:variance() * self.constant * self.constant
end
function Multiplication:roll()
return self.expression:roll() * self.constant
end
-- # Parsing
local Dice = {}
local function try_integer(input)
return input ~= nil and tonumber(input:match'^[+-]?%d+$') or nil
end
local function try_natural(input)
return input ~= nil and tonumber(input:match'^%d+$') or nil
end
function Dice.parse(input)
input = input:gsub('%s', '')
local x, y
-- addition
x, y = input:match'^([^+]+)+(.+)$'
if x ~= nil then
return Addition:new(Dice.parse(x), Dice.parse(y))
end
-- subtraction
x, y = input:match'^([^-]+)-(.+)$'
if x ~= nil and input:match'd' then
return Addition:new(Dice.parse(x), Negation:new(Dice.parse(y)))
end
-- multiplication
x, y = input:match'^([^x]+)x(.+)$'
y = try_integer(y)
if x ~= nil and y ~= nil then
return Multiplication:new(Dice.parse(x), y)
end
-- range
x, y = input:match'^([^-]+)-(.+)$'
x, y = try_natural(x), try_natural(y)
if x ~= nil and y ~= nil then
return Addition:new(DiceRoll:new(1, y - x + 1), Constant:new(x - 1))
end
-- dice
x, y = input:match'^([^d]+)d(.+)$'
x, y = try_integer(x), try_natural(y)
if x ~= nil and y ~= nil then
return DiceRoll:new(x, y)
end
-- constant
x = try_integer(input)
if x ~= nil then
return Constant:new(x)
end
error(([[
I couldn't understand this as part of an expression:
%s]]):format(input))
end
return Dice