# Need logic help (BlackJack)

A Card games logic.
First here is the Logic of the Game.

``````- - BlackJack Planning Phase
- - Dealer Cards
- - Player Cards
- - Deal the Cards
- - Display the Cards
- - Sum of the Dealer
- - Sum of the Player
- - Compare the sum of the Cards, D vs. P
- - The Rules
- - if P cards sum is > 21 = Bust
- - if P cards sum is < 21 = Option : Hit or Stay
- - if Option P Stay compare of, D vs. P
- - if P sum is < 2 and > D sum then P Wins !
- - if P sum is < D sum then P loses !
``````

@kendog400 There are plenty of card game examples on the forum. Do a forum search to see what you can find.

@kendog400 Here’s a start for your card game. Some code to create a deck of cards. You can alter this to create what you want.

``````supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
rectMode(CENTER)
creatDeck()
end

function draw()
background(207, 228, 211, 255)
for value=1,13 do
for suit=1,4 do
sprite(cards[suit][value],value*70,HEIGHT-suit*150)
end
end
end

function creatDeck()
cards={}
local pic={"??","??","??","??"}
local val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
for s=1,4 do
cards[s]={}
for v=1,13 do
img=image(50,70)
setContext(img)
fill(255)
stroke(0, 235, 255, 255)
strokeWidth(3)
rect(25,35,50,70)
sprite("SpaceCute:Star",25,35,75)
fontSize(13)
text(pic[s],12,57)
text(pic[s],38,13)
fill(255,0,0)
fontSize(25)
text(val[v],25,35)
setContext()
cards[s][v]=img
end
end
end
``````

Thank You…

@kendog400 Here’s a version based on what you show above. It’s a total waste of Codea’s capabilities, but it can be done. Press “h” for hit, “s” for stay, and “a” to play again. It’s in portrait mode and you should slide the output window all the way up to show all of the print statements. I think it runs without errors, but I didn’t test it that well.

EDIT: Made minor change for dealer.

``````supportedOrientations(PORTRAIT_ANY)

function setup()
showKeyboard()
output.clear()
player,dealer={},{}
for z=1,2 do
table.insert(player,math.random(11))
table.insert(dealer,math.random(11))
end
showPlayer(1)
end

function draw()
background(0)
fill(255)
if ans=="s" then
output.clear()
showDealer()
showPlayer(2)
showWinner()
elseif ans=="h" then
output.clear()
table.insert(player,math.random(11))
showPlayer(1)
elseif ans=="a" then
setup()
end
ans=""
end

function showDealer()
dealerSum=0
for a=1,#dealer do
dealerSum=dealerSum+dealer[a]
end
while dealerSum<17 do
dealerSum=0
table.insert(dealer,math.random(11))
for a=1,#dealer do
dealerSum=dealerSum+dealer[a]
end
end
print("The dealer has a total of "..dealerSum)
print("from these cards "..table.concat(dealer," "))
end

function showPlayer(a)
playerSum=0
for a=1,#player do
playerSum=playerSum+player[a]
end
print("You have a total of "..playerSum)
print("from these cards "..table.concat(player," "))
if playerSum>21 then
showWinner()
elseif a==1 then
print("\
\
Do you want to stay(s) or hit(h)")
end
end

function showWinner()
if playerSum>21 then
print("Dealer wins!")
elseif dealerSum>21 then
print("You win!")
elseif playerSum>dealerSum then
print("You win!")
else
print("Dealer wins!")
end
print("Play again?(a)")
end

function keyboard(k)
ans=k
end
``````

This is a snapshot picture, notice the Royality Image (Queen). This will be replaced with the picture of the card suit…

@kendog400 You have the correct logic, but some of your values are wrong. Here’s an updated version.

``````    if numAces==1 then
if myValue+11>21 then
myValue=myValue+1
else
myValue=myValue+11
end
elseif numAces==2 then
if myValue+12>21 then
myValue=myValue+2
else
myValue=myValue+12
end
elseif numAces==3 then
if myValue+13>21 then
myValue=myValue+3
else
myValue=myValue+13
end
elseif numAces==4 then
if myValue+14>21 then
myValue=myValue+4
else
myValue=myValue+14
end
end
``````

@kendog400 You’re missing code. The above code has errors.

Here is the PGM. Its a Main and three Classes…

The card game is from Ignaz…

@kendog400 When you post code, use ~~~ before and after your code. You’re missing code at the end of the card class and you have errors in the other code.

self.suits={“??”,”??”,”??”,”??”} Not sure what’s happening. I thought maybe you couldn’t show the emojis, but they work for me.