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

Figure 2: The blackjack game in action: Here the casino player wins against the dealer.

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


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


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

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

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

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

  • Perl: Perl Debugger

    The Perl interpreter, perl, doesn’t often crash, but if it happens, not even the excellent Perl debugger will be able to help you.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95