Advertisement
here2share

# card_deck_main.py

Nov 5th, 2022
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. # card_deck_main.py
  2.  
  3. import random
  4.  
  5. DEBUG = False
  6.  
  7.  
  8. class Card:
  9.     '''Card class: contains num and suit and allows one to compare
  10.    two cards for equality -- based on number only.
  11.    Ace is 14.
  12.    String representation is NNS -- two spaces for the number and 1
  13.    space for the suit.  J = jack, Q = queen, K = king, A = ace.
  14.    '''
  15.  
  16.     def __init__(self, num, suit):
  17.         '''Hold information about a card: its num and suit, whether it is
  18.        showing or not, whether it is locked down or not, its x,y position, etc.'''
  19.  
  20.         # num is a string ('2', '3', ... 'jack', 'queen', etc.): convert to
  21.         # integer 2 - 14.
  22.         try:
  23.             self._num = int(num)
  24.         except:
  25.             # Must be jack, queen, etc.
  26.             if num == 'J':
  27.                 self._num = 11
  28.             elif num == 'Q':
  29.                 self._num = 12
  30.             elif num == 'K':
  31.                 self._num = 13
  32.             else:
  33.                 self._num = 14
  34.         self._suit = suit
  35.  
  36.         # For use in games, when a card is worth a certain number of points.
  37.         self._points = 0
  38.  
  39.     def __eq__(self, other):
  40.         return self._num == other._num and self._suit == other._suit
  41.  
  42.     def getNum(self):
  43.         return self._num
  44.  
  45.     def getSuit(self):
  46.         return self._suit
  47.  
  48.     def getPoints(self):
  49.         return self._points
  50.  
  51.     def setPoints(self, pts):
  52.         if DEBUG:
  53.             print('for card', str(self), 'points set to ', pts)
  54.         self._points = pts
  55.  
  56.     def __str__(self):
  57.         if self._num <= 10:
  58.             res = str(self._num)
  59.         elif self._num == 11:
  60.             res = 'J'
  61.         elif self._num == 12:
  62.             res = 'Q'
  63.         elif self._num == 13:
  64.             res = 'K'
  65.         else:
  66.             res = 'A'
  67.         return "%3s" % (res + self._suit)
  68.  
  69.  
  70. class Deck:
  71.     '''Deck class: contains a list of card objects.'''
  72.  
  73.     NUMS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
  74.     SUITS = ['C', 'H', 'D', 'S']
  75.  
  76.     def __init__(self, cards=None):
  77.         self._deck = []
  78.         if cards is not None:
  79.             self._deck.extend(cards)
  80.  
  81.     def __str__(self):
  82.         res = []
  83.         for card in self._deck:
  84.             res.append(str(card))
  85.         return str(res)
  86.  
  87.     def addCard(self, card):
  88.         self._deck.append(card)
  89.  
  90.     def addCards(self, cards):
  91.         self._deck.extend(cards)
  92.  
  93.     def shuffle(self):
  94.         random.shuffle(self._deck)
  95.  
  96.     def addAllCards(self):
  97.         '''Create all card for a deck and shuffle the deck.
  98.        '''
  99.         for numIdx in range(len(self.NUMS)):
  100.             for suitIdx in range(len(self.SUITS)):
  101.                 card = Card(self.NUMS[numIdx], self.SUITS[suitIdx])
  102.                 self.addCard(card)
  103.         self.shuffle()
  104.  
  105.     def takeTopCard(self):
  106.         return self._deck.pop(0)
  107.  
  108.     def numCards(self):
  109.         return len(self._deck)
  110.  
  111.     def getCards(self):
  112.         '''Return the list of card objects.'''
  113.         return self._deck
  114.  
  115.     def makeCopy(self):
  116.         return Deck(self._deck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement