Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """Colin_Deck_of_Cards.ipynb
- Automatically generated by Colab.
- Original file is located at
- https://colab.research.google.com/drive/1QjobjAhqL9r3RBxo8v4mtefxlbGV1TE3
- """
- """Objective, create code to make a list of a deck of cards A - K, Hearts, Clubs, Diamonds and Spades and 2 x Jokers
- Notation
- H - Hearts
- C - Clubs
- D - Diamonds
- S - Spade
- 1 - Ace
- 2 - 10 2 - 10
- J - Jack
- Q - Queen
- K - King
- I accept there are other ways to do this, lol. (Array for example)
- """
- deck = [] # empty list to populate
- suites = ["H", "C", "D", "S"]
- values = [] # I know I could do "1", "2".... but trying to be clever
- for i in range (1,11): # need to start from 1
- values.append(str(i))
- values.append("J")
- values.append("Q")
- values.append("K")
- print(suites, values) #testing progress
- # running through suites and values to build the decks
- for suit in suites:
- for value in values:
- deck.append(value + suit) # concatenation here
- print(deck) # yep seems good
- # add jokers
- deck.append("J1")
- deck.append("J2")
- print(deck) # yep
- print(len(deck)) # expecing 54 got 54
- """Does this look ok?
- I dont like the 10. change them to T?
- I don't like the 1. Change them to A?
- We can shuffle a deck
- Looks good, 😀
- 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...
- """
- class Cards:
- RANK_ORDER = {
- '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
- '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14
- } # rank as str and int (for value... later on that!)
- SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
- def __init__(self, rank: str, suit: str):
- self.rank = rank
- self.suit = suit
- self.value = self.RANK_ORDER[rank]
- def __repr__(self) -> str:
- return f'{self.rank} of {self.suit}'
- def create_deck():
- return [Cards(rank, suit) for suit in Cards.SUITS for rank in Cards.RANK_ORDER] # fancy list comprehesion!
- print(create_deck())
- import random
- def shuffle_deck(deck: list[Cards]) -> None:
- """Shuffles Cards"""
- random.shuffle(deck)
- def pick_n_cards(deck: list[Cards], n: int) -> list[Cards]:
- """
- Picks specified num of cards at random from the deck (without replacement).
- If n > len(deck), returns all cards in a shuffled order.
- """
- if n > len(deck):
- n = len(deck)
- # remove picked cards from deck:
- selected = random.sample(deck, n)
- for card in selected:
- deck.remove(card)
- return selected
- deck = create_deck()
- shuffle_deck(deck)
- my_hand = pick_n_cards(deck, 5)
- your_hand = pick_n_cards(deck, 5)
- print(f'My hand is: {my_hand}')
- print(f'Your hand is: {your_hand}')
- # can go even further and create a game or two!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement