Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. """ Model a blackjack game to learn about classes and complete bootcamp project """
  2. import random  # insecure but functional shuffling
  3.  
  4.  
  5. class Deck:
  6.  
  7.     """ using RANKS and SUITS, builds a full 52-card deck of playing cards. """
  8.  
  9.     RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
  10.     SUITS = ['c', 'h', 'd', 's']
  11.     #  TODO: might want to move values and scoring to another object
  12.     VALUES = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  13.               'T': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11}  # ace can be 1 also
  14.  
  15.     def __init__(self, ranks=RANKS, suits=SUITS, values=VALUES):
  16.         self._ranks = ranks
  17.         self._suits = suits
  18.         self._values = values
  19.         self._deck = self.sealed_deck()
  20.  
  21.     def sealed_deck(self):
  22.         fresh_deck = [(f'{rank}{suit}')
  23.                       for rank in self._ranks for suit in self._suits]
  24.         return fresh_deck
  25.  
  26.     def shuffle_deck(self):
  27.         random.shuffle(self._deck)
  28.         return self._deck  # might not want to return anything , shuffle in place?
  29.  
  30.     def deal_a_card(self):
  31.         current_card = self._deck.pop()
  32.         return current_card
  33.  
  34.     def score_hand(self, hand):
  35.         """ move to another object? """
  36.         pass
  37.  
  38.     def __str__(self):
  39.         return f'{len(self._deck)} cards in the deck --\n deck is unshuffled: {self._deck == self.sealed_deck()} --\n cards:\n \
  40.            {self._deck}'
  41.  
  42.  
  43. blackjack_deck = Deck()
  44. blackjack_deck.shuffle_deck()
  45. print(blackjack_deck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement