Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- suits = ['hearts', 'spades', 'clubs', 'diamonds']
- cards = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
- def popDeck():
- _deck = []
- for s in range(4):
- # populate each suit with Ace to King, and with values
- # it creates a list of 52 dictionary entries
- for card in [{'card':card, 'val':min(val+1, 10), 'suit':suits[s]} for val, card in enumerate(cards)]:
- _deck.append(card)
- for card in _deck:
- if card['card'] is 'Ace':
- card['val'] = 11
- return _deck
- def randCard():
- index = random.randint(0,len(deck)-1) # index of deck
- card = deck[index] # random card
- deck.pop(index) # remove card from deck
- return card # return it for use / play
- def viewHand(i):
- print('Player', i + 1, 'has', sumTotal(i), 'points:')
- for card in player_hand[i]:
- print('\t', card['card'], 'of', card['suit'])
- def sumTotal(i):
- # player_total = 0
- # for card in player_hand[i]:
- # player_total += card['val']
- return sum([v['val'] for v in player_hand[i]])
- deck = popDeck()
- print('Welcome to BlackJack!')
- num_players = int(input('How many players in this game?\n'))
- game_running = True
- # player_total = [x[:] for x in [[num_players]*4]][0]
- player_hand = []
- for i in range(num_players):
- player_hand.append([randCard(), randCard()])
- viewHand(i)
- while game_running:
- for i in range(len(player_hand)):
- answer = input('Player %d do you want to hit or pass with %d points?' % (i + 1, sumTotal(i)))
- if answer is 'y' or answer is ' ':
- player_hand[i].append(randCard())
- print('Here\'s another card.')
- viewHand(i)
- if sumTotal(i) > 21:
- player_hand.pop(i)
- print('Player', i + 1, 'loses.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement