Perl 6 in a hands-on test
Blackjack in Perl 6
So, in 2004, I wrote a column with a script that simulated a blackjack game for Linux Magazine [2] using superpositions to help count the value(s) of a hand of cards. This month, with my newly acquired Perl 6 knowledge, I thought it could be worthwhile to try to implement the same blackjack program in modern Perl 6.
Listing 1 [3] shows how Perl 6 implements the class code for a single blackjack card from the deck. Cards are valued by their ranks; the Blackjack::Card
class thus has an attribute named rank
in Listing 1 with a Perl scalar as the default type capable of accommodating a string or an integer. If you have worked in Perl 5 with the Moose module, you do not need to change all that much for Perl 6; the new syntax is almost identical.
Listing 1
Card.pm
In addition to a suit
, each card also has a count value val
and a color (suit_color)
when displayed on the screen. The definitions in Listing 1 begin with a dollar sign followed by a dot, which distinguishes these attributes as a Perl scalars. Perl 6 also offers arrays (@) and hashes (%) for attributes, as well as arbitrary class names. The dot after the "sigils," as these special characters are known, distinguishes a variable as an object attribute.
Perl 6 defines handy methods for read and write access behind the scenes at the same time (for example, obj.rank("A")
to set the value) and named parameter lists for the constructor, which then understands calls such as Blackjack::Card.new( rank => "A")
. Important: Perl 6 replaces the arrow notation ->
by a dot; $o->foo()
in Perl 5 thus becomes $o.foo()
in Perl 6. The self
keyword (note the missing sigil) is used to access an object within the object itself.
Strings in Perl 6 can include not only automatically interpolated variables, such as "Hello $guest"
, but also dynamically executed code constructs in curly brackets. This is a big help, especially with the new object syntax if the value does not exist in a variable but originates from a method: "Hello {$o.guest()}"
hence replaces the curly brackets and everything in between with the return value of the guest()
method for an object that resides in $o
.
Blackjack 101
How is blackjack played in a casino in Las Vegas? At a card table, the dealer stands facing the players and deals cards from a 52-card deck in a shoe (Figure 1). To make life harder for the card counters among the players in the casino, a shoe contains up to eight shuffled decks of 52 cards. First, the dealer deals two cards to each player at the table, and then deals him/herself one face-up and one face-down card.
Then it is the players' turn; a player can either ask for more cards from the dealer ("hit") or politely refuse ("stand"). The goal of the game is to get as close as possible to the magic number of 21 with all drawn cards – but without exceeding this number – because the player otherwise automatically loses ("bust"). Once all players are satisfied, the dealer reveals the face-down card and may possibly draw more cards from the shoe.
Soft and Hard 17
Like robots, dealers in Las Vegas play a fixed system: They draw new cards until they have at least 17 points. For example, if a 10 comes out of the shoe, followed by a six, the dealer draws again, even though this will typically mean overshooting the target 21 and going bust, although an opponent may have, for example, already stopped at 13 to avoid drawing a 10 and going bust.
This rule is identical for all casinos, with a minor variation, the so-called "soft 17" [4]. This rule determines whether a dealer with a hand of 17, including at least one ace (which counts as either 1 or 11), needs to draw again. If the dealer draws, for example, an ace and a six, they have 17 and would no longer draw a card following the "hard 17" rule. If the casino uses the "soft 17" rule, however, the dealer must continue, because this 17 (if the ace is counted as 1 instead) can also be interpreted as 1 + 6 = 7. Statistically, this gives the dealer a small advantage; their chances of winning grow by 0.2 percent.
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.