Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import random
  2.  
  3. suits = ['hearts', 'spades', 'clubs', 'diamonds']
  4. cards = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
  5.  
  6. def popDeck():
  7.     _deck = []
  8.     for s in range(4):
  9.         # populate each suit with Ace to King, and with values
  10.         # it creates a list of 52 dictionary entries
  11.         for card in [{'card':card, 'val':min(val+1, 10), 'suit':suits[s]} for val, card in enumerate(cards)]:
  12.           _deck.append(card)
  13.  
  14.     for card in _deck:
  15.         if card['card'] is 'Ace':
  16.             card['val'] = 11
  17.     return _deck
  18.  
  19. def randCard():
  20.     index = random.randint(0,len(deck)-1) # index of deck
  21.     card = deck[index] # random card
  22.     deck.pop(index) # remove card from deck
  23.     return card # return it for use / play
  24.  
  25. def viewHand(i):    
  26.     print('Player', i + 1, 'has', sumTotal(i), 'points:')
  27.     for card in player_hand[i]:
  28.         print('\t', card['card'], 'of', card['suit'])
  29.  
  30. def sumTotal(i):
  31.     # player_total = 0
  32.     # for card in player_hand[i]:
  33.     #     player_total += card['val']
  34.     return sum([v['val'] for v in player_hand[i]])
  35.  
  36. deck = popDeck()
  37.  
  38. print('Welcome to BlackJack!')
  39. num_players = int(input('How many players in this game?\n'))
  40. game_running = True
  41.  
  42. # player_total = [x[:] for x in [[num_players]*4]][0]
  43. player_hand = []
  44. for i in range(num_players):
  45.     player_hand.append([randCard(), randCard()])
  46.     viewHand(i)
  47.  
  48. while game_running:
  49.     for i in range(len(player_hand)):
  50.         answer = input('Player %d do you want to hit or pass with %d points?' % (i + 1, sumTotal(i)))
  51.         if answer is 'y' or answer is ' ':
  52.             player_hand[i].append(randCard())
  53.             print('Here\'s another card.')
  54.         viewHand(i)
  55.         if sumTotal(i) > 21:
  56.             player_hand.pop(i)
  57.             print('Player', i + 1, 'loses.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement