The new Python match
Card Game
The next example is a card game that brings together everything discussed so far (Listing 4). The hand of cards is defined in the program as the ace of spades, jack of clubs, 2 and 9 of hearts, and 5, 6, and 9 of diamonds (lines 22-28).
Listing 4
cards.py
01 from dataclasses import dataclass 02 03 @dataclass 04 class playingCard: 05 rank: int 06 # 11 - Jack 07 # 12 - Queen 08 # 13 - King 09 # 14 - Ace 10 suit: str 11 12 def cardName ( card ): 13 match card: 14 case playingCard ( rank , suit ) if rank <= 10: 15 print ( "{0} of {1}".format ( rank , suit ) ) 16 case playingCard ( rank = 11 , suit = _ ): print ( "Jack of " + suit ) 17 case playingCard ( rank = 12 , suit = _ ): print ( "Queen of " + suit ) 18 case playingCard ( rank = 13 , suit = _ ): print ( "King of " + suit ) 19 case playingCard ( rank = 14 , suit = _ ): print ( "Ace of " + suit ) 20 21 deck = list() 22 deck.append ( playingCard ( 14 , "Spades" ) ) 23 deck.append ( playingCard ( 11 , "Clubs" ) ) 24 deck.append ( playingCard ( 2 , "Hearts" ) ) 25 deck.append ( playingCard ( 9 , "Hearts" ) ) 26 deck.append ( playingCard ( 5 , "Diamonds" ) ) 27 deck.append ( playingCard ( 6 , "Diamonds" ) ) 28 deck.append ( playingCard ( 9 , "Diamonds" ) ) 29 30 print ( "-- Face Cards --" ) 31 for card in deck: 32 match card: 33 case playingCard ( rank , suit ) if rank >= 11: 34 cardName ( card ) 35 36 print ( "-- Hearts -- " ) 37 for card in deck: 38 match card: 39 case playingCard ( rank , suit = "Hearts" ): 40 cardName ( card ) 41 42 print ( "-- Nines --" ) 43 for card in deck: 44 match card: 45 case playingCard ( rank = 9 ): 46 cardName ( card ) 47 48 print ( "-- Odd numbered non-face cards --" ) 49 for card in deck: 50 match card: 51 case playingCard ( rank ) if rank % 2 == 1 and rank <= 10: 52 cardName ( card ) 53 54 print ( "-- Odd numbered Diamonds --" ) 55 for card in deck: 56 match card: 57 case playingCard ( rank , suit = "Diamonds" ) if rank % 2 == 1 and rank <= 10: 58 cardName ( card )
To begin, the program imports dataclass
, which is a helper class that handles some basic definitions. Adding the @dataclass
decorator before the definition (line 3) generates the __init__
function automatically and assigns attributes of the same name to class variables. Therefore, in lines 22-28, where a hand of cards is defined, the values are automatically assigned to rank
and suit
. The match
already knows how to work with a dataclass
, so __match_args__
does not have to be defined, as in an earlier example. The variables rank
and suit
are defined in lines 5 and 10.
Card Names
The cardName
function uses a match
(line 13) to generate a human-readable string of the card name. Line 14 checks to see whether both rank
and suit
are defined in a case
statement. If both are present, the program checks for a numerical card with the guard if rank <= 10
. If it matches, the print
outputs the string {0} of {1}
, where format
replaces 0 with rank
and 1 with suit
.
The rest of the case
statements (lines 16-19) check for a particular rank
by specifying =11
, =12
, and so on. The suit = _
element says that suit
must be defined in the instance, but it doesn't matter what value it holds. Each case
then prints the name of the card and its suit. Note that you can check for a particular value just by specifying it in a case
.
Each of the remaining code blocks uses different combinations of case
and guards to extract different cards from the player's hand.
Card Matching
The match
in lines 32-34 checks that a rank
and suit
is present and then uses the guard if rank >= 11
to return only cards ranked 11 or higher.
The -- Hearts --
print block (lines 36-40) only has to specify suit = "Hearts"
in the case
statement for all of the hearts in the hand. The -- Nines --
case (lines 42-46) only specifies rank = 9
. Optionally it could specify suit
with no condition to make sure it is defined.
The non-face cards section (lines 48-52) is a complex condition, but it is still just a single line in the guard. It requests rank
and then uses rank % 2
to check for a remainder. If true, then the card is odd. It also checks rank <= 10
so that only non-face cards are returned.
Finally, the program specifies the suit
so only diamonds are matched (lines 54-58). The guard if rank % 2 == 1 and rank <= 10
checks for an odd number as described before and then eliminates all cards that are not numbers (Figure 3).
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4