or like this
cards={}
for i=1,52 do cards[i]={i,math.random()} end
table.sort(cards, function(a,b) return a[2]<=b[2] end)
for i=1,52 do print(cards[i][1]) end
or like this
cards={}
for i=1,52 do cards[i]={i,math.random()} end
table.sort(cards, function(a,b) return a[2]<=b[2] end)
for i=1,52 do print(cards[i][1]) end
Or you can do this.
tab={} for z=1,52 do tab[z]=z end
while #tab>0 do n=math.random(#tab) print(tab[n]) table.remove(tab,n) end
You realise we’re going to get into trouble with our resident mathematician for not using the sort method with three mathematicians’ names attached?
Thanks chaps it’s back to the drawing board. It’s hard to find the alternatives that you suggest by reading the ‘manual’ . Most patience games need a shuffled pack to start with.
@Steep Here’s something for you to look over. Calling the function shuffle(52) creates a table with the numbers 1 to 52 in it. Calling shuffle(0) returns a random number from the table and the number of cards left in the deck. When the number left is 0, then the deck is reshuffled. Tap the screen to show a random number from the deck.
function setup()
fontSize(40)
shuffle(52)
card=""
end
function draw()
background(0)
fill(255)
text("Tap screen for next card",WIDTH/2,HEIGHT/2+250)
text(card,WIDTH/2,HEIGHT/2)
text(shuf,WIDTH/2,HEIGHT/2-200)
end
function touched(t)
if t.state==BEGAN then
card,left=shuffle(0) -- card number and remaining card count
if left==0 then -- no cards left, reshuffle deck
shuffle(52)
end
end
end
function shuffle(size)
if size>0 then -- size >0, create table of 52 numbers
cardTab={}
for z=1,size do
cardTab[z]=z
end
shuf="shuffled deck"
elseif #cardTab>0 then -- get a random number from table
shuf=""
n=math.random(#cardTab) -- create random number
local card=cardTab[n] -- get card number
table.remove(cardTab,n) -- remove it from table
return card,#cardTab -- return card number and number of remaining cards
end
end
Enjoyed the code Dave. Added to my learning curve.
I have your simple cards in my Assets:documents, so I changed the ‘card’ value from string to 0 (zero) in the setup function. I then added the two lines :-
str=string.format(“Documents:card%02d”,card)
sprite(str,450,400)
to the Draw function. This gives number/card relationship.
Next step is to put face down pack and touch pack to deal 4 cards top of screen.
these cards will be removed under set conditions, when move is complete 4 more cards are dealt.
This code will produce a display of the numbers 1 to 52 in random and in in four columns. I have simple cards as sprites in my Assets/Documents store thanks to Dave. They are called(card01 to card52). However when I try to swap the numbers with the sprites the randomness breaks down. I have no idea why.
Any help please ?
-- Code to display 52 random numbers.
backingMode(RETAINED)
function setup()
tab = {}
for no = 1,52 do
table.insert(tab,no)
no = no+1
end
tmr = 0 -- initiate timer
nos = 52 -- number of numbers in Table (Tab)
x = 280
y = 700
fontSize(48)
fill(256)
end
function draw()
tmr=tmr+1
if tmr>40 and x<=580 and nos>0 then
tmr=0
local n=math.random(nos)
text((tab[n]),x,y). -- } I have replaced this line with
-- str=string.format("Documents:card%02d",n) } this line and the line below.
-- sprite(str,x,y) }The randomness breaks down
table.remove(tab,n)
nos=nos-1
x=x+100
if x>580 then
y=y-50
x=280
end
end
end
I think it should be
str=string.format("Documents:card%02d",tab[n])
but it doesn’t make much sense to keep loading all 52 images from disk, 60 times a second.
I would load them all into a table of sprites in setup, and show the relevant sprite in draw, eg
sprite(TableOfSprites[n],x,y)
Thanks Ignatz. It’s the way you think. Putting tables and the Draw function together is something I find a bit tricky. Not sure where I will go when I investigate touch to initiate the deal. It just seemed to me that having got the numbers to do what I want and sprites where I wanted them, then I should be able to make the switch and check that things were ok up to this point.
My way of thinking is not always the best way for Lua, Codea and the iPad.
You’ll get used to it, like I did, just stick at it.
You should also try to avoid hard coding anything. So if you want your pack to be dealt starting at (say) (50, HEIGHT-50), with a gap of (50,75) pixels between cards, then I might write it like this (untested)
--in setup
DealStartPos=vec2(50, HEIGHT-50)
DealSpacing=vec2(50,75)
--write this function to give you the screen position for a card
function DealPos(col,row)
local x=DealStartPos.x+(col-1)*DealSpacing.x
local y=DealStartPos.y+(row-1)*DealSpacing.y
return x,y
end
--then in draw
--we want to draw card `n` in col `c`, row `r`
local x,y=DealPos(c,r)
sprite(TableOfSprites[n],x,y)
Using this approach, you can define your playing surface and card positions, and if you ever have to change anything, it is only in one place, in setup.
Thanks again Ignatz but you have taken quite a big step for my simple mind. #:-S
Not sure what you mean by ‘hard coding’ and I need to take a look at sprite(TableOfSprites[n],x,y) before moving any further. My game ‘Kings High’ will require the use of Sprites if I am to use ‘Proper Cards’
I mean, don’t include specific pixel positions when drawing sprites in draw, ie don’t write sprite(someCard, 50+x*75,10+y*60)
Rather define all those numbers as variables in setup, and use the variables to draw the sprites. Then it’s easy to change the code.
Message received and understood. L-)
@Steep I know you’re not to this point yet, but I thought I would give you a peek of where you’re headed. You can look this over in more detail whenever you want to see what you might need in your code.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
rectMode(CENTER)
img={}
msg="Tap screen to deal a card."
card=0
tab = {} -- numbers for random cards
for no = 1,52 do
table.insert(tab,no)
end
tab2={} -- card on screen
x1 = 120
y1 = 900
fontSize(48)
end
function draw()
background(0)
fill(255,0,0)
rect(WIDTH/2,100,60,80) -- draw back of card for draw
fill(255)
text(msg,WIDTH/2,200) -- draw deal message
for a,b in pairs(tab2) do -- draw cards on the screen
sprite(img[a],b.y,b.z)
end
if card>0 then
sprite(str,c.x,c.y) -- draw moving card
end
end
function touched(t)
if t.state==BEGAN and #tab>0 and card==0 then
n=math.random(#tab) -- random number from 1 to table size
card=tab[n] -- get card number from table
table.remove(tab,n) -- remove that number from the table
str=string.format("Documents:card%02d",card) -- format card
table.insert(img,str) -- insert card image in table
x1=x1+100 -- calculate x,y position for card
if x1>520 then
y1=y1-50
x1=220
end
moveCard() -- call the tween routine to move card
end
end
function moveCard()
c={x=WIDTH/2,y=100} -- starting card position
tween(1,c,{x=x1,y=y1},tween.easing.linear,moveDone) -- start tween
msg="" -- card moving, clear message string
end
function moveDone()
table.insert(tab2,vec3(card,c.x,c.y)) -- put card in position table
card=0 -- set card number to 0
msg="Tap screen to deal a card." -- card stopped, set message string
if #tab==0 then
msg="End of deck."
end
end
Dave, that’s brilliant. It will greatly speed up my learning curve. I see that you, like Ignatz, move on to vectors for locating the cards, no doubt to give that smooth movement from Deck to Stack. We now have a shuffled deck displayed to show the complete shuffled Deck. In my game the deal has to be 4 cards, one to each column, when necessary and the game will require 4 tables, one for each column to record the location of cards throughout the game. My next move would be to limit the ‘Touch to deal’ area to the red rectangle that represents the Deck.