Question

I am trying to make a game and i have a 2d array

So its like this:

Grid[x][y]

lets pretend these values are in it:

Column 1 Column 2 Column 3 Column 4 Column 5
1        2        5        2        5 
2        2        3        1        1 
1        4        3        4        5 
1        3        3        3        5  <-- match this row
3        5        3        4        5 
2        4        3        4        5
2        4        4        4        5 

In the middle (index 4) i want to check if there are at least 3 times the same number and what about if there are 4 times the same or even 5.

How do you check this ? What would be a good way to find the same and delete those that are the same... I am stuck to figure out the logic to make something like this

this is what i tried:

grid = {}
for x = 1, 5 do
  grid[x] = {finish = false} 
  for y = 1, 7 do 
    grid[x][y] = {key= math.random(1,4)}
  end 
end

function check(t)
  local tmpArray = {}
  local object
  for i = 1,5 do
    object = t[i][1].key
    if object == t[i+1][1].key then
      table.insert( tmpArray, object )
    else 
      break
    end
  end
end

print_r(grid)
check(grid) 
print_r(grid)

where print_r prints the grid:

function print_r ( t ) 
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    sub_print_r(t,"  ")
end

It doesnt work that great because i check with the index after that one and if that isnt the same it doesnt add the last one.. I dont know if it is the best way to go...

If i "delete" the matched indexes my plan is to move the index row above or beneath it into the 4 index row... but first things first

Was it helpful?

Solution

You should compare the second index not the first: in the table

g = {{1,2,3}, {4,5,6}}

g[1] is first row i.e. {1,2,3}, not {1,4} the first column (first element of first and second rows). You were doing same thing in previous post of yours, you should reread the Lua docs about tables. You should do something like

for i = 1,#t do
     object = t[i][1].key
     if object == t[i][2].key then

This will only compare first two items in row. If you want to check whether the row has any identical consecutive items you will have to loop over the second index from 1 to #t[i]-1.

You might find the following print function much more useful, as it prints table as a grid, easier to see before/after:

function printGrid(g)
    for i, t in ipairs(g) do 
        print('{' .. table.concat(t, ',') .. '}')
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top