Skip to content

Instantly share code, notes, and snippets.

@Hamatti
Created March 11, 2021 10:26
Show Gist options
  • Save Hamatti/559eda8c45a1e00f0c5dcc4243613da0 to your computer and use it in GitHub Desktop.
Save Hamatti/559eda8c45a1e00f0c5dcc4243613da0 to your computer and use it in GitHub Desktop.
Python pattern matching for Pokemon cards. Works on Python 3.10a6
class Card:
def __init__(self, id, name, supertype, subtype):
self.id = id
self.name = name
self.supertype = supertype
self.subtype = subtype
charizard = Card('ex3-100', 'Charizard', 'Pokemon', 'Stage 2')
lapras = Card('ex3-103', 'Lapras', 'Pokemon', 'Basic')
muscle_band = Card('xy1-121', 'Muscle Band', 'Trainer', 'Item')
def play_card(card):
match card:
case Card(supertype='Pokemon', subtype='Basic'):
print("Play to bench")
case (Card(supertype='Pokemon', subtype='Stage 1') |
Card(supertype='Pokemon', subtype='Stage 2')):
print("Play on top of another Pokemon card")
case Card(supertype='Trainer'):
print("Play to discard pile")
play_card(charizard) # Prints Play on top of another Pokemon card
play_card(lapras) # Prints Play to bench
play_card(muscle_band) # Prints Play to discard pile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment