Advertisement
proffreda

Deck of Cards Python Classes

Mar 13th, 2016
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import random
  2.  
  3. class Card:
  4.  
  5.     suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
  6.     rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7",
  7.               "8", "9", "10", "Jack", "Queen", "King"]
  8.  
  9.     def __init__(self, suit, rank):
  10.         self.suit = suit
  11.         self.rank = rank
  12.  
  13.     def __str__(self):
  14.         return (Card.rank_names[self.rank] + " " +
  15.                              Card.suit_names[self.suit])
  16.  
  17. class DeckOfCard:
  18.  
  19.     def __init__(self):
  20.         self.cards = []
  21.         for suit in range(4):
  22.             for rank in range(1, 14):
  23.                 card = Card(suit, rank)
  24.                 self.cards.append(card)
  25.  
  26.     def __str__(self):
  27.         result = "\n"
  28.         for card in self.cards:
  29.             result += str(card) + "\n"
  30.         return result
  31.  
  32.     def shuffle(self):
  33.         random.shuffle(self.cards)
  34.    
  35.  
  36.        
  37.  
  38. deck = DeckOfCard()
  39. deck.shuffle()
  40. print(deck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement