Skip to content

Instantly share code, notes, and snippets.

@randrews
Created March 9, 2011 04:01
Show Gist options
  • Save randrews/861672 to your computer and use it in GitHub Desktop.
Save randrews/861672 to your computer and use it in GitHub Desktop.
Lua library for dice games
Die = {
roll = function(self)
self.value = math.random(self.sides)
end
}
Die["__index"] = Die
function D(sides, value)
local die = {sides = sides, value = value}
setmetatable(die, Die)
return die
end
require("dice")
test("D should make a die",
function()
local d = D(6)
assert(d ~= nil)
end)
test("D should set the sides for the die",
function()
local d = D(6)
assert(d.sides == 6)
end)
test("D should set the value if you pass two args",
function()
local d = D(6, 3)
assert(d.value == 3)
end)
test("D:roll should randomize the value",
function()
local d = D(6, 10) -- Use an invalid value so we can see it change
d:roll()
assert(d.value >= 1 and d.value <= 6)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment