One of the things I like most about Damian Conway's books is his use of clear, resonant, verisimilar code examples. I try to emulate his writing when I answer questions on PerlMonks.

The other day, in a reply to another monk's inquiry, I recommended he add a control break to his program. I decided afterwards to write an example Perl script to demonstrate simple control break processing. Dealing straight poker hands seemed like a good, familiar real-world process to use as the basis of the example.

#!perl

use strict;
use warnings;
use utf8;
use open qw( :encoding(UTF-8) :std );

use List::Util qw( shuffle );

@ARGV == 1 or die "Usage: perl $0 <total rounds>\n";

my $TOTAL_ROUNDS = shift;

my @players = qw( Bob Carol Ted Alice );
my $player_to_dealers_right = $players[-1]; # Alice

my $round = 1;
my %hand_of;
my @deck;
my @cards;

for my $rank (qw( 2 3 4 5 6 7 8 9 10 J Q K A )) {
    for my $suit (qw( ♠ ♥ ♦ ♣ )) {
        push @cards, $rank . $suit;
    }
}

ROUND:
while ($round <= $TOTAL_ROUNDS) {
    @deck = shuffle(@cards);

    SHUFFLE:
    while (@deck) {

        PLAYER:
        for my $player (@players) {
            push @{ $hand_of{$player} }, pop @deck;
        }

        if (@{ $hand_of{$player_to_dealers_right} } == 5) {
            print "Round $round\n";

            for my $player (@players) {
                my $hand = join ' ', @{ $hand_of{$player} };
                print "$player has $hand\n";
            }

            print "\n";

            %hand_of = ();

            last ROUND if ++$round > $TOTAL_ROUNDS;
        }
    }
}

__END__
Round 1
Bob has 7♥ 8♠ Q♥ 2♦ 9♥
Carol has 6♦ Q♠ 2♠ A♠ 3♦
Ted has K♥ 10♦ 5♦ 7♠ A♥
Alice has J♠ 10♥ 6♣ 6♥ K♠

Round 2
Bob has A♣ A♦ 9♣ 4♣ 2♥
Carol has 7♣ 4♦ 4♥ 8♥ 8♣
Ted has 6♠ J♣ K♦ 3♣ 9♠
Alice has 9♦ 7♦ Q♦ 8♦ 10♣

Round 3
Bob has 5♠ K♣ 3♠ 7♦ 7♥
Carol has 5♣ Q♣ 2♣ 9♣ Q♦
Ted has 3♥ 4♠ 10♠ J♦ 5♥
Alice has J♦ 5♥ J♥ A♠ 10♠

Round 4
Bob has K♣ J♥ 7♠ 4♠ J♠
Carol has 10♦ 9♦ Q♥ 9♥ 6♥
Ted has 10♣ 4♦ 4♣ 3♣ K♥
Alice has A♣ 5♦ 2♦ 3♦ 8♣

Round 5
Bob has 5♣ 2♣ 6♠ K♦ 2♠
Carol has A♦ 5♠ 6♣ 10♥ 4♥
Ted has 2♥ 9♠ A♥ Q♠ 7♣
Alice has 3♥ Q♣ K♠ 8♦ 8♠

Round 6
Bob has J♣ 2♠ J♥ 9♥ 6♠
Carol has 8♥ 10♣ 9♣ 5♣ 4♦
Ted has 3♠ K♥ K♣ A♦ Q♣
Alice has 6♦ 9♠ 3♥ 8♦ 9♦

Round 7
Bob has Q♥ 8♥ Q♦ 5♥ 3♣
Carol has 6♥ 10♥ 5♦ 7♣ 2♦
Ted has 10♦ 4♠ 4♥ 8♠ 7♦
Alice has J♠ 7♥ J♦ K♦ 2♣

Round 8
Bob has J♣ Q♠ 5♠ A♠ Q♠
Carol has 6♣ 8♣ A♥ A♣ 5♦
Ted has 10♠ 3♦ 3♠ 4♣ 6♥
Alice has 2♥ 6♦ K♠ 7♠ 7♠

Round 9
Bob has 3♥ 9♥ 8♦ 2♦ 7♣
Carol has 10♠ 6♦ 2♣ 4♣ J♠
Ted has 5♣ 4♥ 4♠ 3♠ 3♣
Alice has J♦ 7♦ 2♠ 8♥ Q♣

Round 10
Bob has 7♥ 10♦ J♥ A♣ 8♣
Carol has 9♦ 5♥ K♠ A♥ K♥
Ted has 5♠ 8♠ 4♦ 9♣ 2♥
Alice has A♦ 9♠ 6♠ 3♦ J♣

I don't play poker myself, so I got some of the poker lingo from the Wikipedia Poker article (for example, "round").

It occurred to me only after I had written the script that perhaps a computer program to deal straight poker hands isn't the best, most typical example of control break processing. What do you think?

Jim

  • Comment on Perl Example of Control Break Processing

Replies are listed 'Best First'.
Re: Perl Example of Control Break Processing
by ww (Archbishop) on Sep 10, 2011 at 10:45 UTC

    OK; most of us don't spend our time programing Five-card stud|draw... and your program lacks verisimilitude, in that (for example) Ted might reasonably fold after the first or second round of betting in Round 2...

    BUT, more important, your program includes no code specific to what you intended to demonstrate (or if it does, I'm misreading "Control Break" and should crawl back in my hole and shutup; 'shaddup!', that is, except for offering the notion that if the illustration is there, you may want to highlight it in the code, and accompany that with explanatory narrative.

    That said, however, the existing code is clear and reasonably short (but remember, publishers -- in effect -- pay by the line for that which they print, so you'll be dealing with an editor with a bias toward VERY short code samples.

      [Y]our program includes no code specific to what you intended to demonstrate …, that is, except for offering the notion that if the illustration is there, you may want to highlight it in the code, and accompany that with explanatory narrative.

      Good suggestion. Yes, Damian Conway would have highlighted the two control breaks in the example code and then elaborated on them in the text.

      The two control breaks are the two if-then constructs. The first one occurs when the dealer has dealt the fifth card to the player to his right. The second one—the terminal one—occurs after the nth round of play.

      I suppose one could argue there's a third control break in the program that occurs when the deck of cards is exhausted and reshuffled. It's implicit in the loop construct, so its implementation is different than the other two control breaks. But I realized after I wrote the script that poker isn't played this way. The deck is reassembled and shuffled after each hand.

Re: Perl Example of Control Break Processing
by zentara (Cardinal) on Sep 10, 2011 at 12:39 UTC
    It would be good for you to wrap your code with code tags instead of pre tags, so a download code link would appear.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      It would be good for you to wrap your code with code tags instead of pre tags, so a download code link would appear.

      I didn't use <code></code> tags for this script simply because PerlMonks doesn't display the Unicode suit characters ♠, ♥, ♦ and ♣ properly when I do. See these discussions of the topic.

      Here's what the same script looks like when I enclose it in <code></code> tags: