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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
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.