#| default_exp card
Define a card in a deck of cards
#|export
from fastcore.all import *
import random
#|export
suits = ["♣️", "♦️", "❤️", "♠️"]
ranks = [None, "A"] + [str(x) for x in range(2,11)] + ["J", "Q", "K"]
#|export
class Card:
"Represents a standard playing card."
def __init__(self, suit=0, rank=2):
self.suit,self.rank = suit, rank
self.suit_nm,self.rank_nm = suits[self.suit],ranks[self.rank]
def __eq__(self, other): return (self.suit, self.rank) == (other.suit, other.rank)
def __lt__(self, other): return (self.suit, self.rank) < (other.suit, other.rank)
def __str__(self): return f'{self.rank_nm}{self.suit_nm}'
__repr__ = __str__
Card is a class that represents a single card in a deck of cards. We can create cards like this:
c = Card(suit=1, rank=3)
c
3♦️
In these docs we'll generally show the expected output from our code like so:
test_eq(str(c), '3♦️')
c2 = Card(suit=2, rank=11)
test_eq(str(c2), 'J❤️')
:::{.callout-tip}
These test_eq statements are not just documentation, they are also unit tests! These cells will get tested automatically with continous integration. You can also run tests locally with the command nbdev_test. If you do not want to show a test in your docs, you can choose to hide cells with the #|hide directive.
:::
You can also compare cards like so:
test_eq(c2 > c, True)
#|hide
assert Card(suit=1, rank=3) == Card(suit=1, rank=3)
#|hide
from nbdev.doclinks import nbdev_export
nbdev_export()