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).

Figure 3: The output of cards.py, demonstrating different combinations of case and guard statements.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Hidden Meaning: Working with Microformats

    Programs aren’t as smart as humans when it comes to interpreting the meaning of web information. If you want to maximize your search rank, you might want to dress up your HTML documents with microformats and microdata.

  • Practical Python in Linux

    We’ll introduce you to Python, an easy-to-learn scripting language, and also help you get started creating your own practical Python scripts.

  • Calculating Probability

    To tackle mathematical problems with conditional probabilities, math buffs rely on Bayes' formula or discrete distributions, generated by short Perl scripts.

  • Escape Room Puzzle

    A digital puzzle presents a challenge for young people in an escape room.

  • Treasure Hunt

    A geolocation guessing game based on the popular Wordle evaluates a player's guesses based on the distance from and direction to the target location. Mike Schilli turns this concept into a desktop game in Go using the photos from his private collection.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News