Deck
Deck ()
Represents a deck of cards
Deck ()
Represents a deck of cards
A Deck of cards is a collection of Card
objects:
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♠️
There are 52 cards in a deck.
Deck.pop (index=-1)
Removes and returns card index
from the deck
Type | Default | Details | |
---|---|---|---|
index | int | -1 | Card number to pop |
There are 51 cards left in the deck now.
You can show the docs for methods not created with patch
by calling show_doc
. For example, the code show_doc(Deck.remove)
produces the following documentation:
Deck.remove (card:cards_deck.card.Card)
Removes card
from the deck or raises exception if it is not there
Type | Details | |
---|---|---|
card | Card | Card to remove |
If we remove a card from the Deck we can verify that it no longer exists:
However, another card that we haven’t removed, such as the 10 of hearts
will still be in the Deck of cards because we haven’t removed it:
Hand ()
Represents a deck of cards
move_cards (source:__main__.Deck, dest:__main__.Hand, num:int)
Pop the given number of cards from the deck and move to dest
.
Type | Details | |
---|---|---|
source | Deck | deck to move cards from |
dest | Hand | destination to move cards to |
num | int | number of cards to move |
You might be wondering: “what are these comments are following each parameter?” These are called docments, a concise way of documenting your code that also renders beautifully in nbdev. nbdev also supports rendering numpy-style docstrings as well.
Let’s try something fun with our deck of cards, drawing a card with replacement:
draw_n (n:int, replace:bool=True)
Draw n
cards, with replacement iif replace
Type | Default | Details | |
---|---|---|---|
n | int | number of cards to draw | |
replace | bool | True | whether or not draw with replacement |
This isn’t terribly interesting from a statistical perspective. However, its an example of how you can include visualizations in your nbdev projects!
Notice how we are hiding just the input with #|echo: false
, so readers can see the output but hide the code. You can also fold the code using the #|code-fold: true
directive.
We can create a CLI with @call parse
draw_cards (n:int, replace:bool=True, outfile:str=None)
Draw n
cards optionally with replacement
Type | Default | Details | |
---|---|---|---|
n | int | number of cards to draw | |
replace | bool | True | whether or not draw with replacement |
outfile | str | None | output file, defaults to stdout |
We normally wouldn’t repeat all of these arguments when one function is wrapping another one. Instead, we would use delegates. However, we wanted to keep this tutorial simple, so we didn’t use that here.