I have a pattern game and it is almost done however I am stuck on the last part.

I have a screen with 25 blocks (in a table format 5x5) and the user clicks on it to turn over the blocks, I need to see if the user has turned over a line of blocks.

So I created a bunch of options and I am trying to compare it to the table of the current system (1 = not turned over and 0 = turned over). But the problem is my table looks like

local level1o1 ={ 0,0,0,0,0,
                  1,0,1,1,1 }

And when I do the compare it doesn't say it is the same because of the other random 0's in the table. I don't want to make every single possible result table, so any ideas of how I could do this?

Compare function

-- Table Compare
local function deepcompare(t1,t2,ignore_mt)
    local ty1 = type(t1)
    local ty2 = type(t2)
    if ty1 ~= ty2 then return false end
    -- non-table types can be directly compared
    if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
    -- as well as tables which have the metamethod __eq
    local mt = getmetatable(t1)
    if not ignore_mt and mt and mt.__eq then return t1 == t2 end
    for k1,v1 in pairs(t1) do
        local v2 = t2[k1]
        if v2 == nil or not deepcompare(v1,v2) then return false end
    for k2,v2 in pairs(t2) do
        local v1 = t1[k2]
        if v1 == nil or not deepcompare(v1,v2) then return false end
    return true

Example list of possible answers

local level1o1 ={ 0,0,0,0,0,
                 1,1,1,1,1 }

local level1o2 ={ 0,1,1,1,1,
                 1,1,1,1,0 }

.. etc
  • \$\begingroup\$ Can you give an input and what you expect as result? The question is not very clear (IMHO). \$\endgroup\$
    – Valmond
    Dec 10, 2011 at 20:44
  • \$\begingroup\$ Sorry? It seems clear to me, I have a bunch of tiles that get turned over (see first code secion for example) and I need to somehow compare it to a bunch of possible matches (see last code section). But the problem is it won't be just like that, there will be random zero's around the entire table as they don't just get to click an entire line. \$\endgroup\$
    – Steven
    Dec 10, 2011 at 21:32

1 Answer 1


Considering your table is a 5x5 table, and you search for complete lines, I guess there is only 12 results to compare with (considering the 2 diagonals).

That doesn't sound that long and hard to create all of them by hand.

Then it's straightforward to compare the 12 tables one by one with your current table to see if you can find the 0 (from result table) scheme matching your current game table.

To be more precise, here is one of the possible methods you could use (and the easiest that come into my mind);

  1. Select the first result table (of 12) and set a "completeLineFlag" to false.
  2. Parse the selected result table and each time you find a 0, use the current table index to verify if you can find a 0 at the exact same index of your current game table.
    • If you can find it then change the flag to true and keep going on
    • If you can't find the required 0 then stop the parsing of the current result table and set the flag to false.
  3. At that point you finished to test the selected result table; if you found all the parsed 0 of the result table you should get the flag set to "true", but if any of the required 0 wasn't there you should get the flag set to "false".
  4. Now, if the flag is true you know that the complete line you just tested is in your current game table, whereas if the flag is false, you can return to point 1 with the next result table until you find a good one or until you tested them all.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .