"this application has been corrupted" - math.random problem

Hello

I get this error when I put my app. on my optimus 2x. I have searched google and checked the other topics about it, but I can’t figure out what it is (my images names are all lower case and same in the code).

It’s a simple slidepuzzle game:

[lua]display.setStatusBar( display.HiddenStatusBar ) – Fjern statusbar

local gridSubdivision = 3 – Antal felter på række - 3*3 = 9 felter i alt
local lastPiece = (gridSubdivision * gridSubdivision) – Den bliver brugt til sidste felt, som er et sort felt, dvs. det felt man kan rykke brikker hen på.
local step = 128 – Sidelængde på billede - ‘ryk 128 pixels i en retning’
local xRoom = 0 – x-koordinat for billederne
local yRoom = 0 – y-koordinat for billederne
local roomNumber = 1 – Hvert billedes nummer, som så bliver brugt til at placere det i et “rum” i griddet
local xBlack = 256 – x-koordinat for det sorte felt
local yBlack = 256 – y-koordinat for det sorte felt
local countFinish = 0

–> Opstil grid

local myText = display.newText("", 100, 100, native.systemFont, 32) – Brugt til fejlsøgning
myText:setTextColor(255, 255, 255) – Samme som ovenover

local puzzle = display.newGroup()

puzzle.xReference = (step * gridSubdivision) / 2
puzzle.yReference = (step * gridSubdivision) / 2
puzzle.x = display.contentWidth / 2 – Placere spillet i midten af x-aksen
puzzle.y = display.contentHeight / 2 – Placere spillet i midten af y-aksen

local ledige = {} – Array der fyldes med tal fra 1-8, som er det antal brikker der er, bruges senere til at placere brikkerne på tilfældige pladser
for r=1, lastPiece-1 do
table.insert (ledige, r)
end
local realnum = {} – Array der fyldes med tal fra 1-8, som er det antal brikker der er, bruges til at kontrollere om brikkerne sidder korrekt
for r=1, lastPiece-1 do
table.insert (realnum, r)
end

local randnum = {} – Array der fyles med “lokationerne” til de tilfældigt placerede brikker

local winnerText = display.newText("", 100, 100, native.systemFont, 32)
winnerText:setTextColor(255, 255, 255)

function evaluate () – Tjekker om man har placeret brikkerne korrekt
countFinish = 0
for b=1, lastPiece-1 do
if randnum[b] == realnum[b] then
countFinish = countFinish+1
end
end
if countFinish == lastPiece-1 then
winnerText.text = “Tillykke du har klaret den!”
end
end

–> Interaktion
local function clickHandler( event ) – Funktion for når der bliver trykket på en brik og rykker den i den rigtige retning og samtidig tjekker om brikkerne er placeret korrekt
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

evaluate()

end

for yp=1,gridSubdivision do
for xp=1,gridSubdivision do

