shuffling a table

Hello 

I am messing around with a game Match3Gems to help learn the fundamental of lua and corona.  The screenshot will show this.

I am trying to to shuffle the table when the player presses the button, but I am having sum trouble doing this.

The code that fills the table is 
[lua]

local gemsTable = {}

for i = 1, 8, 1 do 

gemsTable[i] = {}

for j = 1, 8, 1 do 
gemsTable[i][j] = newGem(i,j) 

end 
end 
[/lua]

I came across this code online but cant get it working

[lua]

function table.shuffle(t) 
math.randomseed(os.time()) 
assert(t, “table.shuffle() expected a table, got nil”) 
local iterations = #t 
local j 
for i = iterations, 2, -1 do  
j = math.random(i) 
t[i], t[j] = t[j], t[i] 
end 
end 

function shuffleGameButton(event) 
for i = 1, 8, 1 do  
gemsTable[i] = i 
for j = 1, 8, 1 do  
gemsTable[j] =j 
end 
end 
[/lua]

Does anyone know what the best way to get this done

Thanking You

Alan Fletcher

That shuffle code is unreadable the way it is pasted.  Here is what I use:

local function shuffleTable(tablename) local t = tablename function table.shuffle(t) math.randomseed(os.time()) assert(t, "table.shuffle() expected a table, got nil") local iterations = #t local j for i = iterations, 2, -1 do j = math.random(i) t[i], t[j] = t[j], t[i] end end return table.shuffle(t) end

Thanks for the help, this does rearange the table but the display doesnt update, thanks for the help

Correct, this function takes a table, shuffles it, then returns the table with shuffled elements.  It’s up to you to update the UI.

any idea what the best way to do that is

How are you creating the display objects now?  I would create a function that first removes the current display objects (if they exist), then shuffles the table, then creates the new display objects.  Then in your “Shuffle” button code call this function.

[lua]newGem = display.newCircle(-100, i*70-20, 35)[/lua]

and entering into the table via

[lua]

for i = 1, 8, 1 do

        gemsTable[i] = {}

        for j = 1, 8, 1 do

            --gemsTable[i][j]:setReferencePoint(display.CenterReferencePoint)

            gemsTable[i][j] = newGem(i,j)

    

         end

     end

[/lua]

That shuffle code is unreadable the way it is pasted.  Here is what I use:

local function shuffleTable(tablename) local t = tablename function table.shuffle(t) math.randomseed(os.time()) assert(t, "table.shuffle() expected a table, got nil") local iterations = #t local j for i = iterations, 2, -1 do j = math.random(i) t[i], t[j] = t[j], t[i] end end return table.shuffle(t) end

Thanks for the help, this does rearange the table but the display doesnt update, thanks for the help

Correct, this function takes a table, shuffles it, then returns the table with shuffled elements.  It’s up to you to update the UI.

any idea what the best way to do that is

How are you creating the display objects now?  I would create a function that first removes the current display objects (if they exist), then shuffles the table, then creates the new display objects.  Then in your “Shuffle” button code call this function.

[lua]newGem = display.newCircle(-100, i*70-20, 35)[/lua]

and entering into the table via

[lua]

for i = 1, 8, 1 do

        gemsTable[i] = {}

        for j = 1, 8, 1 do

            --gemsTable[i][j]:setReferencePoint(display.CenterReferencePoint)

            gemsTable[i][j] = newGem(i,j)

    

         end

     end

[/lua]