Advertisement
Guest User

deckofcards2.py

a guest
May 16th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | None | 0 0
  1. #########################
  2. ## Create a Deck of Cards
  3. ## Shuffle Deck of Cards
  4. ## Display shuffled deck
  5. ## Do with objects
  6. ## and without objects
  7. ##########################
  8.  
  9. # Using random for the shuffle
  10. import random
  11. import timeit
  12.  
  13. print "Shuffling a deck of cards"
  14. print
  15.  
  16. # Store card combinations in tuples
  17. cards = ("A","2","3","4","5","6","7","8","9","10","J","Q","K")
  18. suits = ("<>","<3","%","&")
  19.  
  20. # Create a lookup dictionary with a key of 1 to 52, and the value of the card.
  21. # This allows all our sorting and shuffling to be done on integers rather than strings
  22. deckDict = dict(enumerate(card+" "+suit for card in cards for suit in suits))
  23.  
  24. print "-----------------USING FUNCTIONS------------------------"
  25.  
  26. # Function to create a new shuffled deck given an unshuffled deck
  27. def shuffle(deck):
  28.     # Create a copy of the deck that we will shuffle
  29.     shuffledDeck = deck[:]
  30.  
  31.     # Python is doing the work for us with the random.shuffle()
  32.     random.shuffle(shuffledDeck)
  33.  
  34.     # Return the shuffled deck
  35.     return shuffledDeck
  36.  
  37. # Function to print out the deck of cards
  38. def printDeck(deck, label=""):
  39.  
  40.     # Variable to store the printed list of cards. Add label if provided
  41.     if label:
  42.         output = "{}: ".format(label)
  43.     else:
  44.         output = ""
  45.  
  46.     # Loop through the cards in the deck and add the card to our print veriable
  47.     #output = " | ".join(deckDict[card] for card in deck)
  48.     for card in deck:
  49.         output += "{:10}".format(deckDict[card])
  50.  
  51.     # Print deck
  52.     print output,"\n"
  53.  
  54. # Create a deck of cards using list comprehension
  55. originalDeck = range(52)
  56.  
  57. raw_input("Press ENTER to continue")
  58.  
  59. # Print the unshuffled deck
  60. printDeck(originalDeck, "Unshuffled deck")
  61. raw_input("Press ENTER to continue")
  62.  
  63. # Shuffle the deck
  64. newDeck = shuffle(originalDeck)
  65.  
  66. # Print the shuffled deck
  67. printDeck(newDeck, "Shuffled deck")
  68. raw_input("Press ENTER to continue")
  69.  
  70. # Shuffle again and print in the same line
  71. printDeck(shuffle(newDeck), "Shuffled again")
  72. raw_input("Press ENTER to continue")
  73.  
  74. # Now try the same thing using an object to represent the deck of cards
  75. print "-----------------USING OBJECTS------------------------"
  76.  
  77. class Deck(object):
  78.     # Class initialization function will create the cards list
  79.     def __init__(self):
  80.  
  81.         # Create a deck of cards using list comprehension and store in the new cards property
  82.         self.cards = range(52)
  83.  
  84.     # Function that will be used in comparing/sorting a group of these objects. This would probably make more
  85.     # sense if implemented in a HandOfCards object to determine the winner of a hand.
  86.     def __cmp__(self,other):
  87.         return cmp(self.cards,other.cards)
  88.  
  89.     # Function that will be used to represent the object
  90.     def __repr__(self):
  91.  
  92.         # Just return all the cards in a string
  93.         return "\n{} {} \n".format(self.__class__," | ".join(deckDict[card] for card in self.cards))
  94.  
  95.     # Function that will be used to convert this object to a string
  96.     # Unfortunately, when the print method is used on a sequence of these objects, it doesn't
  97.     #  use this, it uses __repr__ http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python
  98.     def __str__(self):
  99.  
  100.         # Variable to store the printed list of cards.
  101.         out = ""
  102.  
  103.         # Loop through the cards in the deck and add the card to our print veriable
  104.         for card in self.cards:
  105.             out += "{:10}".format(deckDict[card])
  106.  
  107.         # Return the printed representation of the deck
  108.         return out+"\n\n"
  109.  
  110.  
  111.     # Function to shuffle this deck object
  112.     def shuffle(self):
  113.  
  114.         # Python doing the work for us with the random.shuffle()
  115.         # Note: no need for a copy deck since we are shuffling ourselves
  116.         random.shuffle(self.cards)
  117.  
  118. raw_input("Press ENTER to continue")
  119.  
  120. # Create our new deck object and print it
  121. deckObj = Deck()
  122. print "Unshuffled deck object: {} \n".format(deckObj)
  123. raw_input("Press ENTER to continue")
  124.  
  125. # Shuffle our deck object and print it - twice
  126. deckObj.shuffle()
  127. print "Shuffled deck object: {} \n".format(deckObj)
  128. raw_input("Press ENTER to continue")
  129.  
  130. deckObj.shuffle()
  131. print "Shuffled again: {} \n".format(deckObj)
  132. raw_input("Press ENTER to continue")
  133.  
  134. # Create a list of deck objects
  135. decks = [Deck() for x in range(5)]
  136.  
  137. # Print out the list of deck objects
  138. print "List of Deck objects",decks
  139. raw_input("Press ENTER to continue")
  140.  
  141. # Shuffle them all
  142. for deck in decks:
  143.     deck.shuffle()
  144.    
  145. # Print out the list of shuffled deck objects
  146. print "List of shuffled Deck objects",decks
  147. raw_input("Press ENTER to continue")
  148.  
  149. # Print out the sorted list of deck objects
  150. print "Sorted List of Deck objects",sorted(decks)
  151. raw_input("Press ENTER to continue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement