checking object grid position

I have a 10x10 grid. On this grid I spawn a ball (play piece) at grid position grid[1][10]. I have a tap event listener function that checks the position of the ball, spawns two new balls at the same position, removes the original ball and then based on the original position transitions each of the new balls to a new grid position. Here’s my question: Is there a simple way to set up a series of if - then statements to check the grid position of the ball in the tap event listener? What I don’t want is 100 different if-then statements, one for each grid position. Here’s the tap event listener function to handle one grid position:

local function ballTapped(event)

    local t = event.target

    xNew= t.x

    yNew = t.y

    

        if t.x == grid[1][10].x and t.y == grid[1][10].y then

           print(tracker)

            t:removeSelf()

            t = nil 

            blueBallSpawn()

            blueBallSpawn()

            transition.to(spawnTable[tracker-1],{time = 50, x = grid[1][9].x} )

            transition.to(spawnTable[tracker], {time = 50, x = grid[2][10].x})

            audio.play(miteSound)

        end

end

In this example, the ball is in the bottom left corner of the grid. The basis of the game play is that tapping a ball divides it in two, the two new balls transitioning to new grid positions. There will be various grid positions to handle - corners, sides, and two separate interiors. Like I said above, I don’t want 100 different if-then statements to handle the transitions. Any help would be greatly appreciated. Thanks.

What are the rules for balls that aren’t in corners? As long as the destinations of each box aren’t completely random then it should be possible to set this up in a loop and add a child table to each grid ‘box’ that says which grid box to transition the two new balls to.

Thanks for your quick response nick!

No, ball movement would not be random. It would be based on a set of rules specific to the balls location. See example image.

Balls that are along top/bottom/leftside/rightside will spawn left diagonal and right diagonal. Balls on the interior will spawn up/down or left/right. How would I go about adding a child table to the grid ‘box’? My grid code is as follows:

local grid = {}

    for col = 1, numCols do

        grid[col] = {}

            for row = 1, numRows do

                grid[col][row] = display.newImage(“blank.png”)

                grid[col][row].x = xPos + col * (blankWidth + colSpace) - blankWidth/2 - colSpace

                grid[col][row].y = yPos + row * (blankHeight + rowSpace) - blankHeight/2 - rowSpace

                grid[col][row].gridPos = {x=col, y=row}

                grid[col][row].alpha = 1

           end

    end

I’ve resolved this problem. Thanks. 

What are the rules for balls that aren’t in corners? As long as the destinations of each box aren’t completely random then it should be possible to set this up in a loop and add a child table to each grid ‘box’ that says which grid box to transition the two new balls to.

Thanks for your quick response nick!

No, ball movement would not be random. It would be based on a set of rules specific to the balls location. See example image.

Balls that are along top/bottom/leftside/rightside will spawn left diagonal and right diagonal. Balls on the interior will spawn up/down or left/right. How would I go about adding a child table to the grid ‘box’? My grid code is as follows:

local grid = {}

    for col = 1, numCols do

        grid[col] = {}

            for row = 1, numRows do

                grid[col][row] = display.newImage(“blank.png”)

                grid[col][row].x = xPos + col * (blankWidth + colSpace) - blankWidth/2 - colSpace

                grid[col][row].y = yPos + row * (blankHeight + rowSpace) - blankHeight/2 - rowSpace

                grid[col][row].gridPos = {x=col, y=row}

                grid[col][row].alpha = 1

           end

    end

I’ve resolved this problem. Thanks.