Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

shuffling a table
Started by alanfletcher Sep 22 2013 09:20 AM

6 replies to this topic
shuffle game lua corona tables
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

alanfletcher

[GLOBAL: userInfoPane.html]
alanfletcher
  • Starter
  • PipPip
  • 14 posts
  • Member

Hello 

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

 

Attached File  Untitled.png   18.53K   12 downloads

 

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 

local gemsTable = {} for i = 1, 8, 1 do gemsTable[i] = {}for j = 1, 8, 1 do gemsTable[i][j] = newGem(i,j) end end 

 

I came across this code online but cant get it working

 

 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 

 

Does anyone know what the best way to get this done

 

Thanking You

Alan Fletcher



[TOPIC: post.html]
#2

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Starter
  • PipPipPipPipPipPip
  • 651 posts
  • Jedi

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


[TOPIC: post.html]
#3

alanfletcher

[GLOBAL: userInfoPane.html]
alanfletcher
  • Starter
  • PipPip
  • 14 posts
  • Member

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



[TOPIC: post.html]
#4

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Starter
  • PipPipPipPipPipPip
  • 651 posts
  • Jedi

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



[TOPIC: post.html]
#5

alanfletcher

[GLOBAL: userInfoPane.html]
alanfletcher
  • Starter
  • PipPip
  • 14 posts
  • Member

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



[TOPIC: post.html]
#6

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Starter
  • PipPipPipPipPipPip
  • 651 posts
  • Jedi

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.



[TOPIC: post.html]
#7

alanfletcher

[GLOBAL: userInfoPane.html]
alanfletcher
  • Starter
  • PipPip
  • 14 posts
  • Member

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.

newGem = display.newCircle(-100, i*70-20, 35)

and entering into the table via

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



[topic_controls]
[/topic_controls]



Also tagged with one or more of these keywords: shuffle, game, lua, corona, tables