Deck Module

An deck of playing cards

source

Deck

 Deck ()

A deck of 52 cards

When we initially create a deck, all cards will be presend

deck = Deck()
deck
A♠️; 2♠️; 3♠️; 4♠️; 5♠️; 6♠️; 7♠️; 8♠️; 9♠️; 10♠️; J♠️; Q♠️; K♠️; A♥️; 2♥️; 3♥️; 4♥️; 5♥️; 6♥️; 7♥️; 8♥️; 9♥️; 10♥️; J♥️; Q♥️; K♥️; A♣️; 2♣️; 3♣️; 4♣️; 5♣️; 6♣️; 7♣️; 8♣️; 9♣️; 10♣️; J♣️; Q♣️; K♣️; A♦️; 2♦️; 3♦️; 4♦️; 5♦️; 6♦️; 7♦️; 8♦️; 9♦️; 10♦️; J♦️; Q♦️; K♦️
test_eq(len(deck), 52)

As a reminder these are the suits we defined for Card

suits
['♠️', '♥️', '♣️', '♦️']

We can check if the Ace of Spades in the deck

Card(0, 1) in deck
True

source

Deck.pop

 Deck.pop (idx:int=-1)

Remove and return one card

Type Default Details
idx int -1 Index of the card to remove, defaulting to the last card
deck = Deck()
test_eq(deck.pop(), Card(3, 13)) # K♦️

source

Deck.remove

 Deck.remove (card:nbdev_experiment.card.Card)

Removes card from deck and raises an exception if not there

Type Details
card Card A card to remove
three_of_clubs = Card(2,3)
deck.remove(three_of_clubs)
assert three_of_clubs not in deck

source

Deck.shuffle

 Deck.shuffle ()

Shuffle the cards in a deck

d = Deck()
d
A♠️; 2♠️; 3♠️; 4♠️; 5♠️; 6♠️; 7♠️; 8♠️; 9♠️; 10♠️; J♠️; Q♠️; K♠️; A♥️; 2♥️; 3♥️; 4♥️; 5♥️; 6♥️; 7♥️; 8♥️; 9♥️; 10♥️; J♥️; Q♥️; K♥️; A♣️; 2♣️; 3♣️; 4♣️; 5♣️; 6♣️; 7♣️; 8♣️; 9♣️; 10♣️; J♣️; Q♣️; K♣️; A♦️; 2♦️; 3♦️; 4♦️; 5♦️; 6♦️; 7♦️; 8♦️; 9♦️; 10♦️; J♦️; Q♦️; K♦️
d.shuffle()
d
J♣️; 5♦️; 5♥️; K♣️; 4♠️; 3♣️; Q♠️; 8♦️; 7♥️; J♦️; 6♦️; A♦️; 4♣️; 9♦️; 2♠️; K♦️; 10♦️; 3♠️; Q♣️; 4♥️; 7♣️; J♥️; 8♣️; 4♦️; 6♥️; K♥️; 7♠️; 5♠️; 9♠️; A♥️; 2♦️; 9♣️; 8♠️; 3♥️; 5♣️; 2♣️; J♠️; 6♠️; 3♦️; 10♥️; 10♠️; 7♦️; 10♣️; A♣️; 2♥️; 9♥️; A♠️; K♠️; Q♥️; 8♥️; 6♣️; Q♦️

source

draw_n

 draw_n (n:int, replace:bool=True)

Draw n cards from a deck with replacement (if replace=True)

Type Default Details
n int Number of cards to draw
replace bool True wheather or not you want replacement
draw_n(5)
[Q♣️, 3♣️, K♦️, A♣️, 6♠️]