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
-
Zorin OS 17 Beta Available for Testing
The upcoming version of Zorin OS includes plenty of improvements to take your PC to a whole new level of user-friendliness.
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.