Created
March 9, 2011 04:01
-
-
Save randrews/861672 to your computer and use it in GitHub Desktop.
Lua library for dice games
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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