towwey

playing cards

Apr 30th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. #
  2. # playing_cards module - PSP Assignment 1, sp2, 2017.
  3. # DO NOT MODIFY!
  4. #
  5.  
  6. import random
  7.  
  8.  
  9. # Deck of cards - first letter represents the face value and
  10. # second letter represents the suit
  11. deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
  12.         'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
  13.         'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
  14.         'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']
  15.  
  16. # Playing deck in use
  17. playing_deck = []
  18.  
  19.  
  20. # Function to determine whether there are any cards left in the
  21. # deck of playing cards
  22. # Parameters: No parameters
  23. # Returns: True if the deck is empty, False otherwise
  24. def is_empty_deck():
  25.  
  26.     # Check to see whether playing deck is empty
  27.     return len(playing_deck) == 0
  28.  
  29.  
  30. # Function to rebuild and shuffle the deck
  31. # Parameters: No parameters
  32. # Returns: Nothing is returned from the function.
  33. def reset_deck():
  34.     global playing_deck
  35.  
  36.     # Create new playing deck
  37.     playing_deck = deck.copy()
  38.    
  39.     # Shuffle deck
  40.     random.shuffle(playing_deck)
  41.  
  42.  
  43. # Function to deal one card
  44. # Parameters: No parameters
  45. # Returns: A string (containing two characters) representing
  46. # the card delt, i.e. '2H' meaning 2 of Hearts
  47. def deal_one_card():
  48.    
  49.     # Check to see whether there are any cards left
  50.     if is_empty_deck():
  51.    
  52.         # Rebuild and shuffle deck
  53.         reset_deck()
  54.  
  55.     # Return a card (string of two characters)
  56.     return playing_deck.pop(0)
Add Comment
Please, Sign In to add comment