Game Development Reference
In-Depth Information
Converting Numbers into Roman Numerals
If you ever need to translate number into Roman numerals, you use the toRoman function, as follows:
function toRoman(theNumber)
local res = ""
local romans = {
{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"},
{90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"},
{5, "V"}, {4, "IV"}, {1, "I"}
}
local k = theNumber
for i,j in ipairs(romans) do
theVal, theLet = unpack(j)
while k >= theVal do
k = k - theVal
res = res .. theLet
end
end
return res
you would like to implement linked lists in Lua, here's how (note that you need to place this code in a
file called linkedlist.lua ):
local _lists = {}
function _lists:create(theVal)
local root = { value = theVal, next = nil}
setmetatable(root, {__index = _lists })
return root
end
function _lists:append(theVal)
local _node = { value = theVal, next = nil}
if(self.value == nil) then
self.value = _node.value
self.next = _node.next
return
end
local currNode = self
while currNode.next ~= nil do
currNode = currNode.next
end
 
Search WWH ::




Custom Search