daily pastebin goal
58%
SHARE
TWEET

Blackjack.py

a guest Feb 19th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. from time import sleep as s
  3.  
  4. pointref = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'J':10,'Q':10,'K':10}
  5.  
  6. def draw(deck):
  7.     card = deck.pop(deck.index(random.choice(deck)))
  8.     return (card,deck)
  9.  
  10. def getPointTotal(hand):
  11.     points = 0
  12.     checkAces = 0
  13.     for i in hand:
  14.         if i[0:2] == '10':
  15.             points += 10
  16.         elif i[0] == 'A':
  17.             checkAces += 1
  18.             points += 11
  19.         else:
  20.             points += pointref.get(i[0])
  21.     while points > 21 and checkAces > 0:
  22.         points -= 10
  23.         checkAces -= 1
  24.     return points
  25.  
  26. def check(inp):
  27.     while True:
  28.         if inp == 'y' or inp == 'n':
  29.             return inp
  30.         else:
  31.             inp = input('Please try again. (y/n) ')
  32.  
  33. def pull(deck,myhand,p = 'n'):
  34.     card, deck = draw(deck)
  35.     myhand.append(card)
  36.     if p != 'n':
  37.         print(p, 'drew', card + '.')
  38.     return myhand
  39.  
  40. def postDraw(myhand,opphand):
  41.     mypoints = getPointTotal(myhand)
  42.     opoints = getPointTotal(opphand[1:len(opphand)])
  43.     if mypoints <= 21:
  44.         print('Your hand is:', myhand, 'and is worth', mypoints, 'points.')
  45.         s(0.5)
  46.         print('The dealer has', opphand[1:len(opphand)], 'and one face down card. Their face up cards are worth', opoints, 'points.')
  47.         s(0.5)
  48.     if mypoints == 21:
  49.         print('You have a blackjack! No need to play on.')
  50.         return (21,'n')
  51.     elif mypoints > 21:
  52.         print('You bust! You score 0 points.')
  53.         return (0,'n')
  54.     elif mypoints < 21:
  55.         hit = input('Would you like to draw another card? (y/n) ')
  56.         hit = check(hit)
  57.         if hit == 'y':
  58.             print('You take another card.')
  59.         elif hit == 'n':
  60.             print('You pass with', mypoints, 'points.')
  61.         return (mypoints,hit)
  62.  
  63. def dealer(myhand,opphand,deck,hit):
  64.     mypoints = getPointTotal(myhand)
  65.     opoints = getPointTotal(opphand[1:len(opphand)])
  66.     if mypoints == 21:
  67.         print('The dealer passes.')
  68.         return (21,False)
  69.     elif mypoints > 21:
  70.         print('The dealer busts.')
  71.         return(0,False)
  72.     elif mypoints <= opoints + 2:
  73.         print('The dealer takes a card.')
  74.         return (mypoints,True)
  75.     elif mypoints <= 14:
  76.         print('The dealer takes a card.')
  77.         return (mypoints,True)
  78.     elif len(opphand) == 2 and hit == 'n' and mypoints <= 17:
  79.         print('The dealer takes a card.')
  80.         return (mypoints,True)
  81.     elif len(opphand) == 3 and hit == 'n' and mypoints <= 18:
  82.         print('The dealer takes a card.')
  83.         return (mypoints,True)
  84.     elif len(opphand) > 2 and mypoints <= 16:
  85.         print('The dealer takes a card.')
  86.         return (mypoints,True)
  87.     elif len(opphand) > 3 and mypoints <= 17:
  88.         print('The dealer takes a card.')
  89.         return (mypoints,True)
  90.     return (mypoints,False)
  91.  
  92. def game():
  93.     pscore = 0
  94.     dscore = 0
  95.     playerwins = 0
  96.     dealerwins = 0
  97.     game = 'y'
  98.     while game == 'y':
  99.         deck = ['A♥️','2♥️','3♥️','4♥️','5♥️','6♥️','7♥️','8♥️','9♥️','10♥️','J♥️','Q♥️','K♥️','A♣️','2♣️','3♣️','4♣️','5♣️','6♣️','7♣️','8♣️','9♣️','10♣️','J♣️','Q♣️','K♣️','A♦️','2♦️','3♦️','4♦️','5♦️','6♦️','7♦️','8♦️','9♦️','10♦️','J♦️','Q♦️','K♦️','A♠️','2♠️','3♠️','4♠️','5♠️','6♠️','7♠️','8♠️','9♠️','10♠️','J♠️','Q♠️','K♠️']
  100.         play = True
  101.         init = True
  102.         phand = []
  103.         dhand = []
  104.         phit = 'y'
  105.         dhit = True
  106.         print('The current score is:')
  107.         print('You:', playerwins)
  108.         print('Dealer:', dealerwins)
  109.         s(0.5)
  110.         while play == True:
  111.             if init == True:
  112.                 phand = pull(deck,phand)
  113.                 dhand = pull(deck,dhand)
  114.                 phand = pull(deck,phand)
  115.                 dhand = pull(deck,dhand)
  116.                 pscore = getPointTotal(phand)
  117.                 dscore = getPointTotal(dhand)
  118.                 init = False
  119.             if dscore <= 21 and dscore != 0 and phit == 'y':
  120.                 pscore, phit = postDraw(phand,dhand)
  121.                 s(0.5)
  122.                 if phit == 'y':
  123.                     phand = pull(deck,phand,'You')
  124.                     pscore = getPointTotal(phand)
  125.                     s(0.5)
  126.             if pscore <= 21 and pscore != 0 and dhit == True:
  127.                 dscore, dhit = dealer(dhand,phand,deck,phit)
  128.                 s(0.5)
  129.                 print(dhit)
  130.                 if dhit == True:
  131.                     dhand = pull(deck,dhand,'The dealer')
  132.                     dscore = getPointTotal(dhand)
  133.                     s(0.5)
  134.             if pscore == 0 or dscore == 0:
  135.                 phit == 'n'
  136.                 dhit = False
  137.             if phit == 'n' and dhit == False:
  138.                 play = False
  139.         print('Your final hand is', str(phand) + ', worth', pscore,  'points.')
  140.         s(0.5)
  141.         print('The dealer finishes with the hand', str(dhand) + ', worth', dscore, 'points.')
  142.         s(0.5)
  143.         if pscore > dscore:
  144.             playerwins += 1
  145.             print("You win!")
  146.         elif dscore > pscore:
  147.             dealerwins += 1
  148.             print('The dealer wins.')
  149.         elif pscore == dscore:
  150.             print("It's a tie! Nobody wins.'")
  151.         s(0.5)
  152.         game = input('Play again? (y/n) ')
  153.         game = check(game)
  154.         s(0.5)
  155.     print('The final score is:')
  156.     print('You:', playerwins)
  157.     print('Dealer:', dealerwins)
  158.     s(0.5)
  159.     print('Thanks for playing!')
  160.    
  161. game()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top