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
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.
-
Plasma Desktop 6.1.4 Release Includes Improvements and Bug Fixes
The latest release from the KDE team improves the KWin window and composite managers and plenty of fixes.
-
Manjaro Team Tests Immutable Version of its Arch-Based Distribution
If you're a fan of immutable operating systems, you'll be thrilled to know that the Manjaro team is working on an immutable spin that is now available for testing.