Need help to complete a sliding puzzle game

Hi!
I’m a beginner in Corona so i tough it would be a good exercise to make a sliding puzzle.
I managed to create the grid but now I’m stuck in the game mechanics.

[code]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local gridSubdivision = 3
local pieceCount = 0
local lastPiece = (gridSubdivision * gridSubdivision) - 1
local step = 64
local xRoom = 0
local yRoom = 0
local roomNumber = 1
–> Create grid
local puzzle = display.newGroup()

for yp=1,gridSubdivision do
for xp=1,gridSubdivision do
if pieceCount == lastPiece then
local blackPiece = display.newImage( “black.png”, xRoom, yRoom )
local xBlack = xRoom
local yBlack = yRoom
puzzle:insert( blackPiece )
break end
local room = display.newImage( “room” … roomNumber … “.png”, xRoom, yRoom )
room:addEventListener( “touch”, clickHandler )
puzzle:insert( room )
xRoom = xRoom + step
roomNumber = roomNumber + 1
pieceCount = pieceCount + 1
end
xRoom = 0
yRoom = yRoom + step
end

puzzle.xReference = (step * gridSubdivision) / 2
puzzle.yReference = (step * gridSubdivision) / 2
puzzle.x = display.contentWidth / 2
puzzle.y = display.contentHeight / 2[/code]
The specifications :
When you touch a piece, it checks if there is an empty space. If so, it will move to this empty space.

The questions :

  • How can i control each pieces independently. In the current situation, I can’t do anything with them.
  • When I touch a piece, how to check if there is an empty space and in wich direction?
  • Do I need the blackPiece? It is supposed to represent the empty space.

Any suggestion is welcome.

Michael. [import]uid: 25327 topic_id: 7349 reply_id: 307349[/import]

Ok. I have something working. I will try to add animation when a piece is moving.
Actually, i removed the Black piece but i kept its coordinates as an inspector to check where is the empty space.
Enjoy, feel free to upgrade and share it!

[code]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local gridSubdivision = 3
local lastPiece = (gridSubdivision * gridSubdivision)
local step = 64
local xRoom = 0
local yRoom = 0
local roomNumber = 1
local xBlack = 0
local yBlack = 0
–> Interaction
local function clickHandler( event )
local piece = event.target
if piece.x == xBlack - step and piece.y == yBlack then
piece.x = piece.x + step
xBlack = xBlack - step
elseif piece.x == xBlack + step and piece.y == yBlack then
piece.x = piece.x - step
xBlack = xBlack + step
elseif piece.y == yBlack + step and piece.x == xBlack then
piece.y = piece.y - step
yBlack = yBlack + step
elseif piece.y == yBlack - step and piece.x == xBlack then
piece.y = piece.y + step
yBlack = yBlack - step
else return
end
end
–> Create grid
local puzzle = display.newGroup()

for yp=1,gridSubdivision do
for xp=1,gridSubdivision do
if roomNumber == lastPiece then
xBlack = xRoom
yBlack = yRoom
break end
local room = display.newImage( “room” … roomNumber … “.png”, xRoom, yRoom )
room.xReference = -( step / 2 )
room.yReference = -( step / 2 )
room:addEventListener( “touch”, clickHandler )
puzzle:insert( room )
xRoom = xRoom + step
roomNumber = roomNumber + 1
end
xRoom = 0
yRoom = yRoom + step
end

puzzle.xReference = (step * gridSubdivision) / 2
puzzle.yReference = (step * gridSubdivision) / 2
puzzle.x = display.contentWidth / 2
puzzle.y = display.contentHeight / 2[/code]
[import]uid: 25327 topic_id: 7349 reply_id: 25901[/import]