hitting a Lua n00b wall with comparing tables.
attempting some 1-d CA stuff (a la āless conceptsā), basically translating seed integers to binary and using comparisons against a fixed set of neighborhood rules to determine note events. stole a compare function from the internet.
so far, I have...
seed = 30
rule = 251
function seed_to_binary()
seed_as_binary = {}
for i = 0,7 do
table.insert(seed_as_binary, (seed & (2 ^ i)) >> i)
end
end
function rule_to_binary()
rule_as_binary = {}
for i = 0,7 do
table.insert(rule_as_binary, (rule & (2 ^ i)) >> i)
end
end
function init()
seed_to_binary()
rule_to_binary()
seed_packs ={}
seed_packs = { {seed_as_binary[1], seed_as_binary[8], seed_as_binary[7]},
{seed_as_binary[8], seed_as_binary[7], seed_as_binary[6]},
{seed_as_binary[7], seed_as_binary[6], seed_as_binary[5]},
{seed_as_binary[6], seed_as_binary[5], seed_as_binary[4]},
{seed_as_binary[5], seed_as_binary[4], seed_as_binary[3]},
{seed_as_binary[4], seed_as_binary[3], seed_as_binary[2]},
{seed_as_binary[3], seed_as_binary[2], seed_as_binary[1]},
{seed_as_binary[2], seed_as_binary[1], seed_as_binary[8]} }
neighborhoods = { {1,1,1},
{1,1,0},
{1,0,1},
{1,0,0},
{0,1,1},
{0,1,0},
{0,0,1},
{0,0,0} }
if compare (seed_packs,neighborhoods) then
print("true!")
else
print("false")
end
end
function compare (s, n)
if type(s) == type(n) then
if type(s) == "table" then
for loop=1, 3 do
if compare (s[1][loop], n[8][loop]) == false then
return false
end
end
return true
else
return s == n
end
end
return false
end
getting stuck with the compare function working with the tables. right now, itās only able to return results comparing line 1 of the seed table and line 8 of the neighborhood table. it currently verifies that binary seed cluster #1 (000) matches neighborhood #8 (000).
however, I want it to return ātrueā when a line of the seed table matches a line of the neighborhood table, basically like this:
does binary seed cluster #1 match neighborhood #1?
if yes, event and move on.
if no, move on and try with seed cluster #1 + neighborhood #2, then neighborhood #3, etc etc until neighborhood #8, then:
does binary seed cluster #2 match neighborhood #1?
and again.
this is likely murky, but figured Iād ask 