Game question for something like dominoes

Hello,

I was working on a game of my own that has pieces similar to the game of dominoes. Basically what I am trying to figure out is how to snap tiles together as they are played. For example, if I have a tile with a 3 on one side and I want a tile of mine with a 3 to snap to it when I drag it near… I am trying to see how to do this.

I know I will have a table of all tiles that are put on the table that will contain information such as their two values (one for each side), the x/y coordinates of where it is on the table, etc.

The only way I was thinking of doing this is if you drag the tile near where it goes I can tell by the X/Y of the other time where this one should be placed. Then when I let go of the tile from dragging with my finger I just move it there.

Maybe I answered my question but the biggest task I have now is figuring how to know what tile it is going to connect to. One game that does this puts a yellow box over the target tile on the end it connects with when you are near it.

Warren

but the biggest task I have now is figuring how to know what tile it is going to connect to

It’s been a while since I’ve played dominoes, but am I right in remembering that you can only place a new domino against one of the two dominoes that lie on each end of the ‘chain’?  

If so, just make sure all tiles that are placed get put into a table, and then when placing a new tile just check tiles 1 and #tiles:

Here’s a very quick version as an example, obv you’d need to tweak to fit your game (e.g. if you want any doubles to be placed sideways):

local placed = {} local dominoWidth, dominoHeight = 100, 50 local firstDomino = display.newImageRect("domino.png", dominoWidth, dominoHeight) firstDomino.value1 = 2 firstDomino.value2 = 3 placed[1] = firstDomino local function checkIsNewDominoIsNearExistingDomino(dom1, dom2) --find out if new domino is close to existing domino local dx = dom1.x - dom2.x local dy = dom1.y - dom2.y local c = (dx \* dx) + (dy \* dy) local distance = math.sqrt(c) if distance \< dominoWidth then return true else return false end end local function placeDomino(newDomino) if newDomino.value1 == placed[1].value1 or newDomino.value1 == placed[1].value2 or newDomino.value2 == placed[1].value1 or newDomino.value2 == placed[1].value2 then if checkIsNewDominoIsNearExistingDomino(placed[1], newDomino) then --insert new domino at BEGINNING of table table.insert(placed, 1, newDomino) newDomino.x, newDomino.y = placed[1].x - dominoWidth, placed[1].y end elseif newDomino.value1 == placed[#placed].value1 or newDomino.value1 == placed[#placed].value2 or newDomino.value2 == placed[#placed].value1 or newDomino.value2 == placed[#placed].value2 then if checkIsNewDominoIsNearExistingDomino(placed[#placed], newDomino) then --insert new domino at END of table table.insert(placed, newDomino) newDomino.x, newDomino.y = placed[1].x + dominoWidth, placed[1].y end end end

Thanks! That gave me a good start on how to handle this.

Warren

but the biggest task I have now is figuring how to know what tile it is going to connect to

It’s been a while since I’ve played dominoes, but am I right in remembering that you can only place a new domino against one of the two dominoes that lie on each end of the ‘chain’?  

If so, just make sure all tiles that are placed get put into a table, and then when placing a new tile just check tiles 1 and #tiles:

Here’s a very quick version as an example, obv you’d need to tweak to fit your game (e.g. if you want any doubles to be placed sideways):

local placed = {} local dominoWidth, dominoHeight = 100, 50 local firstDomino = display.newImageRect("domino.png", dominoWidth, dominoHeight) firstDomino.value1 = 2 firstDomino.value2 = 3 placed[1] = firstDomino local function checkIsNewDominoIsNearExistingDomino(dom1, dom2) --find out if new domino is close to existing domino local dx = dom1.x - dom2.x local dy = dom1.y - dom2.y local c = (dx \* dx) + (dy \* dy) local distance = math.sqrt(c) if distance \< dominoWidth then return true else return false end end local function placeDomino(newDomino) if newDomino.value1 == placed[1].value1 or newDomino.value1 == placed[1].value2 or newDomino.value2 == placed[1].value1 or newDomino.value2 == placed[1].value2 then if checkIsNewDominoIsNearExistingDomino(placed[1], newDomino) then --insert new domino at BEGINNING of table table.insert(placed, 1, newDomino) newDomino.x, newDomino.y = placed[1].x - dominoWidth, placed[1].y end elseif newDomino.value1 == placed[#placed].value1 or newDomino.value1 == placed[#placed].value2 or newDomino.value2 == placed[#placed].value1 or newDomino.value2 == placed[#placed].value2 then if checkIsNewDominoIsNearExistingDomino(placed[#placed], newDomino) then --insert new domino at END of table table.insert(placed, newDomino) newDomino.x, newDomino.y = placed[1].x + dominoWidth, placed[1].y end end end

Thanks! That gave me a good start on how to handle this.

Warren