@Tyson Here’s a single hand of 5 card poker. It goes thru one deck (5 cards, 10 hands) before it reshuffles the deck. Tap the screen for a new hand. It also shows you the result of each hand which could be “High Card”, “1 Pair”, “2 Pair”, “3 of a Kind”, “Straight”, “Flush”, “Full House”, “4 of a Kind”, “Straight Flush”, “Royal Flush”. If you look at the function check(), you’ll see that it doesn’t require 2 million “if” statements as mentioned above, just 19 and a table to determine the combination of the hand. I suggest that you learn about tables. They’re very important and can save you tons of code. This probable doesn’t do exactly what you want, but I figured you would want the check() function to determine the combination of the hand and this code shows it’s use. Look over the code and if you have any questions let me know. I’ll go into more detail on any section you request.
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
font("Courier-Bold")
fontSize(30)
function setup()
str="" -- matches
hand={} -- table of current hand
suit={"Diamonds","Hearts","Spades","Clubs"}
value={" 2"," 3"," 4"," 5"," 6"," 7"," 8",
" 9","10","Jack","Queen","King","Ace"}
setup1()
end
function setup1()
shuf="New deck"
sc=shuffleCards() -- shuffle cards
r=0 -- card offset
setTable({sc[r+1],sc[r+2],sc[r+3],sc[r+4],sc[r+5]})
check() -- check current hand
end
function draw()
background(40,40,50)
fill(255)
text(shuf,WIDTH/2,HEIGHT-50)
text("Tap screen for next hand",WIDTH/2,HEIGHT-100)
show() -- show the current hand
end
function nextHand()
r=r+5
if r>49 then -- new deck
setup1()
else
setTable({sc[r+1],sc[r+2],sc[r+3],sc[r+4],sc[r+5]})
check()
end
end
function touched(t)
shuf=""
if t.state==ENDED then
nextHand()
end
end
function setTable(cards)
-- set table values to 0
for x=1,13 do
hand[x]={}
for y=1,4 do
hand[x][y]=0
end
end
-- update table with cards
for x=1,5 do
local s=math.floor((cards[x]-1)/13)+1
local v=(cards[x]-1)%13+1
hand[v][s]=1
end
end
function show()
pushStyle()
textMode(CORNER)
local cnt=0
for x=13,1,-1 do
for y=1,4 do
if hand[x][y]==1 then
cnt=cnt+1
local card=string.format("%5s of %s",value[x],suit[y])
text(card,370,HEIGHT-250-cnt*40)
end
end
end
popStyle()
text(str,WIDTH/2,HEIGHT-600)
end
function check() -- determine ranking of current hand
-- clear flags
local same2,same3,same4=0,0,0
local flush,straight,royal=0,0,0
local v2,v3,v4
-- check for pairs, 3 of a kind, 4 of a kind, full house
for x=1,13 do
v2=hand[x][1]+hand[x][2]+hand[x][3]+hand[x][4]
v3=hand[x][1]+hand[x][2]+hand[x][3]+hand[x][4]
v4=hand[x][1]+hand[x][2]+hand[x][3]+hand[x][4]
if v2==2 then
same2=same2+1
end
if v3==3 then
same3=1
end
if v4==4 then
same4=1
end
end
-- check for flush
local s1,s2,s3,s4=0,0,0,0
for x=1,13 do
s1=s1+hand[x][1]
s2=s2+hand[x][2]
s3=s3+hand[x][3]
s4=s4+hand[x][4]
end
if s1==5 or s2==5 or s3==5 or s4==5 then
flush=1
end
-- check for straight
local sum=0
local x=13 -- start with Ace for low straight
if hand[x][1]==1 or hand[x][2]==1 or hand[x][3]==1 or hand[x][4]==1 then
sum=sum+1
end
for x=1,13 do
if hand[x][1]==1 or hand[x][2]==1 or hand[x][3]==1 or hand[x][4]==1 then
if sum==0 and x==9 then
royal=1 -- possible royal 10,J,Q,K,A
end
sum=sum+1
if sum==5 and x==4 then
straight=1 -- Ace,2,3,4,5
end
else
if sum<5 then -- out of sequence
sum=0
end
royal=0 -- clear royal
end
end
if sum==5 then
straight=1
end
-- update str with results of hand
str="High card"
if same2==1 then
str="1 Pair"
end
if same2==2 then
str="2 Pair"
end
if same3==1 then
str="3 of a kind"
end
if straight==1 then
str="Straight"
end
if flush==1 then
str="Flush"
end
if same2==1 and same3==1 then
str="Full House"
end
if same4==1 then
str="4 of a kind"
end
if flush==1 and straight==1 then
str="Straight Flush"
end
if flush==1 and royal==1 then
str="Royal Flush"
end
end
function shuffleCards()
local d1, d2 = {}, {}
for z=1,52 do
d1[z]=z -- fill table with numbers 1 to 52
end
for z=1,52 do
local r=math.random(1,#d1) -- get a random number from table d1
table.insert(d2,d1[r]) -- insert it in table d2
table.remove(d1,r) -- remove it from table d1
end
return d2 -- return shuffled deck
end