Today, when I was having a study(free) period, I was asked by a friend to help him with a python program. The program was a text version of Texas holdem poker, and he needed help to do something about identifying the highest card in each person’s hand. After that problem was solved, I decided that a card game would make a good python program, so I decided to make my own, but instead to make a version using BlackJack.

Before I go into the program’s (relatively few) details, this is how I was taught Blackjack. I have absolutely no idea if this is the commonly played version.

There are at least three people involved, one of whom is the dealer. The dealer deals out one card face down to each player, and to themselves. Each person looks at their card, and takes note of how much it is worth. All the numbers 2-10 are worth their face value. Jack, Queen, and King are worth 10. Ace can be worth 1 or 11, whichever the player wants.

The players (but not the dealer) place bets on their cards. The dealer goes to the first player and asks if they want another card. If they do, the dealer places a new card face up next to the old card. The player can keep getting new cards until either they stop, or the sum of the card values goes over 21. If it goes over 21, they lose automatically. Once they are finished, the dealer moves onto the next person, then the next, doing the same.

Once that’s done, everyone turns over their first card, to reveal what the sum is of all their cards. The aim is to get as close to 21 as possible, but not over; whoever gets closest wins. Once everyone has revealed their cards, the dealer reveals their card. The dealer then starts dealing to themselves, and also tries to get the highest number possible. If the dealer gets a number larger than your amount, but stays under 21, you lose and have to give your bet to them. If they go under your amount, they pay you the same amount as you bet earlier. If they go over 21, they have to pay everyone out.

Essentially, the aim is to have the sum of your card values be less than 21, but as close to it as possible; if you get beaten by the dealer, you pay them. If you beat them, they pay you, and if they bust everyone gets paid. And that is how I have learnt to play blackjack. Now, to the program.

The first step I had was deciding what segments the game needed. This was my list at the start:

A main function, to tie the program together
A deal function for dealing cards
A bet function to allow you to place a bet
A function to allow you to continue recieving cards
A function to allow you to check your bank balance
A function to play as an opponent
A function to play as a dealer
And then some fancy writing to make the game more playable

Right, to business. This is just the things I have done today – I will put up more stuff when I finish the program. I started with the deal function. Here it is so far:

def deal():
    suit = ['Spades', 'Clubs', 'Hearts', 'Diamonds']
    value = ['Ace', 'Jack', 'King', 'Queen', 10,9,8,7,6,5,4,3,2]
    card = str(random.choice(value)) + ' of ' + random.choice(suit)
    return card

It has a list of suits, and a list of values, which it randomly combines to get a card. It then returns this card.

It’s a pretty simple function, but it works. Next, I have the function to allow you to place a bet:

def bet(n=5):
    global Chips
    Chips = Chips - n
    return n

It reduces your total bank of chips by whatever your bet is, then returns your bet to the main function.

Next is a function to allow you to get new cards from the dealer, until you get till 21 or over.

def hitme():
    raiseCard = True
    while raiseCard == True:
        hit = raw_input('Do you want another card? ')
        if hit.lower() == 'yes':
            newCard = deal()
            global YourcardValue
            if newCard[0] == 'A':
                cardValue = 11
            elif newCard[0] == 'K':
                cardValue = 10
            elif newCard[0] == 'Q':
                cardValue = 10
            elif newCard[0] == 'J':
                cardValue = 10
            elif newCard[0] == '1':
                if newCard[1] == '0':
                    cardValue = 10
            else:
                cardValue = int(newCard[0])
            YourcardValue += cardValue
            print 'You recieve', newCard + '.'
            print 'You now have a total value of ' + str(YourcardValue) + '.'
            if YourcardValue > 21:
                print "You've gone over."
                raiseCard = False
            elif YourcardValue == 21:
                print "You cannot go higher than 21."
                raiseCard = False
        else:
            raiseCard = False

‘raiseCard’ is a variable that controls whether you want new cards – if you stop asking for them, it turns to False when you ask to stop, which halts the function. next, it asks the user if they want a new card. If yes, it uses deal to get a random card, then figures out the value of the card. It then increases the variable ‘YourcardValue’by that amount. This variable keeps track of the sum of your cards values; if it goes over 21, it stops to tell you that you have lost. if it gets to 21, it tells you.

Finally, The main function:

def play():
    card = deal()
    Oppocard = deal()
    global Yourcard
    global YourcardValue
    global Chips
    global Oppcard
    global OppcardValue
    Yourcard = card
    Oppcard = Oppocard
    if Yourcard[0] == 'A':
        cardValue = 11
    elif Yourcard[0] == 'K':
        cardValue = 10
    elif Yourcard[0] == 'Q':
        cardValue = 10
    elif Yourcard[0] == 'J':
        cardValue = 10
    elif Yourcard[0] == '1':
        if Yourcard[1] == '0':
            cardValue = 10
    else:
            cardValue = int(Yourcard[0])
    if Oppcard[0] == 'A':
        OppcardValue = 11
    elif Oppcard[0] == 'K':
        OppcardValue = 10
    elif Oppcard[0] == 'Q':
        OppcardValue = 10
    elif Oppcard[0] == 'J':
        OppcardValue = 10
    elif Oppcard[0] == '1':
        if Oppcard[1] == '0':
            OppcardValue = 10
    else:
            OppcardValue = int(Oppcard[0])
    YourcardValue = cardValue
    print 'You are dealt ' + card + '.'
    print 'Your opponent receives a card.'
    Yourbet = raw_input('How many chips do you want to bet on your card? ')
    if int(Chips) >= int(Yourbet):
        n = bet(int(Yourbet))
        m = Oppbet(random.choice([1,2,3]))
        print 'You bet', n, 'chips on your card.'
        print 'You now have', Chips, 'chips left in the bank.'
        print 'Your opponent bets', m, 'chips on his card.'
        print ''
        hitme()
        print 'Your opponent starts playing.'
    elif int(Chips) < int(Yourbet):
        print "You only have", Chips, "chips in the bank, so we'll bet that instead."
        n = bet(int(Chips))
        if OppChips >=3:
            m = Oppbet(random.choice([1,2,3]))
        else:
            m = Oppbet(1)
        print 'You bet', n, 'chips on your card.'
        print 'You now have', Chips, 'chips in the bank.'
        print 'Your opponent bets', m, 'chips on his card.'
        print ''
        hitme()
        print 'Your opponent starts playing.'
        OppHitme()

This function chooses a random card with the deal function. It then pulls in the global variables (global variables are variables that can be used across functions) to tell it the card, and the card’s value. It sets the card’s value, then tells you which card you were dealt. It asks for you to bet on your card, then uses the bet function. It then makes a randomised bet for the other player, which it tells you. It then starts the function used to ask you if you want to keep taking more cards.

This is only a start to the program, but it seems to work pretty well. Here’s the whole code:

import random

Yourcard = ''
YourcardValue = ''
Chips = 50
OppChips = 50
Oppcard = ''
OppcardValue = 0

def deal():
    suit = ['Spades', 'Clubs', 'Hearts', 'Diamonds']
    value = ['Ace', 'Jack', 'King', 'Queen', 10,9,8,7,6,5,4,3,2]
    card = str(random.choice(value)) + ' of ' + random.choice(suit)
    return card

def play():
    card = deal()
    Oppocard = deal()
    global Yourcard
    global YourcardValue
    global Chips
    global Oppcard
    global OppcardValue
    Yourcard = card
    Oppcard = Oppocard
    if Yourcard[0] == 'A':
        cardValue = 11
    elif Yourcard[0] == 'K':
        cardValue = 10
    elif Yourcard[0] == 'Q':
        cardValue = 10
    elif Yourcard[0] == 'J':
        cardValue = 10
    elif Yourcard[0] == '1':
        if Yourcard[1] == '0':
            cardValue = 10
    else:
            cardValue = int(Yourcard[0])
    if Oppcard[0] == 'A':
        OppcardValue = 11
    elif Oppcard[0] == 'K':
        OppcardValue = 10
    elif Oppcard[0] == 'Q':
        OppcardValue = 10
    elif Oppcard[0] == 'J':
        OppcardValue = 10
    elif Oppcard[0] == '1':
        if Oppcard[1] == '0':
            OppcardValue = 10
    else:
            OppcardValue = int(Oppcard[0])
    YourcardValue = cardValue
    print 'You are dealt ' + card + '.'
    print 'Your opponent receives a card.'
    Yourbet = raw_input('How many chips do you want to bet on your card? ')
    if int(Chips) >= int(Yourbet):
        n = bet(int(Yourbet))
        m = Oppbet(random.choice([1,2,3]))
        print 'You bet', n, 'chips on your card.'
        print 'You now have', Chips, 'chips left in the bank.'
        print 'Your opponent bets', m, 'chips on his card.'
        print ''
        hitme()
        print 'Your opponent starts playing.'
    elif int(Chips) < int(Yourbet):
        print "You only have", Chips, "chips in the bank, so we'll bet that instead."
        n = bet(int(Chips))
        if OppChips >=3:
            m = Oppbet(random.choice([1,2,3]))
        else:
            m = Oppbet(1)
        print 'You bet', n, 'chips on your card.'
        print 'You now have', Chips, 'chips in the bank.'
        print 'Your opponent bets', m, 'chips on his card.'
        print ''
        hitme()
        print 'Your opponent starts playing.'
        OppHitme()
    

def bet(n=5):
    global Chips
    Chips = Chips - n
    return n

def Oppbet(n):
    global OppChips
    OppChips = OppChips - n
    return n

def OppHitme():
    newCard = deal()
    global OppcardValue
    if newCard[0] == 'A':
        cardValue = 11
    elif newCard[0] == 'K':
        cardValue = 10
    elif newCard[0] == 'Q':
        cardValue = 10
    elif newCard[0] == 'J':
        cardValue = 10
    elif newCard[0] == '1':
        if newCard[1] == '0':
            cardValue = 10
    else:
        cardValue = int(newCard[0])
    OppcardValue += int(cardValue)
    print newCard
    print cardValue
    print OppcardValue
        
    
def hitme():
    raiseCard = True
    while raiseCard == True:
        hit = raw_input('Do you want another card? ')
        if hit.lower() == 'yes':
            newCard = deal()
            global YourcardValue
            if newCard[0] == 'A':
                cardValue = 11
            elif newCard[0] == 'K':
                cardValue = 10
            elif newCard[0] == 'Q':
                cardValue = 10
            elif newCard[0] == 'J':
                cardValue = 10
            elif newCard[0] == '1':
                if newCard[1] == '0':
                    cardValue = 10
            else:
                cardValue = int(newCard[0])
            YourcardValue += cardValue
            print 'You recieve', newCard + '.'
            print 'You now have a total value of ' + str(YourcardValue) + '.'
            if YourcardValue > 21:
                print "You've gone over."
                raiseCard = False
            elif YourcardValue == 21:
                print "You cannot go higher than 21."
                raiseCard = False
        else:
            raiseCard = False


play()
Advertisements