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.

Figure 1: The blackjack dealer has dealt himself a face-up and a face-down card and each of the six players at the table two face-up cards. Image © Tomas Hajek, 123rf

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

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

  • Python match

    Exploring the new Python match statement, Python's implementation of switch/case.

  • Perl: Automating Color Correction

    If you have grown tired of manually correcting color-casted images (as described in last month's Perl column), you might appreciate a script that automates this procedure.

  • Perl: Retouching Photos

    In many cases, whole series of digital images need the same kind of modifications, which forces the photo-grapher to repeat the same steps time and time again in GIMP. Have you ever considered retouching in Perl?

  • Calculating Probability

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

  • Perl: Call Waiting

    If you can’t get through to your home number, a script can check the line status and send a signal to you via the Web to tell you when the line is free.

comments powered by Disqus
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.

Learn More

News