Skip to content

Instantly share code, notes, and snippets.

@wilbefast
Last active August 29, 2015 14:05
Show Gist options
  • Save wilbefast/ac81fb161680424c29be to your computer and use it in GitHub Desktop.
Save wilbefast/ac81fb161680424c29be to your computer and use it in GitHub Desktop.
Matching Tetrominos using the Zen of Lua...
function Sacrifice:matchTetros(tetros, col, row)
for _, t in ipairs(tetros) do
local i = 1
local matches = {}
for c = col, col + 3 do
for r = row, row + 3 do
local char = t:sub(i,i)
local limb, core = (char == '*'), (char == '@')
if (limb or core) then
if self:match(c, r) then
table.insert(matches, { c = c, r = r, core = core})
if #matches >= 4 then
return matches
end
end
end
i = i + 1
end
end
end
return nil
end
function Sacrifice:match(col, row)
local tile = terrain:gridToTile(col, row)
return (tile and tile.sacrifice)-- and self.ritual[tile.sacrifice])
end
function Sacrifice:performRitual()
-- find the bounds of the ritual space
local min_col, min_row = self.tile.col, self.tile.row
local max_col, max_row = min_col, min_row
for _, s in ipairs(self.ritual) do
local col, row = s.tile.col, s.tile.row
if col > max_col then max_col = col end
if col < min_col then min_col = col end
if row > max_row then max_row = row end
if row < min_row then min_row = row end
end
local w_col, h_col = max_col - min_col + 1, max_row - min_row + 1
-- try to match each Tetromino across this space
terrain:mapRectangle(min_col, min_row, w_col, h_col, function(tile, c, r)
for tetroTypeName, tetroType in pairs(tetrominoes) do
local match = self:matchTetros(tetroType, c, r)
if match then
log:write("found " .. tetroTypeName)
end
end
end)
end
-- O shape
local tetro_O = "**--"..
"**--"..
"----"..
"----"
-- J shape
local tetro_J1 = "-*--"..
"-*--"..
"**--"..
"----"
local tetro_J2 = "***-"..
"--*-"..
"----"..
"----"
local tetro_J3 = "**--"..
"*---"..
"*---"..
"----"
local tetro_J4 = "*---"..
"***-"..
"----"..
"----"
-- L shape
local tetro_L1 = "*---"..
"*---"..
"**--"..
"----"
local tetro_L2 = "--*-"..
"***-"..
"----"..
"----"
local tetro_L3 = "**--"..
"-*--"..
"-*--"..
"----"
local tetro_L4 = "***-"..
"*---"..
"----"..
"----"
-- I shape
local tetro_I1 = "*---"..
"*---"..
"*---"..
"*---"
local tetro_I2 = "****"..
"----"..
"----"..
"----"
-- S shape
local tetro_S1 = "-*--"..
"**--"..
"*---"..
"----"
local tetro_S2 = "**--"..
"-**-"..
"----"..
"----"
-- Z shape
local tetro_Z1 = "*---"..
"**--"..
"-*--"..
"----"
local tetro_Z2 = "-**-"..
"**--"..
"----"..
"----"
-- T shape
local tetro_T1 = "*---"..
"**--"..
"*---"..
"----"
local tetro_T2 = "-*--"..
"***-"..
"----"..
"----"
local tetro_T3 = "-*--"..
"**--"..
"-*--"..
"----"
local tetro_T4 = "***-"..
"-*--"..
"----"..
"----"
-- pack and export
return
{
O = { tetro_O },
J = { tetro_J1, tetro_J2, tetro_J3, tetro_J4},
L = { tetro_L1, tetro_L2, tetro_L3, tetro_L4},
I = { tetro_I1, tetro_I2},
S = { tetro_S1, tetro_S2},
Z = { tetro_Z1, tetro_Z2},
T = { tetro_T1, tetro_T2, tetro_T3, tetro_T4},
}
@wilbefast
Copy link
Author

tetrominoes come from here, and mapRectangle comes from here :)

@wilbefast
Copy link
Author

As for core and self.ritual - what, you want all my secrets? :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment