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
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
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.