#| default_exp card
A simple API for creating and using playing cards
#| export
from fastcore.utils import *
#| hide
from nbdev.showdoc import *
from fastcore.test import *
#| export
suits = ["♣️","♦️","❤️","♠️"]
ranks = [None, "A"] + [str(x) for x in range(2,11)] + ["J", "Q", "K"]
We will be using numbers to represent playing card clubs and ranks. These are the suits:
suits
['♣️', '♦️', '❤️', '♠️']
For instance the suit at index 0
:
suits[0]
'♣️'
These are the ranks:
ranks
[None, 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
For instance the rank at index 1
(note that there isn't a playing card at position 0
, since we want the ranks to match the indicies where possible):
ranks[1]
'A'
#| export
class Card:
"A playing card"
def __init__(self,
suit:int, # An index into `suits`
rank:int): # An index into `ranks`
self.suit,self.rank = suit,rank
def __str__(self): return f"{ranks[self.rank]}{suits[self.suit]}"
__repr__ = __str__
Here's an example of creating and displaying a card:
c = Card(suit=1, rank=3)
c
3♦️
Equality, less than, and greater than work on the rank and suit indices.
#| export
@patch
def __eq__(self:Card, a:Card): return (self.suit,self.rank)==(a.suit,a.rank)
@patch
def __lt__(self:Card, a:Card): return (self.suit,self.rank)<(a.suit,a.rank)
@patch
def __gt__(self:Card, a:Card): return (self.suit,self.rank)>(a.suit,a.rank)
For instance, here's a test of equality...
test_eq(Card(suit=1, rank=3), Card(suit=1, rank=3))
#| hide
test_ne(Card(suit=2, rank=3), Card(suit=1, rank=3))
test_ne(Card(suit=1, rank=2), Card(suit=1, rank=3))
...and a test of <
...
assert Card(suit=1, rank=3)<Card(suit=2, rank=3)
...and finally of >
:
assert Card(suit=3, rank=3)>Card(suit=2, rank=3)
assert not Card(suit=1, rank=3)>Card(suit=2, rank=3)