Blackjack Game (Sample Programs)

by Tomas Fitzpatrick, Ireland, Thursday, April 25, 2013, 03:08 (1523 days ago)

Here's a blackjack game I threw together. Everything made sense when I was doing it and I got over some difficult problems, but there's probably much easier ways to do some of it, so let me know if I made it difficult for myself or anything.

#Blackjack v0.1
#Tomas Fitzpatrick
#25/04/2013

######################################################


# The introduction
input "With whom do I have the pleasure of playing?: ", name$
pause .3
print "Best of luck, " + name$
print ""

# Setting the variables for the game
money = 1000
max = 100
min = 5

begin:
cls

# The initial cards are dealt
gosub nextcard
card1 = nextc
gosub nextcard
card2 = nextc
gosub nextcard
dealer1 = nextc
gosub nextcard
dealer2 = nextc
hand = (card1 + card2)
dhand = (dealer1 + dealer2)

# The player makes their bet
gosub betting

# The cards are announced
print "The Dealer's card is: " + dealer1
print ""
print "Your 1st card is: " + card1
print "Your 2nd card is: " + card2
print "Your Total is: " + hand
print ""

# If either player has 21, it is announced and the game ends
if dhand = 21 and hand <> 21 then
print "Dealer Has Blackjack!"
goto choice
end if
if hand = 21 and dhand <> 21 then
print "You Have Blackjack!"
money = money + (bet * 2)
goto choice
end if
if hand = 21 and dhand = 21 then
print "Draw!"
money = (money + bet)
goto choice
end if


# The player has the option to 'Hit' or to 'Stand'
hitorstand:
input "Would you like to Hit or Stand? (h/s): ", play$
if play$ = "h" then
goto hitme
else
goto dealer
end if

# Hit system
hitme:
gosub nextcard
if nextc = 11 then
gosub oneleven
end if
nextpcard = nextc
hand = hand + nextpcard
print "Your next card is: " + nextpcard
print " Your total is: " + hand
if hand > 21 then
print "Bust!"
goto choice
else
goto hitorstand
end if

# After the player is finished, the dealer plays
dealer:
print ""
print "The Dealer's 2nd card is: " + dealer2
print "The Dealer's Total is: " + dhand
while dhand <= 16 and dhand < hand # The dealer must stand on 17
print ""
pause 1
print "The Dealer takes another card..."
gosub nextcard
if nextc = 11 then
if dhand > 10 then
nextc = 1
end if
end if
nextdcard = nextc
dhand = dhand + nextdcard
print "The Dealer's next card is: " + nextdcard
print "The Dealer's Total is: " + dhand
if dhand > 21 then
print "The Dealer is Bust!"
money = money + (bet * 2)
goto choice
end if
end while

# The winner is announced
print ""
if dhand > hand then
print "The Dealer Wins!"
end if
if hand > dhand then
print "You Win!"
money = money + (bet * 2)
end if
if hand = dhand then
print "Draw!"
money = (money + bet)
end if

# Player gets the option to play again
choice:
print ""
print "Would you like to play again, " + name$ + "?"
input "(Enter 'y' for yes, 'n' for no or 'money' to see your balance): ", replay$
if replay$ = "y" then
goto begin
end if
if replay$ = "n" then
print "You cash up with " + money + " remaining!"
print "Goodbye, " + name$ + "!"
end
end if
if replay$ = "money" then
print money
goto choice
end if

# System to pick playing cards at random (J, Q, K all worth 10 and A worth 11 in Blackjack)
nextcard:
nextc = int(rand * 13) + 1
if nextc = 11 or nextc = 12 or nextc = 13 then
nextc = 10
end if
if nextc = 1 then
nextc = 11
end if
return

# Betting system
betting:
print "Balance: " + money
input "Place your bet: ", bet
if bet > max then
print "The maximum bet is " + max
goto betting
end if
if bet < min then
print "The minimum bet is " + min
goto betting
end if
if bet > money then
print "You only have " + money + " on you..."
goto betting
end if
print ""
money = money - bet
return

oneleven:
input "Use Ace as a 1 or an 11? (Current Total = " + hand + "): ", ace
if ace = 1 then
nextc = 1
else
nextc = 11
end if
return

Blackjack Game

by UglyMike ⌂ @, Brussels, Thursday, April 25, 2013, 08:16 (1523 days ago) @ Tomas Fitzpatrick

Nice! It looks like you've got the mechanics of the game down pat.
You really should take a look at demo pogram "cards_deal5" and try to incorporate the visual/graphical aspect in your game. That would make it so much better!

Blackjack Game

by Tomas Fitzpatrick, Ireland, Thursday, April 25, 2013, 09:51 (1523 days ago) @ UglyMike

Thanks Mike, I'd like to have a visual aspect but was too lazy last night... Can you link to the demo program you mentioned, I can't seem to track it down. Thanks for your help

Blackjack Game

by UglyMike ⌂ @, Brussels, Thursday, April 25, 2013, 13:55 (1523 days ago) @ Tomas Fitzpatrick

Tomas,

It's written by Jim himself and I have it in my Examples-folder with creation date of 30/7/2011

Blackjack Game

by Jim ⌂ @, Russell, KY, Thursday, April 25, 2013, 17:06 (1523 days ago) @ UglyMike

Here are three updated subroutines to draw card faces based on my old example program:

subroutine drawcardback(x,y,w,h)
# draw a moire pattern for the back of a card
call drawcardborder(x,y,w,h)
color blue
for t = 3 to w step 4
line x+2,y+2,x+t,y+h-2
next t
for t = 3 to h step 4
line x+2,y+2,x+w-2,y+t
next t
end subroutine

subroutine drawcardborder(x,y,w,h)
# draw blank card with border
color blue
rect x,y,w,h
color white
rect x+2,y+2,w-4,h-4
end subroutine

subroutine drawcard(x,y,w,h,suite$, card$)
# drawcard - draw a w,h card at the location x,y
# on the screen.
# set suite$ to H D S or C
# and card$ to A 2 3 4 5 6 7 8 9 10 J Q K
#
call drawcardborder(x,y,w,h)
color black
if suite$ = "H" or suite$ = "D" then color red
# stamp suit on card
if suite$ = "H" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0,.2, 0,.4, .1,.7, .4,1, .7,.7, .8,.4, .8,.2, .7,0, .5,0, .4,.1, .3,0, .1,0}
if suite$ = "D" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0, .5, .4, 0, .8, .5, .4, 1}
if suite$ = "S" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0,.5, 0,.6, .1,.7, .3,.6, .2,.9, .2,1, .4,1, .6,1, .6,.9, .5,.6, .7,.7, .8,.6, .8,.5, .7,.3, .4,0, .1,.3 }
if suite$ = "C" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0,.2, .1,.3, .3,.4, .1,.4, 0,.6, .1,.8, .2,.8, .3,.7, .2,1, .6,1, .5,.7, .6,.8, .7,.8, .8,.6, .7,.4, .5,.4, .7,.3, .8,.2, .7,0, .5,0, .4,.1, .3,0, .1,0}
# now show card value
font "tahoma",h/4,100
text x + w/8, y + h/16, card$
end subroutine

You can
call drawcardback(top, left, width, height)
to draw a card's back. Then call
call drawcard(top, left, width, height, suite$, value$)
to draw the card's face.

These functions should scale from small to large without much need for changes. Also I created new stamp arrays for the shapes to make them a bit smoother.

I am looking forward to the completed game.

Jim

Blackjack Game

by Tomas Fitzpatrick, Ireland, Thursday, April 25, 2013, 21:36 (1522 days ago) @ Jim

APOLOGIES FOR THE FOLLOWING WALL OF TEXT

Thanks Jim and Mike for your help so far. As it stands I'm about 90% of the way there, but I fear the other 10% may require a significant rewrite of some of the mechanics (well, significant to me anyway). At the moment:

The game mechanics seem to work perfectly. This includes dealing, betting, hitting, standing, dealer gameplay and win/loss scenarios.

The graphics subroutines (which I copied verbatim from Jim's post - sorry Jim) work a treat, and cards are printed.

Also added a little text to display each player's total and the current money/bet

HOWEVER

As I hadn't originally intended to add any graphics, I have a problem. The system for picking a random card picks a number between 1 and 13 and assigns it to the respective variable. 11, 12, 13 (J, Q, K) become 10 (their value in blackjack) and 1 (A) becomes 11 (with the choice of whether it's a 1 or 11 taking place later). This means that when the cards are printed in the graphics area, they are printed as a 2,3,4,5,6,7,8,9,10 or 11 of their respective suits, whereas they should be A,2,3,4,5,6,7,8,9,10,J,Q,K. Also, I have no way to stop duplicate cards coming up (especially as 10s are very common).

For the first part of the problem, I wanted to create another variable, so that the cards initial value (ie. 12) be stored as it's "playing card value" (ie. Q). However when I do this, I have an issue as the numeric cards can't be placed in a variable for strings and vice versa.

For the second part, I have notions of comparing the randomised card value and suit against all the others in play before saving it to a variable, but I wonder is there an easier way.

These are both TEENSY problems, which I really don't mind too much, but I'd love to get everything right if possible. Is there any way around these problems that doesn't involve any major changes to the mechanics? Or alternatively, is there any changes to the mechanics I should really make, because I've done something the hard way/reinvented the wheel?

As always, thanks for any help and let me know if you see any bugs/things that should be done differently.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


#Blackjack v0.1.1
#Tomas Fitzpatrick
#Graphics subroutines courtesy of Jim
#26/04/2013

######################################################


# The introduction
cls
clg
input "With whom do I have the pleasure of playing?: ", name$
pause .3
print "Best of luck, " + name$ + "!"
pause 1
print ""

# Setting the variables for the game
carddistance = 120
money = 1000
maxbet = 100
minbet = 5

begin:
cls
clg

# The initial cards are dealt
gosub nextcard
card1 = nextc
suitp1$ = nextsuit$
gosub nextcard
card2 = nextc
suitp2$ = nextsuit$
gosub nextcard
dealer1 = nextc
suitd1$ = nextsuit$
gosub nextcard
dealer2 = nextc
suitd2$ = nextsuit$
phand = (card1 + card2)
dhand = (dealer1 + dealer2)

# The player makes their bet
gosub betting

# The cards are announced
print "The Dealer's card is: " + dealer1
call drawcard(0,20,50,100,suitd1$,dealer1)
call drawcardback(60,20,50,100)
print ""
print "Your 1st card is: " + card1
call drawcard(0,160,50,100,suitp1$,card1)
print "Your 2nd card is: " + card2
call drawcard(60,160,50,100,suitp2$,card2)
print "Your Total is: " + phand
gosub ptotal
print ""

# If either player has 21, it is announced and the game ends
if dhand = 21 and phand <> 21 then
print "Dealer Has Blackjack!"
goto choice
end if
if phand = 21 and dhand <> 21 then
print "You Have Blackjack!"
money = money + (bet * 2)
goto choice
end if
if phand = 21 and dhand = 21 then
print "Draw!"
money = (money + bet)
goto choice
end if


# The player has the option to 'Hit' or to 'Stand'
hitorstand:
input "Would you like to Hit or Stand? (h/s): ", play$
if play$ = "h" then
goto hitme
else
goto dealer
end if

# Hit system
hitme:
gosub nextcard
if nextc = 11 then
gosub oneleven
end if
nextpcard = nextc
phand = phand + nextpcard
print "Your next card is: " + nextpcard
print " Your total is: " + phand
call drawcard(carddistance,160,50,100,nextsuit$,nextpcard)
carddistance = carddistance + 60
gosub ptotal
if phand > 21 then
print "Bust!"
goto choice
else
goto hitorstand
end if

# After the player is finished, the dealer plays
dealer:
carddistance = 120
print ""
print "The Dealer's 2nd card is: " + dealer2
print "The Dealer's Total is: " + dhand
color white
rect 60,20,50,100
call drawcard(60,20,50,100,suitd2$,dealer2)
gosub dtotal
while dhand <= 16 and dhand < phand # The dealer must stand on 17
print ""
pause 1
print "The Dealer takes another card..."
gosub nextcard
if nextc = 11 then
if dhand > 10 then
nextc = 1
end if
end if
nextdcard = nextc
dhand = dhand + nextdcard
print "The Dealer's next card is: " + nextdcard
print "The Dealer's Total is: " + dhand
call drawcard(carddistance,20,50,100,nextsuit$,nextdcard)
carddistance = carddistance + 60
gosub dtotal
if dhand > 21 then
print "The Dealer is Bust!"
money = money + (bet * 2)
goto choice
end if
end while

# The winner is announced
print ""
if dhand > phand then
print "The Dealer Wins!"
end if
if phand > dhand then
print "You Win!"
money = money + (bet * 2)
end if
if phand = dhand then
print "Draw!"
money = (money + bet)
end if

# Player gets the option to play again
choice:
gosub ptotal
carddistance = 120
print ""
print "Would you like to play again, " + name$ + "?"
input "(Enter 'y' for yes, 'n' for no or 'money' to see your balance): ", replay$
if replay$ = "y" then
goto begin
end if
if replay$ = "n" then
print ""
print "You cash up with " + money + " remaining!"
print "Goodbye, " + name$ + "!"
end
end if
if replay$ = "money" then
print money
goto choice
end if

# Print the dealer's total to the graphics area
dtotal:
color white
rect 0,0,300,19
color black
font "Tahoma",12,100
text 0,0,"Dealer Total: " + dhand
return

# Print the player's total to the graphics area (Also prints money and current bet)
ptotal:
color white
rect 0,130,300,19
color black
font "Tahoma",12,100
text 0,130,"Your Total: " + phand
color white
rect 0,270,300,19
color black
font "Tahoma",12,100
text 0,270,"Your Money: " + money + " Your Bet: " + bet
return

# System to pick playing cards at random (J, Q, K all worth 10 and A worth 11 in Blackjack)
nextcard:
nextc = int(rand * 13) + 1
if nextc = 11 or nextc = 12 or nextc = 13 then
nextc = 10
end if
if nextc = 1 then
nextc = 11
end if
# This just chooses a suit at random
nextsuit = int(rand * 4) + 1
if nextsuit = 1 then
nextsuit$ = "H"
end if
if nextsuit = 2 then
nextsuit$ = "D"
end if
if nextsuit = 3 then
nextsuit$ = "C"
end if
if nextsuit = 4 then
nextsuit$ = "S"
end if
return

# Betting system
betting:
print "Balance: " + money
input "Place your bet: ", bet
if bet > maxbet then
print "The maximum bet is " + max
goto betting
end if
if bet < minbet then
print "The minimum bet is " + min
goto betting
end if
if bet > money then
print "You only have " + money + " on you..."
goto betting
end if
print ""
money = money - bet
return

# System for choosing if an Ace is a 1 or an 11
oneleven:
input "Use Ace as a 1 or an 11? (Current Total = " + phand + "): ", ace
if ace = 1 then
nextc = 1
else
nextc = 11
end if
return

######################################################
#All following code is Jim's card drawing subroutines
#All credit for the graphics goes to Jim
######################################################

subroutine drawcardback(x,y,w,h)
# draw a moire pattern for the back of a card
call drawcardborder(x,y,w,h)
color blue
for t = 3 to w step 4
line x+2,y+2,x+t,y+h-2
next t
for t = 3 to h step 4
line x+2,y+2,x+w-2,y+t
next t
end subroutine

subroutine drawcardborder(x,y,w,h)
# draw blank card with border
color blue
rect x,y,w,h
color white
rect x+2,y+2,w-4,h-4
end subroutine

subroutine drawcard(x,y,w,h,suite$, card$)
# drawcard - draw a w,h card at the location x,y
# on the screen.
# set suite$ to H D S or C
# and card$ to A 2 3 4 5 6 7 8 9 10 J Q K
#
call drawcardborder(x,y,w,h)
color black
if suite$ = "H" or suite$ = "D" then color red
# stamp suit on card
if suite$ = "H" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0,.2, 0,.4, .1,.7, .4,1, .7,.7, .8,.4, .8,.2, .7,0, .5,0, .4,.1, .3,0, .1,0}
if suite$ = "D" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0, .5, .4, 0, .8, .5, .4, 1}
if suite$ = "S" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0,.5, 0,.6, .1,.7, .3,.6, .2,.9, .2,1, .4,1, .6,1, .6,.9, .5,.6, .7,.7, .8,.6, .8,.5, .7,.3, .4,0, .1,.3 }
if suite$ = "C" then stamp x+(w-h/3*.8)/2, y+h/2, h/3, {0,.2, .1,.3, .3,.4, .1,.4, 0,.6, .1,.8, .2,.8, .3,.7, .2,1, .6,1, .5,.7, .6,.8, .7,.8, .8,.6, .7,.4, .5,.4, .7,.3, .8,.2, .7,0, .5,0, .4,.1, .3,0, .1,0}
# now show card value
font "tahoma",h/4,100
text x + w/8, y + h/16, card$
end subroutine

Blackjack Game

by Tomas Fitzpatrick, Ireland, Saturday, April 27, 2013, 02:09 (1521 days ago) @ Tomas Fitzpatrick

Fixed the problem with the text on the card graphics. Other problem almost fixed. Should be done soon.

RSS Feed of thread
powered by my little forum