# Blackjack Game (Sample Programs)

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