Module:Dice: Difference between revisions
Jump to navigation
Jump to search
(add error checking) |
m (get up to date with repo version) |
||
Line 2: | Line 2: | ||
Dice.lua | Dice.lua | ||
The core repository for this Lua module can be found at: | The core repository and documentation for this Lua module can be found at: | ||
https://bitbucket.org/HeladoDeBrownie/dice/ | https://bitbucket.org/HeladoDeBrownie/dice/ | ||
Line 17: | Line 17: | ||
--# Interface | --# Interface | ||
function Dice.fromDiceString(diceString) | function Dice.fromDiceString(diceString) | ||
local diceTable = {} | local diceTable = {} | ||
Line 63: | Line 49: | ||
end | end | ||
Dice.fromString = Dice.fromDiceString | Dice.fromString = Dice.fromDiceString | ||
function Dice.fromRangeString(rangeString) | function Dice.fromRangeString(rangeString) | ||
local minimum, maximum = rangeString:gsub('%s', ''):match'^(%d+)-(%d+)$' | local minimum, maximum = rangeString:gsub('%s', ''):match'^(%d+)-(%d+)$' | ||
Line 98: | Line 71: | ||
end | end | ||
function Dice:minimum() | function Dice:minimum() | ||
local sum = 0 | local sum = 0 | ||
Line 113: | Line 85: | ||
end | end | ||
function Dice:mean() | function Dice:mean() | ||
local sum = 0 | local sum = 0 | ||
Line 127: | Line 95: | ||
end | end | ||
function Dice:maximum() | function Dice:maximum() | ||
local sum = 0 | local sum = 0 |
Revision as of 20:03, 31 July 2019
This module is maintained and documented on GitHub.
--[[
Dice.lua
The core repository and documentation for this Lua module can be found at:
https://bitbucket.org/HeladoDeBrownie/dice/
--]]
local Dice = {}
Dice.__index = Dice
--# Private
local function newDice(diceTable)
return setmetatable({diceTable = diceTable}, Dice)
end
--# Interface
function Dice.fromDiceString(diceString)
local diceTable = {}
for term in diceString:gsub('%s', ''):gmatch'[+-]?[^+-]+' do
local quantity, size = term:match'^([+-]?%d+)d(%d+)$'
if quantity ~= nil and size ~= nil then
diceTable[#diceTable + 1] = {
quantity = tonumber(quantity, 10),
size = tonumber(size, 10),
}
else
local quantity = term:match'^([+-]?%d+)$'
if quantity ~= nil then
diceTable[#diceTable + 1] = {
quantity = tonumber(quantity, 10),
size = 1,
}
else
error([[
I couldn't make sense of this as a dice string: ]] .. diceString .. [[
In particular, this part doesn't look like a term I understand: ]] .. term)
end
end
end
return newDice(diceTable)
end
Dice.fromString = Dice.fromDiceString
function Dice.fromRangeString(rangeString)
local minimum, maximum = rangeString:gsub('%s', ''):match'^(%d+)-(%d+)$'
if minimum ~= nil and maximum ~= nil then
return newDice{
{
quantity = 1,
size = maximum - minimum + 1,
},
{
quantity = minimum - 1,
size = 1,
},
}
else
error("I couldn't make sense of this as a range string: "
.. rangeString)
end
end
function Dice:minimum()
local sum = 0
for _, subdice in ipairs(self.diceTable) do
if subdice.quantity >= 0 then
sum = sum + subdice.quantity * 1
else
sum = sum + subdice.quantity * subdice.size
end
end
return sum
end
function Dice:mean()
local sum = 0
for _, subdice in ipairs(self.diceTable) do
sum = sum + subdice.quantity * (1 + subdice.size) / 2
end
return sum
end
function Dice:maximum()
local sum = 0
for _, subdice in ipairs(self.diceTable) do
if subdice.quantity >= 0 then
sum = sum + subdice.quantity * subdice.size
else
sum = sum + subdice.quantity * 1
end
end
return sum
end
--# Export
return Dice