Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################
- ## Create a Deck of Cards
- ## Shuffle Deck of Cards
- ## Display shuffled deck
- ## Do with objects
- ## and without objects
- ##########################
- # Using random for the shuffle
- import random
- import timeit
- print "Shuffling a deck of cards"
- print
- # Store card combinations in tuples
- cards = ("A","2","3","4","5","6","7","8","9","10","J","Q","K")
- suits = ("<>","<3","%","&")
- # Create a lookup dictionary with a key of 1 to 52, and the value of the card.
- # This allows all our sorting and shuffling to be done on integers rather than strings
- deckDict = dict(enumerate(card+" "+suit for card in cards for suit in suits))
- print "-----------------USING FUNCTIONS------------------------"
- # Function to create a new shuffled deck given an unshuffled deck
- def shuffle(deck):
- # Create a copy of the deck that we will shuffle
- shuffledDeck = deck[:]
- # Python is doing the work for us with the random.shuffle()
- random.shuffle(shuffledDeck)
- # Return the shuffled deck
- return shuffledDeck
- # Function to print out the deck of cards
- def printDeck(deck, label=""):
- # Variable to store the printed list of cards. Add label if provided
- if label:
- output = "{}: ".format(label)
- else:
- output = ""
- # Loop through the cards in the deck and add the card to our print veriable
- #output = " | ".join(deckDict[card] for card in deck)
- for card in deck:
- output += "{:10}".format(deckDict[card])
- # Print deck
- print output,"\n"
- # Create a deck of cards using list comprehension
- originalDeck = range(52)
- raw_input("Press ENTER to continue")
- # Print the unshuffled deck
- printDeck(originalDeck, "Unshuffled deck")
- raw_input("Press ENTER to continue")
- # Shuffle the deck
- newDeck = shuffle(originalDeck)
- # Print the shuffled deck
- printDeck(newDeck, "Shuffled deck")
- raw_input("Press ENTER to continue")
- # Shuffle again and print in the same line
- printDeck(shuffle(newDeck), "Shuffled again")
- raw_input("Press ENTER to continue")
- # Now try the same thing using an object to represent the deck of cards
- print "-----------------USING OBJECTS------------------------"
- class Deck(object):
- # Class initialization function will create the cards list
- def __init__(self):
- # Create a deck of cards using list comprehension and store in the new cards property
- self.cards = range(52)
- # Function that will be used in comparing/sorting a group of these objects. This would probably make more
- # sense if implemented in a HandOfCards object to determine the winner of a hand.
- def __cmp__(self,other):
- return cmp(self.cards,other.cards)
- # Function that will be used to represent the object
- def __repr__(self):
- # Just return all the cards in a string
- return "\n{} {} \n".format(self.__class__," | ".join(deckDict[card] for card in self.cards))
- # Function that will be used to convert this object to a string
- # Unfortunately, when the print method is used on a sequence of these objects, it doesn't
- # use this, it uses __repr__ http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python
- def __str__(self):
- # Variable to store the printed list of cards.
- out = ""
- # Loop through the cards in the deck and add the card to our print veriable
- for card in self.cards:
- out += "{:10}".format(deckDict[card])
- # Return the printed representation of the deck
- return out+"\n\n"
- # Function to shuffle this deck object
- def shuffle(self):
- # Python doing the work for us with the random.shuffle()
- # Note: no need for a copy deck since we are shuffling ourselves
- random.shuffle(self.cards)
- raw_input("Press ENTER to continue")
- # Create our new deck object and print it
- deckObj = Deck()
- print "Unshuffled deck object: {} \n".format(deckObj)
- raw_input("Press ENTER to continue")
- # Shuffle our deck object and print it - twice
- deckObj.shuffle()
- print "Shuffled deck object: {} \n".format(deckObj)
- raw_input("Press ENTER to continue")
- deckObj.shuffle()
- print "Shuffled again: {} \n".format(deckObj)
- raw_input("Press ENTER to continue")
- # Create a list of deck objects
- decks = [Deck() for x in range(5)]
- # Print out the list of deck objects
- print "List of Deck objects",decks
- raw_input("Press ENTER to continue")
- # Shuffle them all
- for deck in decks:
- deck.shuffle()
- # Print out the list of shuffled deck objects
- print "List of shuffled Deck objects",decks
- raw_input("Press ENTER to continue")
- # Print out the sorted list of deck objects
- print "Sorted List of Deck objects",sorted(decks)
- raw_input("Press ENTER to continue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement