Interesting use of a trie there
Just a suggestion, you could make it a bit more flexible, eliminate global state and (if you want) metatables by changing the API a bit:
Code: Select all
Cheatcode = require('cheatcode')
local codes = {}
codes['{up}{up}{down}{down}{left}{right}{left}{right}ba{return}'] = function() lives = lives + 99 end
codes.bighead = function() bighead = not bighead end
local cheatcodes = Cheatcode(codes)
function love.keyreleased(key)
cheatcodes:handle(key)
end
Thought it would be interesting to try to build the trie with pattern matching, it's ugly as hell but here it is:
Code: Select all
local function trie (t, text)
('}' .. text .. '{'):gsub('%b}{', function (m)
return '}' .. m:sub(2, -2):gsub('.', '{%0}') ..'{'
end):sub(2, -2):gsub('%b{}', function (m)
m = m:sub(2, -2)
t[m] = t[m] or {}
t = t[m]
end)
end
http://hastebin.com/qukarasipi.lua