Looks like you’re still learning the basics of Lua, so maybe code is more useful than words. Here’s a sample using approach “A” I described above. Just drop it in some folder as main.lua and run it.
math.randomseed(os.time()) local GRID\_COLS = 4 local GRID\_ROWS = 6 local GRID\_SPACING = 40 local grid = {} -- set up the grid for i = 1, GRID\_ROWS do grid[i] = {} for j = 1, GRID\_COLS do local tile = display.newCircle(j \* GRID\_SPACING, i \* GRID\_SPACING, 4) tile.isOccupied = false grid[i][j] = tile end end -- make a list of unoccupied grid tiles local findFreeTiles = function() local freeTiles = {} for i = 1, #grid do for j = 1, #grid[i] do if not grid[i][j].isOccupied then table.insert(freeTiles, grid[i][j]) end end end return freeTiles end -- spawn a thing on top of a grid tile local spawnThing = function(tile) local thing = display.newCircle(tile.x, tile.y, 20) thing:setFillColor(1, 0, 0) thing:toFront() tile.isOccupied = true end -- spawn a thing and place it on a random free grid tile local placeRandomThing = function() local freeTiles = findFreeTiles() if #freeTiles == 0 then print("No more room available, can't spawn more things!") return end local randomTile = freeTiles[math.random(1, #freeTiles)] spawnThing(randomTile) end -- make a button that spawns things local button = display.newText("Spawn Thing", display.contentCenterX, 10, native.systemFont, 16) button:addEventListener("tap", placeRandomThing)