Thank you, Jason!))
Your post has been most helpful to me! Thank you very much)
And I have one more question. Could you, please, advise me on how to write the function that checks the 15 puzzle game for victory?
I’ve been trying to write it this and that way, but it wouldn’t work =(
I call my function at the end of your “B” function, so that I could check the state of all buttons on the board, but I don’t seem to understand how to correctly extract the coordinates of every button on the board at an exact moment so that I could check if they all are in the right places.
I’m looking forward to your reply) Thank you in advance!!)
Here’s my function:
local function Check4Victory(j) if (math.round(j[1].x) == a) and (math.round(j[1].y) == a) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[2].x) == b) and (math.round(j[2].y) == a) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[3].x) == c) and (math.round(j[3].y) == a) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[4].x) == d) and (math.round(j[4].y) == a) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[5].x) == a) and (math.round(j[5].y) == b) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[6].x) == b) and (math.round(j[6].y) == b) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[7].x) == c) and (math.round(j[7].y) == b) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[8].x) == d) and (math.round(j[8].y) == b) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[9].x) == a) and (math.round(j[9].y) == c) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[10].x) == b) and (math.round(j[10].y) == c) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[1].x) == c) and (math.round(j[11].y) == c) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[12].x) == d) and (math.round(j[12].y) == c) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[13].x) == a) and (math.round(j[13].y) == d) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[14].x) == b) and (math.round(j[14].y) == d) then checkSquaresPos = checkSquaresPos + 1 end if (math.round(j[15].x) == c) and (math.round(j[15].y) == d) then checkSquaresPos = checkSquaresPos + 1 end if checkSquaresPos == 15 then local wSplash = display.newImage( "winSplash.png", true ) wSplash.x = Wd; wSplash.y = Hg+180; goWinSoundID = audio.loadSound("tada.mp3") goWinSound = audio.play( goWinSoundID ) physics.pause() end return true end
Hi mrjeenb3,
I think I might approach this in a slightly different way than you did above, actually. To start with, assign an “answer” sub-table to each tile, with X and Y values that correspond to the position the tile would be in, were the puzzle finished correctly. Something like:
[lua]
tile.answer= {x=40, y=40}
[/lua]
Then, every time the user lifts their finger off of a tile (triggered when event.phase==“ended” or event.phase==“cancelled”), you can run a compact Check4Victory function that sees if the tiles are in the right spot (or within a few pixels of the right spot, to allow for slight deviation from the exact winning X and Y position. (Keep in mind that the function below will not actually work - you’d need to change it to accurately reflect the table that contains all your tiles - plus I haven’t actually tested it in the simulator. There might be some little errors - I’m just “winging it” here):
[lua]
function Check4Victory()
for i=1, #tileTable do
local tile = tileTable[i]
local xOffset = math.abs(tile.x - tile.answer.x)
local yOffset = math.abs(tile.y - tile.answer.y)
if xOffset>5 or yOffset>5 then return true end --stops the loop if a tile is out of position
– show winning notification if all 15 tiles are in the right place
if i==#tileTable then
local wSplash = display.newImage( “winSplash.png”, true )
wSplash.x = Wd;
wSplash.y = Hg+180;
goWinSoundID = audio.loadSound(“tada.mp3”)
goWinSound = audio.play( goWinSoundID )
physics.pause()
end
end
end
[/lua]
I hope this helps!
Best,
Jason
Thank you ever so much 4 your help, Jason )
Wow, that was a quick response!
I’m very grateful to you))
Running away to code @once :)
Hi, Jason.
It’s me again. Sorry for being such a nuisance.
I did as you advised me to. As far as I understood the table ‘tile’ should be used as an auxiliary table for my ‘Check4Victory’ function.
So I initialized a local variable ‘tile’ at first. Then I initialized all the 15 tiles ‘answer’-coordinates:
local tile = {answer = {}} tile.answer[1] = {x = 40, y = 40} tile.answer[2] = {x = 120, y = 40} tile.answer[3] = {x = 200, y = 40} tile.answer[4] = {x = 280, y = 40} tile.answer[5] = {x = 40, y = 120} tile.answer[6] = {x = 120, y = 120} tile.answer[7] = {x = 200, y = 120} tile.answer[8] = {x = 280, y = 120} tile.answer[9] = {x = 40, y = 200} tile.answer[10] = {x = 120, y = 200} tile.answer[11] = {x = 200, y = 200} tile.answer[12] = {x = 280, y = 200} tile.answer[13] = {x = 40, y = 280} tile.answer[14] = {x = 120, y = 280} tile.answer[15] = {x = 200, y = 280}
So the body of my ‘Check4Victory’ function looks like this:
local function Check4Victory(TileSet) for i=1, #TileSet do tile = TileSet[i] local xOffset = math.abs(tile.x - tile.answer.x) local yOffset = math.abs(tile.y - tile.answer.y) if xOffset\>5 or yOffset\>5 then return true end --stops the loop if a tile is out of position -- show winning notification if all 15 tiles are in the right place if i==#TileSet then local wSplash = display.newImage( "winSplash.png", true ) wSplash.x = Wd; wSplash.y = Hg+180; goWinSoundID = audio.loadSound("tada.mp3") goWinSound = audio.play( goWinSoundID ) physics.pause() end end end
*Note: the table ‘TileSet’ is the table which stores the set of actual tiles on the playboard.
However, every time I move a tile during the game, an error pops up in the console window of the simulator. It reads: “Runtime error: in
function ‘Check4Victory’ attempt to index field ‘answer’ (nil value)”.
I can’t figure out what exactly is wrong. Could you please help identify the mistake?
Thank you.
Hi again!
I think you misunderstood a bit - there’s no need to create a new table called “tile” as you showed above. If you have a table called “tileSet” that contains the actual tiles on the playboard, then you should assign each of the tiles in THAT table a “.answer” parameter. That way, your Check4Victory function will check the X and Y positions of each tile against the “answer” X and Y positions to see if they’re where they are supposed to be. Something like what you see below, though it might be easier to add these values at the moment you actually create the tiles:
[lua]
tileSet[1].answer = {x = 40, y = 40}
tileSet[2].answer = {x = 120, y = 40}
tileSet[3].answer = {x = 200, y = 40}
tileSet[4].answer = {x = 280, y = 40}
tileSet[5].answer = {x = 40, y = 120}
tileSet[6].answer = {x = 120, y = 120}
tileSet[7].answer = {x = 200, y = 120}
tileSet[8].answer = {x = 280, y = 120}
tileSet[9].answer = {x = 40, y = 200}
tileSet[10].answer = {x = 120, y = 200}
tileSet[11].answer = {x = 200, y = 200}
tileSet[12].answer = {x = 280, y = 200}
tileSet[13].answer = {x = 40, y = 280}
tileSet[14].answer = {x = 120, y = 280}
tileSet[15].answer = {x = 200, y = 280}
[/lua]
The runtime error you’re getting is because the Check4Victory function is looking for the “.answer” parameter on each tile, but you haven’t set those values as a parameter of the actual tiles. Once you assign those values to the actual tiles, it should work as expected.
I hope that makes sense.
Best,
Jason
Thank you, Jason!))
halo, guys,
i would like to ask one question,
the code above i can only run in the simulator, but not in the iphone
when i install it into iphone, it cannot cut the photo i capture into 15 pieces
Hi Ryan,
I have not revisited this code in quite a while, and it was written before iOS 7 came out - is your device running iOS 7? If I can find the time I will try it on my own device and report back, but let me know what iOS version you are running.
Thanks,
Jason
halo, guys,
i would like to ask one question,
the code above i can only run in the simulator, but not in the iphone
when i install it into iphone, it cannot cut the photo i capture into 15 pieces
Hi Ryan,
I have not revisited this code in quite a while, and it was written before iOS 7 came out - is your device running iOS 7? If I can find the time I will try it on my own device and report back, but let me know what iOS version you are running.
Thanks,
Jason