local randRoom = math.random (#ledige) – Tilfældigt nummer af arrayet 1-8
roomNumber = ledige[randRoom] – Værdien til det valgte nummer fra ovenover

if roomNumber == lastPiece then
xBlack = xRoom
yBlack = yRoom
break end

local room = display.newImage( “images/room” … roomNumber … “.jpg”, xRoom, yRoom )
room.xReference = -( step / 2 )
table.insert (randnum, roomNumber) – Her bliver der indsat de værdier for tilfældigt placerede brikker i arrayet “randnum”
room.yReference = -( step / 2 )

puzzle:insert( room )
xRoom = xRoom + step
room:addEventListener( “touch”, clickHandler )
table.remove (ledige, randRoom) – Fjerne nummeret fra arrayet, så det ikke kan vælges igen
end

xRoom = 0
yRoom = yRoom + step
end[/lua]

If I remove the last bit with the loops the app works (no pictures/game though), so it could be something in the code there I have screwed up. (it works fine in the simulator)

Thanks for your help!

LeCattez [import]uid: 142583 topic_id: 27056 reply_id: 327056[/import]

Small update. I installed corona on my windows computer and noticed the simulator output where it comes up with two runtime errors:

Runtime error ...\main.lua:80: bad argument #1 to 'random' (interval is empty) stack traceback: [C]: ? [C]: in function 'random' ...\main.lua:80: in main chun k Runtime error: ...\main.lua:80: bad a rgument #1 to 'random' (interval is empty) stack traceback: [C]: ? [C]: in function 'random' ...\main.lua:80: in main chun k

I suppose that’s the error? (when I removed the same part as before from the code, it doesn’t show)

LeCattez

Edit: Hmm, or maybe just the random generator… Is there a problem taking something from the whole array by doing “#array”? If yes, what’s the alternative? [import]uid: 142583 topic_id: 27056 reply_id: 109843[/import]

Can it be the math.random(#ledige) which is the problem for that error? (besides the runtime errors)

Of what I could see I have to write a low number and high number in the math.random method, but it didn’t help to try with (1, #ledige), so I guess I have to change my random code completely? [import]uid: 142583 topic_id: 27056 reply_id: 109893[/import]

try

print ("num: "… #ledige)

what does it print to the console?
because “bad argument #1 to ‘random’ (interval is empty)” sounds like #ledige is nil…
-finefin [import]uid: 70635 topic_id: 27056 reply_id: 109901[/import]

The game does work, so there shouldn’t be a problem with the array of what I know (it does randomize in the simulator):

Copyright (C) 2009-2011 A n s c a , I n c . Version: 2.0.0 Build: 2011.704 num: 8 num: 7 num: 6 num: 5 num: 4 num: 3 num: 2 num: 1 Runtime error ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:80: bad argument #1 to 'random' (interval is empty) stack traceback: [C]: ? [C]: in function 'random' ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:80: in main chun k Runtime error: ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:80: bad a rgument #1 to 'random' (interval is empty) stack traceback: [C]: ? [C]: in function 'random' ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:80: in main chun k [import]uid: 142583 topic_id: 27056 reply_id: 109905[/import]

try math.random( (#ledige) ) [import]uid: 7911 topic_id: 27056 reply_id: 109926[/import]

Eventually the ledige table becomes empty and math.random(0) is an undefined case I believe.

Search the forums for “Shuffle”, which is what I think you’re trying to do. [import]uid: 19626 topic_id: 27056 reply_id: 109929[/import]

try math.random( (#ledige) )

That didn’t do anything different.

**Eventually the ledige table becomes empty and math.random(0) is an undefined case I believe.

Search the forums for “Shuffle”, which is what I think you’re trying to do.**

Hmm, it’s only set to run between 1 and 8 in the loop though. (8 numbers in the array)

I read about the shuffling and had in mind that would solve it, I just have to figure out how to implement it hehe.

That’s the way to pick a random number in the array?

LeCattez
[import]uid: 142583 topic_id: 27056 reply_id: 109963[/import]

You *can* use that shuffle function, but in my opinion it’s absolutely okay to do what you’re trying to do.
so, when I do

local rndm = math.random(0)  

it generates exactly the error you posted.
you can try something like this

local randRoom  
if #ledige \> 0 then  
 randRoom = math.random (#ledige)  
else  
 print ("array is empty")  
end  

-finefin [import]uid: 70635 topic_id: 27056 reply_id: 109982[/import]

I though that error meant that something had written or changed a file in the application bundle, so that it no longer had the same checksum as the original?
[import]uid: 108660 topic_id: 27056 reply_id: 109983[/import]

[lua]for yp=1,gridSubdivision do
for xp=1,gridSubdivision do

local randRoom
if #ledige > 0 then
randRoom = math.random (#ledige)
else
print (“array is empty”)
end

–local randRoom = math.random (#ledige) – Tilfældigt nummer af arrayet 1-8
roomNumber = ledige[randRoom] – Værdien til det valgte nummer fra ovenover
if roomNumber == lastPiece then
xBlack = xRoom
yBlack = yRoom
break end

local room = display.newImage( “images/room” … roomNumber … “.jpg”, xRoom, yRoom )
room.xReference = -( step / 2 )
table.insert (randnum, roomNumber) – Her bliver der indsat de værdier for tilfældigt placerede brikker i arrayet “randnum”
room.yReference = -( step / 2 )

puzzle:insert( room )
xRoom = xRoom + step
room:addEventListener( “touch”, clickHandler )
table.remove (ledige, randRoom) – Fjerne nummeret fra arrayet, så det ikke kan vælges igen
end

xRoom = 0
yRoom = yRoom + step
end[/lua]

I tried to place it in there and get this error instead (it does say array is empty):

Copyright (C) 2009-2011 A n s c a , I n c .  
 Version: 2.0.0  
 Build: 2011.704  
array is empty  
Runtime error  
 ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:115: attempt to  
concatenate local 'roomNumber' (a nil value)  
stack traceback:  
 [C]: ?  
 ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:115: in main chu  
nk  
Runtime error: ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:115: atte  
mpt to concatenate local 'roomNumber' (a nil value)  
stack traceback:  
 [C]: ?  
 ... litteratur\modul 9\eksamensopgave\app-kopi\main.lua:115: in main chu  
nk  

Looks like I have much to learn hehe.

Edit: The runtime error in line 116 is line 18 in the above code snippet.
Printing roomNumber also shows 8 different numbers which is all good. [import]uid: 142583 topic_id: 27056 reply_id: 109984[/import]

sure, if #ledige == 0 randRoom stays nil.
I don’t know exactly how your game works, but I see at least two options:

  • give randRoom a default value
  • put all the code inside the “if #ledige > 0 then” statement
  • do both:
for yp=1,gridSubdivision do  
 for xp=1,gridSubdivision do  
  
 local randRoom = 1  
 if #ledige \> 0 then  
 randRoom = math.random (#ledige)  
  
 roomNumber = ledige[randRoom]  
 if roomNumber == lastPiece then  
 xBlack = xRoom  
 yBlack = yRoom  
 break   
 end  
   
 local room = display.newImage( "images/room" .. roomNumber .. ".jpg", xRoom, yRoom )  
 room.xReference = -( step / 2 )  
 table.insert (randnum, roomNumber)   
 room.yReference = -( step / 2 )  
   
 puzzle:insert( room )  
 xRoom = xRoom + step  
 room:addEventListener( "touch", clickHandler )  
 table.remove (ledige, randRoom)   
 else  
 print ("array is empty")  
 end  
 end  
 xRoom = 0  
 yRoom = yRoom + step  
end  

hope this helps

-finefin [import]uid: 70635 topic_id: 27056 reply_id: 109989[/import]

Ah that did help thanks! (will just have to test on my phone to be sure)

The game is a very simple puzzle sliding game, where you have to move the pieces around to get the original picture.
I had to pick a place to start and I hoped it wasn’t too difficult which it turned out to be afterall hehe.

Edit: It does work on the phone as well. [import]uid: 142583 topic_id: 27056 reply_id: 109994[/import]

Is it easy to make the pieces dragable instead of touchable? As it is now I have to click each piece and it moves to the destination.
I have an idea that it’s quite complicated to do that, but I suppose I can give it a shot.

I have to use the event.phase began/moved/ended right? [import]uid: 142583 topic_id: 27056 reply_id: 109997[/import]

In the original post, there are 2 nested for loops that iterate 9 times removing 8 items from the array. On the 9th pass that table is empty and you end up requesting a random number of zero.

I recommended the shuffle routine so you could randomize the table with it, then simply loop through that list to draw your tiles. I think it would be a little cleaner, but you’re basically doing that anyway. [import]uid: 19626 topic_id: 27056 reply_id: 109998[/import]