Advertisement
HytheCoderDojo

Deck of cards

May 22nd, 2025
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """Colin_Deck_of_Cards.ipynb
  3.  
  4. Automatically generated by Colab.
  5.  
  6. Original file is located at
  7.    https://colab.research.google.com/drive/1QjobjAhqL9r3RBxo8v4mtefxlbGV1TE3
  8. """
  9.  
  10.  
  11.  
  12. """Objective, create code to make a list of a deck of cards A - K, Hearts, Clubs, Diamonds and Spades and 2 x Jokers
  13.  
  14. Notation
  15. H - Hearts
  16. C - Clubs
  17. D - Diamonds
  18. S - Spade
  19.  
  20. 1 - Ace
  21. 2 - 10 2 - 10
  22. J - Jack
  23. Q - Queen
  24. K - King
  25.  
  26. I accept there are other ways to do this, lol.  (Array for example)
  27. """
  28.  
  29. deck = [] # empty list to populate
  30. suites = ["H", "C", "D", "S"]
  31. values = [] # I know I could do "1", "2"....  but trying to be clever
  32.  
  33. for i in range (1,11): # need to start from 1
  34.   values.append(str(i))
  35. values.append("J")
  36. values.append("Q")
  37. values.append("K")
  38.  
  39. print(suites, values) #testing progress
  40.  
  41. # running through suites and values to build the decks
  42. for suit in suites:
  43.   for value in values:
  44.     deck.append(value + suit) # concatenation here
  45.  
  46. print(deck) # yep seems good
  47.  
  48. # add jokers
  49.  
  50. deck.append("J1")
  51. deck.append("J2")
  52.  
  53. print(deck) # yep
  54.  
  55. print(len(deck)) # expecing 54 got 54
  56.  
  57. """Does this look ok?
  58.  
  59. I dont like the 10.  change them to T?
  60. I don't like the 1.  Change them to A?
  61.  
  62. We can shuffle a deck
  63.  
  64. Looks good, 😀
  65. My verbose and overly engineered version below 😆, I've have added card value and a couple of functions for shuffling / picking n number of cards...
  66. """
  67.  
  68. class Cards:
  69.     RANK_ORDER = {
  70.         '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
  71.         '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14
  72.     } # rank as str and int (for value... later on that!)
  73.     SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
  74.  
  75.     def __init__(self, rank: str, suit: str):
  76.         self.rank = rank
  77.         self.suit = suit
  78.         self.value = self.RANK_ORDER[rank]
  79.  
  80.     def __repr__(self) -> str:
  81.         return f'{self.rank} of {self.suit}'
  82.  
  83.  
  84. def create_deck():
  85.     return [Cards(rank, suit) for suit in Cards.SUITS for rank in Cards.RANK_ORDER] # fancy list comprehesion!
  86.  
  87. print(create_deck())
  88.  
  89. import random
  90.  
  91. def shuffle_deck(deck: list[Cards]) -> None:
  92.     """Shuffles Cards"""
  93.     random.shuffle(deck)
  94.  
  95. def pick_n_cards(deck: list[Cards], n: int) -> list[Cards]:
  96.     """
  97.    Picks specified num of  cards at random from the deck (without replacement).
  98.  
  99.    If n > len(deck), returns all cards in a shuffled order.
  100.    """
  101.     if n > len(deck):
  102.         n = len(deck)
  103.  
  104.     # remove picked cards from deck:
  105.     selected = random.sample(deck, n)
  106.     for card in selected:
  107.         deck.remove(card)
  108.     return selected
  109.  
  110.  
  111. deck = create_deck()
  112. shuffle_deck(deck)
  113. my_hand = pick_n_cards(deck, 5)
  114. your_hand  = pick_n_cards(deck, 5)
  115.  
  116. print(f'My hand is: {my_hand}')
  117. print(f'Your hand is: {your_hand}')
  118.  
  119. # can go even further and create a game or two!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement