Perl 6 in a hands-on test
Blackjack in the Terminal
In the command-line version of the script featured in Figure 2, blackjack.p6
, the dealer follows the "soft 17" rule and plays against one other player. Figure 2 shows a game in which the player wins against the dealer. First, the dealer gets a Q
(for queen) and a face-down card, which the script does not display. The player receives two 2s and types h
("hit"), to ask for one more card. It is an ace (A
), and given that an ace in blackjack is either 1 or 11, the value of the hand of cards is now 5 or 15, which the script shows as any(5,15)
.
Another hit gives the player a 10; the hand of cards is either 15 or 25, although the latter gets discarded being over 21. The player boldly presses h
again, receives a 6 as if by a miracle, and now has precisely 21, as the alternative count of 31 makes no sense. The player presses s
("stand"), and it's the dealer's turn. The dealer turns over the face-down card (a 4), so that he or she now has Q-4
, a queen and a 4, which makes 14 all told. Because 14 is less than 17, the dealer draws one more card in accordance with the rules of the casino, but gets a jack (J
) and has now gone bust with 27: game over. The player wins the point (score: 1
), and his total score (total
) is now 1 as well, because this was the first game.
Cards in a Poker Hand
The playing cards in a 52-card hand are represented by a Blackjack::Deck class object in Listing 2. So that the Perl parser knows that Perl 6 code follows and won't choke on the new syntax, the file starts with use v6
: This defines the version in a syntax that the Perl 5 interpreter also understands causing it to abort with an error message should someone accidentally point Perl 5 at the program. Line 7 uses the following expression
Listing 2
Deck.pm
flat < A J Q K >, 2..10
to define a list of possible values of cards (ace, ten, jack, queen, king), as well as the numbers from 2 to 10.
Space-separated values in angle brackets are the Perl 6 syntax for the qw(...)
lists in Perl 5. The range operator ..
existed in Perl 5 too, except it returned a long list of values, whereas Perl 6 returns a single item that contains that list. Therefore, the expression above needs an additional flat
operator to flatten out the whole thing and create a long list.
To simulate all the cards, the shuffle()
method in line 15 of Listing 2 iterates through the card attributes in two for loops, permutating @.ranks
and @.suits
, and combining two entries for each card of the Blackjack::Card class. The code then pushes the object created in this way onto the @.cards
array. Lines 18-23 determine the count value for each card, the 1 | 11
entry for an ace is one of the previously discussed superpositions (called "junctions") of the values 1 and 11.
Zipper
The screen color assigned to the card colors by the %.suit_color
hash is determined by a mapping between @.suits
and a flattened list of display colors in line 12. The Z
operator at the end of line 11 applies the zip function to the two lists and assigns a screen color to each suit in the hash. The for loops in Perl 6 are syntactically different from loops in Perl 5; the for
keyword is followed by a list or an array and then an arrow operator and the name of the variable to assume the current iteration value within the loop block.
To mix the deck, line 33 uses a clever trick involving the so-called "whatever" operator *
. The pick()
method selects a random element from an array, removes it from the array, and returns the element. The * tells Perl 6 in this construct to continue until the array is empty and then shuffles the original array and returns it as a list. The values are then assigned to the variable on the left side of the equation. A user-defined method of same name defined at line 36 removes a card from the mixed deck and returns it to the caller.
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
-
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.
-
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.