Hello there i am creating a sliding picture puzzle the problem is every starting of the game it always start at iteration 1,2,3..i used images..i want to start every refresh with randomization logic but i always fail...i want to start the game with automatic shuffle like 3,1,2 or 2,3,1 not 123.. because it already solved the puzzle and i want to show also if the picture is already solved Heres the code..... display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR local gridSubdivision = 2 local lastPiece = (gridSubdivision \* gridSubdivision) local step = 83 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 room = display.newImage(roomNumber .. ".png",xRoom,yRoom) room.xScale=.2 room.yScale=.2 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-80 puzzle.y = display.contentHeight / 2-100 I adopt this code also in this forum and then experimenting some logics i hope you can help me..thanks sirs in advanced. ..hope you cand lend me some logic thank you
Currently you are creating one global object named room and writing over it on each iteration of the loop. This is creating a memory leak but more importantly you don’t have a reference you can use to the previous “room”'s you’ve created.
What you need is a table that holds each room. I’m not going to write out all the code, but something (in pseudo code) would be like:
local rooms = {} local maxRooms = 10 for i = 1, maxRooms do rooms[i] = display.newImage(i .. ".png",xRoom,yRoom) end math.randomseed( os.time() ) local function shuffleTable( t ) local rand = math.random assert( t, "shuffleTable() expected a table, got nil" ) local iterations = #t local j for i = iterations, 2, -1 do j = rand(i) t[i], t[j] = t[j], t[i] end end shuffleTable( rooms )
See this tutorial: https://coronalabs.com/blog/2014/09/30/tutorial-how-to-shuffle-table-items/
Once you have the table sorted, you can loop over it and position the objects. In the main loop, you will want to add your touch handlers and all your other things you’re doing.
Rob
Currently you are creating one global object named room and writing over it on each iteration of the loop. This is creating a memory leak but more importantly you don’t have a reference you can use to the previous “room”'s you’ve created.
What you need is a table that holds each room. I’m not going to write out all the code, but something (in pseudo code) would be like:
local rooms = {} local maxRooms = 10 for i = 1, maxRooms do rooms[i] = display.newImage(i .. ".png",xRoom,yRoom) end math.randomseed( os.time() ) local function shuffleTable( t ) local rand = math.random assert( t, "shuffleTable() expected a table, got nil" ) local iterations = #t local j for i = iterations, 2, -1 do j = rand(i) t[i], t[j] = t[j], t[i] end end shuffleTable( rooms )
See this tutorial: https://coronalabs.com/blog/2014/09/30/tutorial-how-to-shuffle-table-items/
Once you have the table sorted, you can loop over it and position the objects. In the main loop, you will want to add your touch handlers and all your other things you’re doing.
Rob