freeribosome
freeribosome

Reputation: 29

How to avoid lots of manual work while making blackjack in python?

I'm currently working on a little project. I'm attempting to make blackjack, in python. I can calculate the sum of the cards, but its a very lengthy process. I have to manually type each and every thing. Is there some way I can make my code shorter? Any suggestions would be appreciated.

import random
stringlist=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
cards=[]
cardssum=0
def deal():
    cards.append(stringlist[random.randrange(0,13)])
    cards.append(stringlist[random.randrange(0,13)])
    print "First hand :"+str(cards)
blackjack="false"
def blackjack1(cards):
    if cards[0]=="A" and cards[1]=="K":
        print "BlackJack!"
        blackjack="true"
    elif cards[0]=="A" and cards[1]=="Q":
        print "BlackJack!"
        blackjack="true"
    elif cards[0]=="A" and cards[1]=="J":
        print "BlackJack!"
        blackjack="true"
    elif cards[0]=="A" and cards[1]==10:
        print "BlackJack!"
        blackjack="true"
    elif cards[0]=="K" and cards[1]=="A":
        print "BlackJack!"
        blackjack="true"
    elif cards[0]=="Q" and cards[1]=="A":
        print "BlackJack!"
        blackjack="true"
    elif cards[0]=="J" and cards[1]=="A":
        print "BlackJack!"
        blackjack="true"
    elif cards[0]==10 and cards[1]=="A":
        print "BlackJack!"
        blackjack="true"
cardsum=0
def givesum(cardsum):
    if type(cards[0])==int and type(cards[1])==int:
        print "Your Cards add up to "+str(cards[0]+cards[1])
        cardsum+=cards[0]+cards[1]
        blackjack="false"
    elif cards[0]=="A" and type(cards[1])==int:
        print "Your Cards add up to "+str(11+cards[1])
        cardsum+=11+cards[1]
        blackjack="false"
    elif type(cards[0])==int and cards[1]=="A":
        print "Your Cards add up to "+str(11+cards[0])
        cardsum+=11+cards[0]
        blackjack="false"
    elif cards[0]=="K" and type(cards[1])==int:
        print "Your Cards add up to "+str(10+cards[1])
        cardsum+=10+cards[1]
        blackjack="false"
    elif type(cards[0])==int and cards[1]=="K":
        print "Your Cards add up to "+str(10+cards[0])
        cardsum+=10+cards[0]
        blackjack="false"
    elif cards[0]=="Q" and type(cards[1])==int:
        print "Your Cards add up to "+str(10+cards[1])
        cardsum+=10+cards[1]
        blackjack="false"
    elif type(cards[0])==int and cards[1]=="Q":
        print "Your Cards add up to "+str(10+cards[0])
        cardsum+=10+cards[0]
        blackjack="false"
    elif cards[0]=="J" and type(cards[1])==int:
        print "Your Cards add up to "+str(10+cards[1])
        cardsum+=10+cards[1]
        blackjack="false"
    elif type(cards[0])==int and cards[1]=="J":
        print "Your Cards add up to "+str(10+cards[0])
        cardsum+=10+cards[0]
        blackjack="false"
    elif cards[0]=="K" and cards[1]=="K":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="K" and cards[1]=="Q":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="Q" and cards[1]=="K":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="K" and cards[1]=="J":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="J" and cards[1]=="K":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="Q" and cards[1]=="Q":
        print "Your Cards add up 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="Q" and cards[1]=="J":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="J" and cards[1]=="Q":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="J" and cards[1]=="J":
        print "Your Cards add up to 20"
        cardsum+=20
        blackjack="false"
    elif cards[0]=="A" and cards[1]=="A":
        print "Close Call. Your Cards add up to 12"
        cardsum+=12
        blackjack="false"

deal()
blackjack1(cards)
givesum(cardsum)
var=raw_input("Would you like another card? Enter HIT or STAND").upper()

def deal2():
    if var=="HIT" and cardsum<21 and blackjack=="false":
        cards.append(stringlist[random.randrange(0,13)])
        print cards
    elif var=="STAND":
        print "CHECK FOR DEALER'S CARDS"
deal2()

def givesum2(cardsum):
    if cards[3]=="K" or cards[3]=="Q" or cards[3]=="J" or cards[3]

Upvotes: 0

Views: 524

Answers (3)

Burhan Khalid
Burhan Khalid

Reputation: 174672

You should map cards to values, then calculate the sum of the hand, if its 21, then print 'blackjack!':

import random

cards_values = {'J': 10, 'K': 10, 'Q': 10}  # Face cards are 10
suits = ['C','H','D','S'] # Club, Heart, Diamond, Spade
cards = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
deck = ['{}{}'.format(i,k) for i in suits for k in cards]

random.shuffle(deck) # shuffle the deck
card1, card2 = deck[:2] # Get two random cards

# Each card is SuitNumber, so a two of clubs is C2
# We need to get the second value to figure out if
# we have a winning blackjack hand
# Here we are checking the second part, if its one of the face cards
# get its value, otherwise the value is its actual number
# We convert it to an integer, so we can sum it and get
# the value of the hand

value_card1 = cards_values.get(card1[1], card1[1])
value_card2 = cards_values.get(card2[1], card2[1])

hand_value = int(value_card1) + int(value_card2)

if hand_value == 21:
    print 'BlackJack!'
else:
    # We need to check if the difference is
    # 1 or 11, and the person had an Ace, he can
    # still win
    if hand_value - 21 in (1,11) and card1[1] == 'A' or card2[1] == 'A':
        print 'BlackJack!'
    else:
        print 'Oops, you lose. Your cards were {} {}'.format(card1, card2)

Upvotes: 1

Scis
Scis

Reputation: 2984

You could map various cards to values using a dictionary and then use that, plus in your blackjack1 method you could print "BlackJack!" just once...

e.g (demo):

import random
stringlist=['2','3','4','5','6','7','8','9','10',"J","Q","K","A"]

bmap = { 'A' : 11,
         'K' : 10,
         'Q' : 10,
         "J" : 10,
         '10': 10,
         '9' : 9,
         '8' : 8,
         '7' : 7,
         '6' : 6,
         '5' : 5,
         '4' : 4, 
         '3' : 3, 
         '2' :2
       }
cards=[]
cards.append(stringlist[random.randrange(0,13)])
cards.append(stringlist[random.randrange(0,13)])
print "Got %s and %s. Sum: %s"  % (cards[0],cards[1], bmap[cards[0]] + bmap[cards[1]])
if(bmap[cards[0]] + bmap[cards[1]] == 21):
    print "Yay"
else:
    print "..."

Upvotes: 2

selbie
selbie

Reputation: 104569

Here's a hint. By no means is this complete or considered production code. It may not even run or compile. I use an extra array to map the values in "stringlist" to their equivalent value. A more formal implementation would use more advanced data structures (hash table, objects). Extra work is needed for the "ace" card's dual value (11 or 1).

Also, your "stringlist" array is actually a mix of strings and integers. Consider making every item in this array a string.

stringlist=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
valuelist= [2,3,4,5,6,7,8,9,10,10, 10, 10, 11]

def getCardValue(c):
    for i in stringlist:
        if (stringlist[i] == c):
           return valuelist[i]; 
    return 0

def isBlackJack(c1, c2):
    return (21 == (getCardValue(c1)+getCardValue(c2))

def blackjack1(cards):
   if (isBlackJack(cards[0], cards[1])):
      print "BlackJack!"

Upvotes: 0

Related Questions